MySQL进阶(多实例部署)

MySQL多实例部署

1.软件下载

[root@localhost ~]# cd /usr/src/
[root@localhost src]# wget https://downloads.mysql.com/archives/get/p/23/file/mysql-5.7.38-linux-glibc2.12-x86_64.tar.gz
--2022-07-31 12:25:27--  https://downloads.mysql.com/archives/get/p/23/file/mysql-5.7.38-linux-glibc2.12-x86_64.tar.gz
Resolving downloads.mysql.com (downloads.mysql.com)... 23.2.135.207, 2600:140b:2:99c::2e31, 2600:140b:2:99d::2e31
Connecting to downloads.mysql.com (downloads.mysql.com)|23.2.135.207|:443... connected.
HTTP request sent, awaiting response... 302 Moved Temporarily
Location: https://cdn.mysql.com/archives/mysql-5.7/mysql-5.7.38-linux-glibc2.12-x86_64.tar.gz [following]
--2022-07-31 12:25:28--  https://cdn.mysql.com/archives/mysql-5.7/mysql-5.7.38-linux-glibc2.12-x86_64.tar.gz
Resolving cdn.mysql.com (cdn.mysql.com)... 23.78.91.208
Connecting to cdn.mysql.com (cdn.mysql.com)|23.78.91.208|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 674830866 (644M) [application/x-tar-gz]
Saving to: 'mysql-5.7.38-linux-glibc2.12-x86_64.tar.gz'

mysql-5.7.38-linux-glibc 100%[===============================>] 643.57M  1.33MB/s    in 11m 18s 

2022-07-31 12:36:47 (971 KB/s) - 'mysql-5.7.38-linux-glibc2.12-x86_64.tar.gz' saved [674830866/674830866]
[root@localhost src]# ls
debug  kernels  mysql-5.7.38-linux-glibc2.12-x86_64.tar.gz

2.配置用户&组,解压二进制程序

[root@localhost src]# useradd -M -r -s /sbin/nologin mysql
[root@localhost src]# id mysql
uid=994(mysql) gid=991(mysql) groups=991(mysql)
[root@localhost src]# tar xf mysql-5.7.38-linux-glibc2.12-x86_64.tar.gz -C /usr/local/
[root@localhost src]# cd
[root@localhost ~]# ls /usr/local/
bin  games    lib    libexec                              sbin   src
etc  include  lib64  mysql-5.7.38-linux-glibc2.12-x86_64  share
[root@localhost ~]# cd /usr/local/
[root@localhost local]# ln -sv mysql-5.7.38-linux-glibc2.12-x86_64 mysql
'mysql' -> 'mysql-5.7.38-linux-glibc2.12-x86_64'
[root@localhost local]# ll
total 0
drwxr-xr-x. 2 root root   6 Jun 22  2021 bin
drwxr-xr-x. 2 root root   6 Jun 22  2021 etc
drwxr-xr-x. 2 root root   6 Jun 22  2021 games
drwxr-xr-x. 2 root root   6 Jun 22  2021 include
drwxr-xr-x. 2 root root   6 Jun 22  2021 lib
drwxr-xr-x. 3 root root  17 Jul 19 14:22 lib64
drwxr-xr-x. 2 root root   6 Jun 22  2021 libexec
lrwxrwxrwx. 1 root root  35 Jul 31 13:20 mysql -> mysql-5.7.38-linux-glibc2.12-x86_64
drwxr-xr-x. 9 root root 129 Jul 31 13:18 mysql-5.7.38-linux-glibc2.12-x86_64
drwxr-xr-x. 2 root root   6 Jun 22  2021 sbin
drwxr-xr-x. 5 root root  49 Jul 19 14:22 share
drwxr-xr-x. 2 root root   6 Jun 22  2021 src

//修改目录/usr/local/mysql的属主属组
[root@localhost local]# chown -R mysql.mysql mysql*
[root@localhost local]# ll /usr/local/mysql -d
lrwxrwxrwx. 1 mysql mysql 35 Jul 31 13:20 /usr/local/mysql -> mysql-5.7.38-linux-glibc2.12-x86_64


//配置环境变量
[root@localhost local]# echo 'export PATH=$PATH:/usr/local/mysql/bin' > /etc/profile.d/mysql.sh
[root@localhost local]# source /etc/profile.d/mysql.sh 
[root@localhost local]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/usr/local/mysql/bin

