1、 实战案例:CentOS 7 实现MySQL的多实例
实验目的 CentOS 7.7 yum安装mariadb并实现三个实例
1.安装mariadb
yum install mariadb-server
Systemctl start mariadb
2.准备三个实例的目录
mkdir -pv /mysql/{3306,3307,3308}/{data,etc,socket,log,bin,pid}
chown -R mysql.mysql /mysql
3. 生成数据库文件
mysql_install_db --datadir=/mysql/3306/data --user=mysql
mysql_install_db --datadir=/mysql/3307/data --user=mysql
mysql_install_db --datadir=/mysql/3308/data --user=mysql
4. 准备配置文件
cp /etc/my.cnf /mysql/3306/etc/
vim /mysql/3306/etc/my.cnf
port=3306
datadir=/mysql/3306/data/
socket=/mysql/3306/socket/mysql.sock
[mysqld_safe]
log-error=/mysql/3306/log/mariadb.log
pid-file=/mysql/3306/pid/mariadb.pid
![f57fa0a20e9ccd2069350605d322f5b3.png](https://img.php1.cn/3cd4a/1eebe/cd5/bff2716168d1ed7b.webp)
重复上面步骤设置3307,3308
准备启动脚本
vim /mysql/3306/bin/mysqld
![879d9f304a834e4fb6ed1b605ed6ff31.png](https://img.php1.cn/3cd4a/1eebe/cd5/e88efe5b0a13a7fa.webp)
![5b4f8e9dce25132c73e54f203710cd22.png](https://img.php1.cn/3cd4a/1eebe/cd5/70be2ca197098d98.webp)
!/bin/bash
port=3306
mysql_user="root"
mysql_pwd="magedu"
cmd_path="/usr/bin"
mysql_basedir="/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
#${cmd_path}/mysqladmin -u ${mysql_user} -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
6.测试启动3306实例
![fc8775f694ace27b86b2cd549b6b6ccb.png](https://img.php1.cn/3cd4a/1eebe/cd5/bcafc120671304eb.webp)
![f778d11077953cfd86fbcf7fa296f3a1.png](https://img.php1.cn/3cd4a/1eebe/cd5/2d903861d5ad779c.png)
7.停止服务
![21b7ea3f88a71333b634b092703e023b.png](https://img.php1.cn/3cd4a/1eebe/cd5/0a0ce631ec450943.webp)
![3553f44c5588256760ee739bcf1b0086.png](https://img.php1.cn/3cd4a/1eebe/cd5/0a0ce631ec450943.webp)
![ca5438e25521dd6a2bb401ebf95e53ee.png](https://img.php1.cn/3cd4a/1eebe/cd5/67cc2e96eddffff8.png)
登录测试
mysqladmin -uroot -S /mysql/3306/socket/mysql.sock password 'magedu' #加上新口令
mysql -uroot -pmagedu -S /mysql/3306/socket/mysql.sock
![02d3fc8b153e5a5f4fe99ec6d10023f2.png](https://img.php1.cn/3cd4a/1eebe/cd5/6789f68dabde0aed.png)
![399f03370cb6f5f2460cb64d20a43d90.png](https://img.php1.cn/3cd4a/1eebe/cd5/60405fda58cd0acd.webp)
![b1cf1d58d99e80d5845fd467953e163c.png](https://img.php1.cn/3cd4a/1eebe/cd5/2fdc212433a29829.png)
重复上述过程,分别建立3307,3308的启动脚本
到此演示完毕