#准备多个目录
mkdir-p /data/330{7,8,9}/data
#创建配置文件
cat>> /data/3307/my.cnf<
[mysqld]
basedir=/usr/local/mysql
datadir=/data/3307/data
pid-file=/data/3307/data/mysql.pid
log_error=/data/3307/data/mysql.log
socket=/data/3307/mysql.sock
log_bin=/data/3307/mysql-bin
user=mysql
port=3307server_id=3307EOF
#复制配置文件
cp/data/3307/my.cnf /data/3308cp/data/3307/my.cnf /data/3309#修改地址
sed-i 's#3307#3308#g' /data/3308/my.cnf
sed-i 's#3307#3309#g' /data/3309/my.cnf
#初始化数据库
mysqld--initialize-insecure --user=mysql --datadir=/data/3307/data --basedir=/usr/local/mysql
mysqld--initialize-insecure --user=mysql --datadir=/data/3308/data --basedir=/usr/local/mysql
mysqld--initialize-insecure --user=mysql --datadir=/data/3309/data --basedir=/usr/local/mysql
#授权
chown-R mysql.mysql /data/*cd/usr/lib/systemd/system/at>>mysqld3307.service<
Description=MySQL3307 Service
Documentation=man:mysqld(8)
Documentation=http://dev.mysql.com/doc/refman/en/using-systemd.html
After=network.target
After=syslog.target
[Install]
WantedBy=multi-user.target
[Service]
User=mysql
Group=mysql
Type=forking
PIDFile=/data/3307/data/mysql.pid
# Disable service startandstop timeout logic of systemd for mysqld service.
TimeoutSec=0# Execute preandpost scripts as root
PermissionsStartOnly=true# Needed to create system tables
#ExecStartPre=/usr/bin/mysqld_pre_systemd
# Start main service
ExecStart=/usr/local/mysql/bin/mysqld --defaults-file=/data/3307/my.cnf --daemonize --pid-file=/data/3307/data/mysql.pid $MYSQLD_OPTS
# Use this to switch malloc implementation
EnvironmentFile=-/etc/sysconfig/mysql
# Sets open_files_limit
LimitNOFILE= 5000Restart=on-failure
RestartPreventExitStatus=1EOF
#复制配置文件
cp mysqld3307.service mysqld3308.service
cp mysqld3307.service mysqld3309.service
#修改地址
sed-i 's#3307#3308#g'mysqld3308.service
sed-i 's#3307#3309#g'mysqld3309.service
#重新加载服务配置文件
systemctl daemon-reload
#启动服务
systemctl start mysqld3307.service
systemctl start mysqld3308.service
systemctl start mysqld3309.service
#开机启动
systemctl enable mysqld3307.service
systemctl enable mysqld3308.service
systemctl enable mysqld3309.service
验证多实例
netstat-lnp|grep 330mysql-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"
netstat-lnp|grep 330#查看服务是否启动
ps-ef | grep mysql #查看MySQL进程
ps-axo cmd | grep mysql #查看MySQL进程