MySQL 사례 전투 -MySQL 데이터베이스 다중 인스턴스 배포

머리말

이 환경은 Centos 7.8 시스템을 기반으로
특정 구성 을 위해 MySQL-5.7.14를 빌드 합니다. MySQL-5.7.14 환경 구성을 참조하십시오.

더 많은 경우에 동일한 서버에 여러 응용 프로그램 데이터베이스를 배포하지만 동일한 데이터베이스 서비스에 배포하는 경우 후속 제품 업데이트, 업그레이드 및 유지 관리는 많은 불편을 초래합니다. 따라서 MySQL 다중 인스턴스 배포는이 문제를 해결합니다. 여러 응용 프로그램 데이터베이스 서비스를 독립적으로 실행합니다. 온라인 비즈니스의 효율적이고 안정적이며 안전한 운영을 보장합니다.


환경 요구 사항 : mysql-5.7.14 범용 바이너리 또는 소스 코드 배포

하나의 mysqld 별도 구성 파일 배포

참고 :이 배포는 mysql 소스 환경 설치를 기반으로합니다.

2 개의 mysql 서비스
포트 의 다중 인스턴스 배포 는 3306, 3307
데이터베이스 디렉토리 : / mysql / 330 {6,7} / date
공용 기반 디렉토리 : / 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

2. 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

추천

출처blog.csdn.net/XY0918ZWQ/article/details/113664968