1. 闲不住的人首页
  2. MySQL

MySQL5.7 基于通用二进制包安装多实例

1. 创建 mysql 用户

useradd -r -s /sbin/nologin mysql

2. 安装 mysql

cd /usr/local/src/
tar xf mysql-5.7.27-linux-glibc2.12-x86_64.tar.gz -C /usr/local/
cd /usr/local/
ln -sv mysql-5.7.27-linux-glibc2.12-x86_64/ mysql
cd mysql
chown -R root.mysql ./*

3. 创建数据目录和日志目录

mkdir -pv /data/mysql/{3306,3307,logs/{3306,3307}}

修改数据目录的属主及属组

chown -R mysql.mysql /data/mysql/

4. 配置 MySQL 的配置文件/etc/my.cnf

[mysqld_multi]
mysqld = /usr/local/mysql/bin/mysqld
mysqladmin = /usr/local/mysql/bin/mysqladmin
log = /tmp/mysql_multi.log
 
[mysqld3306]
datadir = /data/mysql/3306
socket = /tmp/mysql_3306.sock
pid-file = /data/mysql/3306/mysql_3306.pid
port = 3306
user = mysql
performance_schema = off
bind_address = 0.0.0.0
skip-name-resolve = 0
log-bin=mysql-bin
binlog_format=mixed
server-id=1
innodb_buffer_pool_size = 128M
innodb_log_file_size = 128M
innodb_file_per_table = 1
innodb_flush_log_at_trx_commit = 0
log-error = /data/mysql/logs/3306/mysql_error.log
log-bin = /data/mysql/logs/3306/mysql_bin.log
slow_query_log = 1
slow_query_log_file = /data/mysql/logs/3306/mysql_slow_query.log
long_query_time = 5


[mysqld3307]
datadir = /data/mysql/3307
socket = /tmp/mysql_3307.sock
pid-file = /data/mysql/3307/mysql_3307.pid
port = 3307
user = mysql
performance_schema = off
bind_address = 0.0.0.0
skip-name-resolve = 0
log-bin=mysql-bin
binlog_format=mixed
server-id=3
innodb_buffer_pool_size = 128M
innodb_log_file_size = 128M
innodb_file_per_table = 1
innodb_flush_log_at_trx_commit = 0
log-error = /data/mysql/logs/3307/mysql_error.log
log-bin = /data/mysql/logs/3307/mysql_bin.log
slow_query_log = 1
slow_query_log_file = /data/mysql/logs/3307/mysql_slow_query.log
long_query_time = 5

5. 初始化各个实例 root 密码为空

/usr/local/mysql/bin/mysqld --initialize-insecure --user=mysql --basedir=/usr/local/mysql --datadir=/data/mysql/3306/
/usr/local/mysql/bin/mysqld --initialize-insecure --user=mysql --basedir=/usr/local/mysql --datadir=/data/mysql/3307/

6. 开启实例的 SSL 连接

/usr/local/mysql/bin/mysql_ssl_rsa_setup --user=mysql --basedir=/usr/local/mysql --datadir=/data/mysql/3306
/usr/local/mysql/bin/mysql_ssl_rsa_setup --user=mysql --basedir=/usr/local/mysql --datadir=/data/mysql/3307

7. 复制多实例脚本到服务管理目录下

cp  /usr/local/mysql/support-files/mysqld_multi.server  /etc/init.d/mysqld_multi
chmod +x /etc/init.d/mysqld_multi
chkconfig --add mysqld_multi

8. 添加环境变量

echo '/usr/local/mysql/bin:$PATH' > /etc/profile.d/mysql.sh
source /etc/profile.d/mysql.sh

9. 测试示例

9.1. 查看实例状态

$ /etc/init.d/mysqld_multi report
Reporting MySQL servers
MySQL server from group: mysqld3306 is not running
MySQL server from group: mysqld3307 is not running

9.2. 启动所有实例

# /etc/init.d/mysqld_multi start
# mysqld_multi report           
Reporting MySQL servers
MySQL server from group: mysqld3306 is running
MySQL server from group: mysqld3307 is running

启动单个实例或者停止单个实例可以使用 /etc/init.d/mysqld_multi start 3306 或者 /etc/init.d/mysqld_multi stop 3306

原创文章,作者:恩志,如若转载,请注明出处:https://www.xbzdr.com/712.html

发表评论

电子邮件地址不会被公开。 必填项已用*标注

联系我们

在线咨询:点击这里给我发消息

邮件:510749025@qq.com

工作时间:周一至周五,9:30-18:30,节假日休息

QR code