1.mkdir -p /data/330{7,8,9}/data
2.生成配置文件
cat >> /data/3307/my.cnf << EOF
[mysqld]
basedir=/app/mysql
datadir=/data/3307/data
socket=/data/3307/mysql.sock
log_error=/data/3307/mysql.log
port=3307
server_id=7
log_bin=/data/3307/mysql-bin
EOF

cat >> /data/3308/my.cnf << EOF
[mysqld]
basedir=/app/mysql
datadir=/data/3308/data
socket=/data/3308/mysql.sock
log_error=/data/3308/mysql.log
port=3308
server_id=8
log_bin=/data/3308/mysql-bin
EOF

cat >> /data/3309/my.cnf << EOF
[mysqld]
basedir=/app/mysql
datadir=/data/3309/data
socket=/data/3309/mysql.sock
log_error=/data/3309/mysql.log
port=3309
server_id=9
log_bin=/data/3309/mysql-bin
EOF
3.初始化数据
注意:初始化的时候会读取/etc/my.cnf 所以先把它挪走
mv /etc/my.cnf  /etc/my.cnf.bak
mysqld --initialize-insecure --user=mysql --basedir=/app/mysql --datadir=/data/3307/data
mysqld --initialize-insecure --user=mysql --basedir=/app/mysql --datadir=/data/3308/data
mysqld --initialize-insecure --user=mysql --basedir=/app/mysql --datadir=/data/3309/data

修改配置
cd /etc/systemd/system
cp mysqld.service mysqld3307.service
cp mysqld.service mysqld3308.service
cp mysqld.service mysqld3309.service
vim mysqld3307.service
vim mysqld3308.service
vim mysqld3309.service
修改一下倒数第二句,修改加载的默认配置文件
ExecStart=/app/mysql/bin/mysqld --defaults-file=/data/3307/my.cnf
ExecStart=/app/mysql/bin/mysqld --defaults-file=/data/3308/my.cnf
ExecStart=/app/mysql/bin/mysqld --defaults-file=/data/3309/my.cnf

4.授权
chown -R mysql.mysql /data/*
启动
systemctl start mysqld3307.service
systemctl start mysqld3308.service
systemctl start mysqld3309.service
自启动
systemctl enable mysqld3307.service
systemctl enable mysqld3308.service
systemctl enable mysqld3309.service
5.验证多实例
netstat -lnp|grep 330
mysql -S /data/3307/mysql.sock -e "select @@server_id"
mysql -S /data/3308/mysql.sock -e "select @@server_id"
mysql -S /data/3309/mysql.sock -e "select @@server_id"

把配置文件弄回来
mv /etc/my.cnf.bak  /etc/my.cnf
View Code

这就是多实例的搭建

01-31 15:56