msyql多实例部署
51排版太垃圾了了,csdn的博客
https://blog.csdn.net/qq_36023121/article/details/116613762
什么是多实例?(what where why)
what:在linux系统中代表:多个进程+多个线程+多个预分配内存结构,简单地说,Mysql多实例就是在一台服务器上同时开启多个不同的服务端口(3306、3307),同时运行多个Mysql服务进程,这些服务进程通过不同的socket监听不同的服务端口来提供服务。
where:一般用来测试环境中,测试主从,高可用等。
why:希望不同业务的数据库服务各自尽量独立的提供服务且不受影响,同时还需要主从复制等技术提供备份和读写分离服务
创建各实例数据存放的目录
[root@localhost ~]# mkdir -p /opt/data/{3306,3307,3308}
[root@localhost ~]# chown -R mysql.mysql /opt/data/
[root@localhost ~]# ll /opt/data/
total 0
drwxr-xr-x. 2 mysql mysql 6 May 10 18:07 3306
drwxr-xr-x. 2 mysql mysql 6 May 10 18:07 3307
drwxr-xr-x. 2 mysql mysql 6 May 10 18:07 3308
初始化各实例
[root@localhost ~]# mysqld --initialize --datadir=/opt/data/3306 --user=mysql
2021-05-10T10:08:41.281132Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2021-05-10T10:08:41.436326Z 0 [Warning] InnoDB: New log files created, LSN=45790
2021-05-10T10:08:41.456677Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2021-05-10T10:08:41.524925Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: b24872dd-b177-11eb-aa74-000c298fedd0.
2021-05-10T10:08:41.526972Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2021-05-10T10:08:41.977982Z 0 [Warning] CA certificate ca.pem is self signed.
2021-05-10T10:08:42.288878Z 1 [Note] A temporary password is generated for root@localhost: dUe47afbhE+h
[root@localhost ~]# mysqld --initialize --datadir=/opt/data/3307 --user=mysql
2021-05-10T10:09:08.225394Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2021-05-10T10:09:08.351809Z 0 [Warning] InnoDB: New log files created, LSN=45790
2021-05-10T10:09:08.374154Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2021-05-10T10:09:08.429753Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: c251cd02-b177-11eb-ab6f-000c298fedd0.
2021-05-10T10:09:08.433891Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2021-05-10T10:09:08.832259Z 0 [Warning] CA certificate ca.pem is self signed.
2021-05-10T10:09:09.098486Z 1 [Note] A temporary password is generated for root@localhost: 9d<_))qLIBb9
[root@localhost ~]# mysqld --initialize --datadir=/opt/data/3308 --user=mysql
2021-05-10T10:09:36.057303Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2021-05-10T10:09:36.182805Z 0 [Warning] InnoDB: New log files created, LSN=45790
2021-05-10T10:09:36.204555Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2021-05-10T10:09:36.258840Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: d2e82eb2-b177-11eb-ad4c-000c298fedd0.
2021-05-10T10:09:36.259646Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2021-05-10T10:09:37.092331Z 0 [Warning] CA certificate ca.pem is self signed.
2021-05-10T10:09:37.211332Z 1 [Note] A temporary password is generated for root@localhost: J*9t)c-K#CUq
yum 安装perl
[root@localhost ~]# yum -y install perl
配置配置文件/etc/my.cnf
[root@localhost ~]# cat /etc/my.cnf
[mysqld_multi]
mysqld = /usr/local/mysql/bin/mysqld_safe
mysqladmin = /usr/local/mysql/bin/mysqladmin
[mysqld3306]
datadir = /opt/data/3306
port = 3306
socket = /tmp/mysql3306.sock
pid-file = /opt/data/3306/mysql_3306.pid
log-error=/var/log/3306.log
[mysqld3307]
datadir = /opt/data/3307
port = 3307
socket = /tmp/mysql3307.sock
pid-file = /opt/data/3307/mysql_3307.pid
log-error=/var/log/3307.log
[mysqld3308]
datadir = /opt/data/3308
port = 3308
socket = /tmp/mysql3308.sock
pid-file = /opt/data/3308/mysql_3308.pid
log-error=/var/log/3308.log
启动各实例
[root@localhost ~]# mysqld_multi start 3306
[root@localhost ~]# mysqld_multi start 3307
[root@localhost ~]# mysqld_multi start 3308
[root@localhost ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 128 [::]:22 [::]:*
LISTEN 0 80 *:3306 *:*
LISTEN 0 80 *:3307 *:*
LISTEN 0 80 *:3308 *:*
安装libncurses
[root@localhost ~]# yum -y install libncurses*
初始化密码
[root@localhost ~]# mysql -uroot -p'dUe47afbhE+h' -S /tmp/mysql3306.sock
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.33
Copyright (c) 2000, 2021, Oracle and/or its affiliates.
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> set password = password('a123456');
Query OK, 0 rows affected, 1 warning (0.00 sec)
[root@localhost ~]# mysql -uroot -p'9d<_))qLIBb9' -S /tmp/mysql3307.sock
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.33
Copyright (c) 2000, 2021, Oracle and/or its affiliates.
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> set password = password('b123456');
Query OK, 0 rows affected, 1 warning (0.00 sec)
[root@localhost ~]# mysql -uroot -p'J*9t)c-K#CUq' -S /tmp/mysql3308.sock
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.33
Copyright (c) 2000, 2021, Oracle and/or its affiliates.
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> set password = password('c123456');
Query OK, 0 rows affected, 1 warning (0.00 sec)
多实例完成
[root@localhost ~]# ll /opt/data/
total 12
drwxr-xr-x. 5 mysql mysql 4096 May 10 18:14 3306
drwxr-xr-x. 5 mysql mysql 4096 May 10 18:14 3307
drwxr-xr-x. 5 mysql mysql 4096 May 10 18:14 3308
多实例配置文件位置
[root@localhost ~]# vim /usr/local/mysql/support-files/mysqld_multi.server
修改mysqld.service
[root@localhost ~]# cat mysqld.service
[Unit]
Description=MySQL Server
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
# Disable service start and stop timeout logic of systemd for mysqld service.
TimeoutSec=0
# Execute pre and post scripts as root
PermissionsStartOnly=true
# Start main service
ExecStart=/usr/local/mysql/bin/mysqld-multi start 3306 && /usr/local/mysql/bin/mysqld-multi start 3307 && /usr/local/mysql/bin/mysqld-multi start 3308
# Use this to switch malloc implementation
EnvironmentFile=-/etc/sysconfig/mysql
# Sets open_files_limit
LimitNOFILE = 5000
RestartPreventExitStatus=1
PrivateTmp=false
修改mysqld_multi.server
[root@localhost ~]# vim /etc/init.d/mysqld.multi
basedir=/usr/local/mysql
bindir=/usr/local/mysql/bin
export PATH=$bindir:$PATH
配置开机自启
[root@localhost ~]# cp /usr/local/mysql/support-files/mysqld_multi.server /etc/init.d/
[root@localhost ~]# vim /etc/init.d/mysqld_multi.server
basedir=/usr/local/mysql
bindir=/usr/local/mysql/bin
export PATH=/usr/local/mysql/bin:$PATH
[root@localhost ~]# chkconfig mysqld_multi.server on
[root@localhost ~]# chkconfig --list
Note: This output shows SysV services only and does not include native
systemd services. SysV configuration data might be overridden by native
systemd configuration.
If you want to list systemd services use 'systemctl list-unit-files'.
To see services enabled on particular target use
'systemctl list-dependencies [target]'.
mysqld_multi.server 0:off 1:off 2:on 3:on 4:on 5:on 6:off