//创建头文件
[root@localhost local]# cd /usr/local/mysql
[root@localhost mysql]# ls
LICENSE  README  bin  docs  include  lib  man  share  support-files

[root@localhost mysql]# ln -sv /usr/local/mysql/include/ /usr/include/mysql
'/usr/include/mysql' -> '/usr/local/mysql/include/'
[root@localhost mysql]# chown -R mysql.mysql /usr/include/mysql
[root@localhost mysql]# ll -d /usr/include/mysql
lrwxrwxrwx. 1 mysql mysql 25 Jul 31 13:25 /usr/include/mysql -> /usr/local/mysql/include/

[root@localhost ~]# vim /etc/ld.so.conf.d/mysql.conf
/usr/local/mysql/lib/
[root@localhost ~]# ldconfig
[root@localhost ~]# vim /etc/man_db.conf 
#MANDATORY_MANPATH                      /usr/src/pvm3/man
#
MANDATORY_MANPATH                       /usr/man
MANDATORY_MANPATH                       /usr/share/man
MANDATORY_MANPATH                       /usr/local/share/man
MANDATORY_MANPATH                       /usr/local/mysql/man  //在前一行后面添加此行

3.创建各实例数据存放的目录

[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 Jul 31 13:30 3306
drwxr-xr-x. 2 mysql mysql 6 Jul 31 13:30 3307
drwxr-xr-x. 2 mysql mysql 6 Jul 31 13:30 3308
[root@localhost ~]# tree /opt/data/
/opt/data/
|-- 3306
|-- 3307
`-- 3308

3 directories, 0 files

4.初始化示例

//3306:
[root@localhost ~]# mysqld --initialize --user mysql --datadir /opt/data/3306
2022-07-31T05:33:50.861240Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2022-07-31T05:33:51.136267Z 0 [Warning] InnoDB: New log files created, LSN=45790
2022-07-31T05:33:51.232923Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2022-07-31T05:33:51.246349Z 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: 5bf5e024-1092-11ed-aa1a-000c2939ef7e.
2022-07-31T05:33:51.247152Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2022-07-31T05:33:51.439341Z 0 [Warning] A deprecated TLS version TLSv1 is enabled. Please use TLSv1.2 or higher.
2022-07-31T05:33:51.439384Z 0 [Warning] A deprecated TLS version TLSv1.1 is enabled. Please use TLSv1.2 or higher.
2022-07-31T05:33:51.439731Z 0 [Warning] CA certificate ca.pem is self signed.
2022-07-31T05:33:51.569766Z 1 [Note] A temporary password is generated for root@localhost: J1gfquM7TO-L
[root@localhost ~]# echo 'J1gfquM7TO-L' > 3306
[root@localhost ~]# ls
3306  anaconda-ks.cfg

//3307:
[root@localhost ~]# mysqld --initialize --user mysql --datadir /opt/data/3307
2022-07-31T05:35:56.454739Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2022-07-31T05:35:56.950322Z 0 [Warning] InnoDB: New log files created, LSN=45790
2022-07-31T05:35:57.055888Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2022-07-31T05:35:57.069465Z 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: a6f4f5b2-1092-11ed-b570-000c2939ef7e.
2022-07-31T05:35:57.071681Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2022-07-31T05:35:57.215920Z 0 [Warning] A deprecated TLS version TLSv1 is enabled. Please use TLSv1.2 or higher.
2022-07-31T05:35:57.215972Z 0 [Warning] A deprecated TLS version TLSv1.1 is enabled. Please use TLSv1.2 or higher.
2022-07-31T05:35:57.216383Z 0 [Warning] CA certificate ca.pem is self signed.
2022-07-31T05:35:57.244414Z 1 [Note] A temporary password is generated for root@localhost: DRquWwW1mW!&
[root@localhost ~]# echo 'DRquWwW1mW!&' > 3307
[root@localhost ~]# ls
3306  3307  anaconda-ks.cfg

//3308:
[root@localhost ~]# mysqld --initialize --user mysql --datadir /opt/data/3308
2022-07-31T05:37:20.519694Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2022-07-31T05:37:21.052193Z 0 [Warning] InnoDB: New log files created, LSN=45790
2022-07-31T05:37:21.152296Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2022-07-31T05:37:21.164130Z 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: d914c986-1092-11ed-a72f-000c2939ef7e.
2022-07-31T05:37:21.165048Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2022-07-31T05:37:21.521412Z 0 [Warning] A deprecated TLS version TLSv1 is enabled. Please use TLSv1.2 or higher.
2022-07-31T05:37:21.521472Z 0 [Warning] A deprecated TLS version TLSv1.1 is enabled. Please use TLSv1.2 or higher.
2022-07-31T05:37:21.521857Z 0 [Warning] CA certificate ca.pem is self signed.
2022-07-31T05:37:21.647704Z 1 [Note] A temporary password is generated for root@localhost: fsHA=urhD4-r
[root@localhost ~]# echo 'fsHA=urhD4-r' >3308
[root@localhost ~]# ls
3306  3307  3308  anaconda-ks.cfg

5.配置配置文件

[root@localhost ~]# vim /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

6.启动各示例

[root@localhost ~]# mysqld_multi start 3306
-bash: /usr/local/mysql/bin/mysqld_multi: /usr/bin/perl: bad interpreter: No such file or directory				
[root@localhost ~]# dnf -y install perl
[root@localhost mysql]# yum install ncurses-compat-libs
[root@localhost ~]# file /usr/local/mysql/bin/mysqld_multi
/usr/local/mysql/bin/mysqld_multi: Perl script text executable

[root@localhost ~]# mysqld_multi start 3306
perl: warning: Setting locale failed.
perl: warning: Please check that your locale settings:
        LANGUAGE = (unset),
        LC_ALL = (unset),
        LANG = "zh_CN.UTF-8"
    are supported and installed on your system.
perl: warning: Falling back to the standard locale ("C").

[root@localhost ~]# mysqld_multi start 3307
perl: warning: Setting locale failed.
perl: warning: Please check that your locale settings:
        LANGUAGE = (unset),
        LC_ALL = (unset),
        LANG = "zh_CN.UTF-8"
    are supported and installed on your system.
perl: warning: Falling back to the standard locale ("C").

[root@localhost ~]# mysqld_multi start 3308
perl: warning: Setting locale failed.
perl: warning: Please check that your locale settings:
        LANGUAGE = (unset),
        LC_ALL = (unset),
        LANG = "zh_CN.UTF-8"
    are supported and installed on your system.
perl: warning: Falling back to the standard locale ("C").

[root@localhost ~]# ss -amlt
State      Recv-Q     Send-Q         Local Address:Port                    Peer Address:Port     Process                                                                                          
LISTEN     0          128                  0.0.0.0:ssh                          0.0.0.0:*        
         skmem:(r0,rb87380,t0,tb16384,f0,w0,o0,bl0,d0)                                                 
LISTEN     0          80                         *:opsession-prxy                     *:*        
         skmem:(r0,rb87380,t0,tb16384,f0,w0,o0,bl0,d0)                                                 
LISTEN     0          80                         *:tns-server                         *:*        
         skmem:(r0,rb87380,t0,tb16384,f0,w0,o0,bl0,d0)                                                 
LISTEN     0          128                     [::]:ssh                             [::]:*        
         skmem:(r0,rb87380,t0,tb16384,f0,w0,o0,bl0,d0)                                                 
LISTEN     0          80                         *:mysql                              *:*        
         skmem:(r0,rb87380,t0,tb16384,f0,w0,o0,bl0,d0) 

7.使用system控制启动

[root@localhost ~]# ps -ef | grep 3306 | grep -v grep
root      278753       1  0 13:42 pts/0    00:00:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/opt/data/3306 --port=3306 --socket=/tmp/mysql3306.sock --pid-file=/opt/data/3306/mysql_3306.pid --log-error=/var/log/3306.log
mysql     278916  278753  0 13:42 pts/0    00:00:00 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/opt/data/3306 --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=/var/log/3306.log --pid-file=/opt/data/3306/mysql_3306.pid --socket=/tmp/mysql3306.sock --port=3306
[root@localhost ~]# ps -ef | grep 3306 | grep -v grep | awk '{print$2}'
278753
278916
[root@localhost ~]# ps -ef | grep 3306 | grep -v grep | awk '{print$2}' | xargs kill -9
[root@localhost ~]# ss -anlt
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          80                         *:3307                    *:*                   
LISTEN     0          80                         *:3308                    *:*                   
LISTEN     0          128                     [::]:22                   [::]:* 

[root@localhost ~]# ps -ef | grep 3307 | grep -v grep | awk '{print$2}' | xargs kill -9
[root@localhost ~]# ss -anlt
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          80                         *:3308                    *:*                   
LISTEN     0          128                     [::]:22                   [::]:* 

[root@localhost ~]# ps -ef | grep 3308 | grep -v grep | awk '{print$2}' | xargs kill -9
[root@localhost ~]# ss -anlt
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                   [::]:* 

//配置3306
[root@localhost ~]# vim /usr/lib/systemd/system/3306.service
[root@localhost ~]# cat /usr/lib/systemd/system/3306.service
[Unit]
Description=3306 server daemon
After=network.target sshd-keygen.target

[Service]
Type=forking
ExecStart=/usr/local/mysql/bin/mysqld_multi start 3306
ExecStop=ps -ef | grep 3306 | grep -v grep | awk '{print$2}' | xargs kill -9
ExecReload=/bin/kill -HUP $MAINPID

[Install]
WantedBy=multi-user.target

//配置3307
[root@localhost ~]# cp /usr/lib/systemd/system/3306.service /usr/lib/systemd/system/3307.service
[root@localhost ~]# vim /usr/lib/systemd/system/3307.service 
[root@localhost ~]# cat /usr/lib/systemd/system/3307.service 
[Unit]
Description=3307 server daemon
After=network.target sshd-keygen.target

[Service]
Type=forking
ExecStart=/usr/local/mysql/bin/mysqld_multi start 3307
ExecStop=ps -ef | grep 3307 | grep -v grep | awk '{print$2}' | xargs kill -9
ExecReload=/bin/kill -HUP $MAINPID

[Install]
WantedBy=multi-user.target

//配置3308
[root@localhost ~]# cp /usr/lib/systemd/system/3306.service /usr/lib/systemd/system/3308.service
[root@localhost ~]# vim /usr/lib/systemd/system/3308.service
[root@localhost ~]# cat /usr/lib/systemd/system/3308.service
[Unit]
Description=3308 server daemon
After=network.target sshd-keygen.target

[Service]
Type=forking
ExecStart=/usr/local/mysql/bin/mysqld_multi start 3308
ExecStop=ps -ef | grep 3308 | grep -v grep | awk '{print$2}' | xargs kill -9
ExecReload=/bin/kill -HUP $MAINPID

[Install]
WantedBy=multi-user.target

//刷新后关闭防火墙
[root@localhost ~]# systemctl daemon-reload
[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# systemctl disable firewalld
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.

//启动3306,3307,3308
[root@localhost ~]# systemctl start 3306
Job for 3306.service failed because the control process exited with error code.
See "systemctl status 3306.service" and "journalctl -xe" for details.

//创建一个软链接到/usr/bin下
[root@localhost ~]# ln -s /usr/local/mysql/bin/my_print_defaults /usr/bin/my_print_defaults
[root@localhost ~]# systemctl start 3306
[root@localhost ~]# systemctl start 3307
[root@localhost ~]# systemctl start 3308
[root@localhost ~]# ss -anlt
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          80                         *:3307                    *:*                   
LISTEN     0          80                         *:3308                    *:*                   
LISTEN     0          128                     [::]:22                   [::]:*                   
LISTEN     0          80                         *:3306                    *:* 

8.初始化密码

//3306
[root@localhost ~]mysql -uroot -p'J1gfquM7TO-L' -S /tmp/mysql3306.sock
mysql> set password = password('123456');
Query OK, 0 rows affected, 1 warning (0.00 sec)

//3307
[root@localhost ~]# mysql -uroot -p'DRquWwW1mW!&' -S /tmp/mysql3307.sock
mysql> set password = password('123456');
Query OK, 0 rows affected, 1 warning (0.00 sec)

//3308
[root@localhost ~]# mysql -uroot -p'fsHA=urhD4-r' -S /tmp/mysql3308.sock
mysql> set password = password('123456');
Query OK, 0 rows affected, 1 warning (0.00 sec)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值