步骤1 环境准备
安装多实例数据库
1)安装好数据库mariadb
yum -y install mariadb-server
2)创建mysql多实例数据文件目录
mkdir /data/mysql/3307/{data,etc,socket,log,bin,pid} -pv
mkdir /data/mysql/3308/{data,etc,socket,log,bin,pid} --pv
chown -R mysql.mysql /data/mysql
mysql_install_db --datadir=/data/mysql/3307/data/ --user=mysql
mysql_install_db --datadir=/data/mysql/3308/data/ --user=mysql
步骤2 创建配置文件
cp -a /etc/my.cnf /data/mysql/3307/etc/
vi /data/mysql/3307/etc/my.cnf
[mysqld]
port=3307
datadir=/data/mysql/3307/data
socket=/data/mysql/3307/socket/mysql.sock
[mysqld_safe]
log-error=/data/mysql/3307/log/mariadb.log
pid-file=/data/mysql/3307/pid/mariadb.pid
cp /data/mysql/3307/etc/my.cnf /data/mysql/3308/etc/
vi /data/mysql/3308/etc/my.cnf
编辑启动脚本
vi /data/mysql/3307/bin/mysqld
#!/bin/bash
port=3307
mysql_user="root"
mysql_pwd=""
cmd_path="/usr/bin"
mysql_basedir="/data/mysql"
mysql_sock="{mysql_basedir}/{port}/socket/mysql.sock"
function_start_mysql()
{
if [ ! -e "$mysql_sock" ];then
printf "Starting MySQL...\n"
{cmd_path}/mysqld_safe --defaults-file={mysql_basedir}/${port}/etc/my.cnf &> /dev/null &
else
printf "MySQL is running...\n"
exit
fi
}
function_stop_mysql()
{
if [ ! -e "$mysql_sock" ];then
printf "MySQL is stopped...\n"
exit
else
printf "Stoping MySQL...\n"
${cmd_path}/mysqladmin -u {mysql_user} -p{mysql_pwd} -S ${mysql_sock} shutdown
fi
}
function_restart_mysql()
{
printf "Restarting MySQL...\n"
function_stop_mysql
sleep 2
function_start_mysql
}
case $1 in
start)
function_start_mysql
;;
stop)
function_stop_mysql
;;
restart)
function_restart_mysql
;;
*)
printf "Usage: {mysql_basedir}/{port}/bin/mysqld {start|stop|restart}\n"
esac
vi /data/mysql/3308/bin/mysqld
#!/bin/bash
port=3308
mysql_user="root"
mysql_pwd=""
cmd_path="/usr/bin"
mysql_basedir="/data/mysql"
mysql_sock="{mysql_basedir}/{port}/socket/mysql.sock"
function_start_mysql()
{
if [ ! -e "$mysql_sock" ];then
printf "Starting MySQL...\n"
{cmd_path}/mysqld_safe --defaults-file={mysql_basedir}/${port}/etc/my.cnf &> /dev/null &
else
printf "MySQL is running...\n"
exit
fi
}
function_stop_mysql()
{
if [ ! -e "$mysql_sock" ];then
printf "MySQL is stopped...\n"
exit
else
printf "Stoping MySQL...\n"
${cmd_path}/mysqladmin -u {mysql_user} -p{mysql_pwd} -S ${mysql_sock} shutdown
fi
}
function_restart_mysql()
{
printf "Restarting MySQL...\n"
function_stop_mysql
sleep 2
function_start_mysql
}
case $1 in
start)
function_start_mysql
;;
stop)
function_stop_mysql
;;
restart)
function_restart_mysql
;;
*)
printf "Usage: {mysql_basedir}/{port}/bin/mysqld {start|stop|restart}\n"
esac
chmod +x /data/mysql/3307/bin/mysqld
chmod +x /data/mysql/3308/bin/mysqld
步骤3 数据库启动
cd /usr/bin
./mysqld_safe --defaults-file=/data/mysql/3307/etc/my.cnf &
chmod 644 /data/mysql/3307/etc/my.cnf
./mysqld_safe --defaults-file=/data/mysql/3307/etc/my.cnf &
ps -elf|grep mariadb
netstat -tanp | grep 3307
./mysqld_safe --defaults-file=/data/mysql/3308/etc/my.cnf &
chmod 644 /data/mysql/3308/etc/my.cnf
./mysqld_safe --defaults-file=/data/mysql/3308/etc/my.cnf &
ps -elf|grep mariadb
netstat -tanp | grep 3308
步骤4 多实例验证
登录多实例数据库
mysql -uroot -S /data/mysql/3307/socket/mysql.sock
show variables like '%port%';
mysql -uroot -S /data/mysql/3308/socket/mysql.sock
show variables like '%port%';