实验: yum 安装包的实现多实例
以yum安装的包,或者二进制安装的包都可以做多实例。现在用yum 安装的包来做。
先装包 yum install mariadb-server
装完之后在主机上就有一个实例了,然后在这个实例上进行修改变成多个实例。
系统默认的数据库是放在 /var/lib/mysql 目录下,现在不用这个,自己来创建,将来把各自的实例数据文件以及一些配置文件、日志文件、进程文件。socket文件等放在各自的目录下。
mkdir /mysqldb/{3306,3307,3308}/{etc,socket,pid,data} -pv
为了让mysql 用户对创建的目录具有控制权限,要求把所有者跟所属组都修改成 mysql 。因为这是yum 安装,所以不用创建 mysql 用户,会自动生成,秩序修改权限即可。
chown -R mysql.mysql /mysqldb/
接下来要把用户数据库文件生成。
mysql_install_db --datadir=/mysqldb/3306/data --user=mysql
mysql_install_db --datadir=/mysqldb/3307/data --user=mysql
mysql_install_db --datadir=/mysqldb/3308/data --user=mysql
接下来准备配置文件
系统自带的配置文件在 /etc/my.cnf 目录下,复制到各自实例下准备的配置文件目录中。
cp /etc/my.cnf /mysqldb/3306/etc/
cp /etc/my.cnf /mysqldb/3307/etc/
cp /etc/my.cnf /mysqldb/3308/etc/
然后修改各自的配置文件
vim /mysqldb/3306/etc/my.cnf
注释掉 includedir /etc/my.cnf.d 这行
添加 port=3306
修改其他行 datadir=/mysqldb/3306/data
socket=/mysqldb/3306/socket/mysql.sock
log-error=/mysqldb/3306/log/mariadb.log
pid-file=/mysqldb/3306/pid/mariadb.pid
vim /mysqldb/3307/etc/my.cnf
注释掉 includedir /etc/my.cnf.d 这行
添加 port=3307
修改其他行 datadir=/mysqldb/3307/data
socket=/mysqldb/3307/socket/mysql.sock
log-error=/mysqldb/3307/log/mariadb.log
pid-file=/mysqldb/3307/pid/mariadb.pid
vim /mysqldb/3308/etc/my.cnf
注释掉 includedir /etc/my.cnf.d 这行
添加 port=3308
修改其他行 datadir=/mysqldb/3308/data
socket=/mysqldb/3308/socket/mysql.sock
log-error=/mysqldb/3308/log/mariadb.log
pid-file=/mysqldb/3308/pid/mariadb.pid
这样各种文件就准备好了,接下来就是启动脚本了。
我们现在要准备三个脚本,三个配置文件,来适用于三个实例
vim /mysqldb/3306/mysqld
#!/bin/bash
port=3306
mysql_user="root"
mysql_pwd="centos"
cmd_path="/app/mysql/bin"
mysql_basedir="/mysqldb"
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} -S ${mysql_sock} shutdown
#${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
vim /mysqldb/3307/mysqld
#!/bin/bash
port=3307
mysql_user="root"
mysql_pwd="centos"
cmd_path="/app/mysql/bin"
mysql_basedir="/mysqldb"
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} -S ${mysql_sock} shutdown
#
${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
vim /mysqldb/3308/mysqld
#!/bin/bash
port=3308
mysql_user="root"
mysql_pwd="centos"
cmd_path="/app/mysql/bin"
mysql_basedir="/mysqldb"
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} -S ${mysql_sock} shutdown
#${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 700 /mysqldb/3306/mysqld
chmod 700 /mysqldb/3307/mysqld
chmod 700 /mysqldb/3308/mysqld
然后就可以运行了
/
mysqldb/3306/mysqld start
/mysqldb/3307/mysqld start
/mysqldb/3308/mysqld start
启动后可以用命令查看下端口打开没 ss -ntl
启动后还会自动生成各自的日志文件,socket 文件,pid 文件等。
接下来就可以连接了。但是不能跟传统连接一样,要跟上socket 文件路径
mysql -s /mysqldb/3308/socket/mysql.sock
那怎么能知道连接的是哪个端口的呢。
用命令 show variables like '%port%'; 就可以查看详细信息
其他两个实例一样。
如果要停掉服务跟上 stop 参数就可以了。
/mysqldb/3308/mysqld stop
这样就把3308 端口的服务停掉了。其他两个实例一样。
到此多实例实验就实现了。