MySQL database multi-instance deployment
Preface
This environment is based on Centos 7.8 system to build MySQL-5.7.14 for
specific construction, please refer to MySQL-5.7.14 environment construction
In more cases, we deploy multiple application databases on the same server, but if they are deployed on the same database service, subsequent product updates, upgrades, and maintenance will cause many inconveniences: influences. Therefore, MySQL multi-instance deployment solves this problem. Makes multiple application database services run independently. Ensure the efficient, stable and safe operation of online business.
Environmental requirements: mysql-5.7.14 universal binary or source code deployment
One, mysqld separate configuration file deployment
Note: This deployment is based on the mysql source environment installation
Multi-instance deployment of two mysql service
ports respectively 3306, 3307
database directory: /mysql/330{6,7}/date
Public basedir directory: /usr/local/mysql
# 禁用数据库服务
[root@mysql-source_code ~]# systemctl stop mysqld
[root@mysql-source_code ~]# systemctl disable mysqld
# 创建多实例服务目录
[root@mysql-source_code ~]# mkdir /mysql/330{6,7}/data -p
# 分别提供配置文件
[root@mysql-source_code ~]# vim /mysql/3306/my.cnf
[client]
port = 3306
socket = /mysql/3306/mysql.sock
[mysqld]
user = mysql
port = 3306
socket = /mysql/3306/mysql.sock
basedir = /usr/local/mysql
datadir = /mysql/3306/data
server-id = 5
[mysqldump]
quick
max_allowed_packet = 16M
[mysqld_safe]
log-error=/mysql/3306/mysql_3306.err
pid-file=/mysql/3306/mysqld.pid
[root@mysql-source_code ~]# vim /mysql/3307/my.cnf
[client]
port = 3307
socket = /mysql/3307/mysql.sock
[mysqld]
user = mysql
port = 3307
socket = /mysql/3307/mysql.sock
basedir = /usr/local/mysql
datadir = /mysql/3307/data
server-id = 6
[mysqldump]
quick
max_allowed_packet = 16M
[mysqld_safe]
log-error=/mysql/3307/mysql_3307.err
pid-file=/mysql/3307/mysqld.pid
# 修改权限
[root@mysql-source_code ~]# chown -R mysql.mysql /mysql
# 初始化mysqld服务
[root@mysql-source_code ~]# /usr/local/mysql/bin/mysqld --initialize --datadir=/mysql/3306/data \
> --basedir=/usr/local/mysql --user=mysql
[root@mysql-source_code ~]# /usr/local/mysql/bin/mysqld --initialize --datadir=/mysql/3307/data \
> --basedir=/usr/local/mysql --user=mysql
# 启动服务
[root@mysql-source_code ~]# mysqld_safe --defaults-file=/mysql/3306/my.cnf &
[root@mysql-source_code ~]# mysqld_safe --defaults-file=/mysql/3307/my.cnf &
# 查看mysqld服务端口、进程状况
[root@mysql-source_code ~]# netstat -lnutp | grep 330
tcp6 0 0 :::3306 :::* LISTEN 2087/mysqld
tcp6 0 0 :::3307 :::* LISTEN 2288/mysqld
[root@mysql-source_code ~]# ps aux | grep mysqld
root 1914 0.0 0.0 113416 1584 pts/0 S 21:59 0:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --defaults-file=/mysql/3306/my.cnf
mysql 2087 0.3 9.9 1833568 185824 pts/0 Sl 21:59 0:02 /usr/local/mysql/bin/mysqld --defaults-file=/mysql/3306/my.cnf --basedir=/usr/local/mysql --datadir=/mysql/3306/data --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=/mysql/3306/mysql_3306.err --pid-file=/mysql/3306/mysqld.pid --socket=/mysql/3306/mysql.sock --port=3306
root 2115 0.0 0.0 113416 1588 pts/0 S 22:00 0:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --defaults-file=/mysql/3307/my.cnf
mysql 2288 0.3 9.8 1833568 183540 pts/0 Sl 22:00 0:03 /usr/local/mysql/bin/mysqld --defaults-file=/mysql/3307/my.cnf --basedir=/usr/local/mysql --datadir=/mysql/3307/data --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=/mysql/3307/mysql_3307.err --pid-file=/mysql/3307/mysqld.pid --socket=/mysql/3307/mysql.sock --port=3307
root 2390 0.0 0.0 112812 972 pts/0 S+ 22:15 0:00 grep --color=auto mysqld
Log in to the database
--- 3306
[root@mysql-source_code ~]# mysql -uroot -p -S /mysql/3306/mysql.sock
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.14 Source distribution
Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
--- 3307
[root@mysql-source_code ~]# mysql -uroot -p -S /mysql/3307/mysql.sock
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.14
Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> alter user root@localhost identified by '123456';
Query OK, 0 rows affected (0.00 sec)
mysql>
Stop a multi-instance database
[root@mysql-source_code ~]# mysqladmin -uroot -p123456 -S /mysql/3306/mysql.sock shutdown
[root@mysql-source_code ~]# mysqladmin -uroot -p123456 -S /mysql/3307/mysql.sock shutdown
2. mysqld shared configuration file deployment
# 禁用运行中的数据库
[root@mysql-source_code ~]# mysqladmin -uroot -p123456 -S /mysql/3306/mysql.sock shutdown
[root@mysql-source_code ~]# mysqladmin -uroot -p123456 -S /mysql/3307/mysql.sock shutdown
# 备份my.cnf文件
[root@mysql-source_code ~]# cp /etc/my.cnf /etc/my.cnf.bak
# 提供配置文件
[root@localhost ~]# cp /etc/my.cnf /etc/my.cnf.bak
[root@localhost ~]# vim /etc/my.cnf
[mysqld_multi]
mysqld = /usr/local/mysql/bin/mysqld_safe
mysqladmin = /usr/local/mysql/bin/mysqladmin
user = root
pass = 123456
[mysqld3306]
socket = /mysql/3306/mysql.sock
port = 3306
pid-file = /mysql/3306/mysql.pid
datadir = /mysql/3306/data
basedir = /usr/local/mysql
[mysqld3307]
socket = /mysql/3307/mysql.sock
port = 3307
pid-file = /mysql/3307/mysql.pid
datadir = /mysql/3307/data
basedir = /usr/local/mysql
# 启动服务
[root@mysql-source_code ~]# mysqld_multi start 3306,3307
# 查看mysqld服务端口、进程状况
[root@mysql-source_code ~]# netstat -lnutp | grep 330
tcp6 0 0 :::3306 :::* LISTEN 2022/mysqld
tcp6 0 0 :::3307 :::* LISTEN 2223/mysqld
[root@mysql-source_code ~]# ps aux | grep mysql
root 1849 0.0 0.0 113416 1588 pts/0 S 23:12 0:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --defaults-file=/mysql/3306/my.cnf
mysql 2022 0.2 9.8 1833568 184216 pts/0 Sl 23:12 0:02 /usr/local/mysql/bin/mysqld --defaults-file=/mysql/3306/my.cnf --basedir=/usr/local/mysql --datadir=/mysql/3306/data --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=/mysql/3306/mysql_3306.err --pid-file=/mysql/3306/mysqld.pid --socket=/mysql/3306/mysql.sock --port=3306
root 2050 0.0 0.0 113416 1588 pts/0 S 23:12 0:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --defaults-file=/mysql/3307/my.cnf
mysql 2223 0.4 9.9 1833568 185988 pts/0 Sl 23:12 0:03 /usr/local/mysql/bin/mysqld --defaults-file=/mysql/3307/my.cnf --basedir=/usr/local/mysql --datadir=/mysql/3307/data --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=/mysql/3307/mysql_3307.err --pid-file=/mysql/3307/mysqld.pid --socket=/mysql/3307/mysql.sock --port=3307
root 2276 0.0 0.1 133800 2516 pts/1 S+ 23:13 0:00 mysql -uroot -p -S /mysql/3307/mysql.sock
root 2470 0.0 0.0 112812 972 pts/0 S+ 23:25 0:00 grep --color=auto mysql
Log in to the database
--- 3306
[root@mysql-source_code ~]# mysql -uroot -p -S /mysql/3306/mysql.sock
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.14 Source distribution
Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
--- 3306
[root@mysql-source_code ~]# mysql -uroot -p -S /mysql/3307/mysql.sock
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.14 Source distribution
Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
Stop a multi-instance database
[root@mysql-source_code ~]# mysqld_multi stop 3306,3307