MySQL数据库 多实例部署
前言
本环境是基于 Centos 7.8 系统构建MySQL-5.7.14
具体构建,请参考 MySQL-5.7.14 环境构建
更多情况下,我们将多个应用的数据库部署到同一台服务器上,但是如果部署到同一个数据库服务上,后面的产品跟新、升级、维护带来诸多不便:多个数据库服务之间相互影响。因此,MySQL 多实例部署解决了这个问题。使得多个应用数据库服务之间独立运行。保证了线上业务的高效、稳定、安全运行。
环境要求:mysql-5.7.14 通用二进制或者源码部署
一、mysqld 单独配置文件部署
注:本次部署基于mysql源码环境安装
多实例部署两台mysql服务
端口分别为3306、3307
数据库目录:/mysql/330{6,7}/date
公用basedir目录:/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
登录数据库
--- 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>
停止多实例数据库
[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
二、mysqld 共用配置文件部署
# 禁用运行中的数据库
[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
登录数据库
--- 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>
停止多实例数据库
[root@mysql-source_code ~]# mysqld_multi stop 3306,3307