mysql多实例部署

mysql实例部署安装

多实例部署最多应用在现实中只有一台服务器,但是想用多个DBA,这个时候就可以用到多实例部署。

mysql多实例部署是在一台服务器上开启多个不同的服务端口用来控制数据库

首先应该有mysql,这里安装一个二进制格式的mysql

[root@localhost ~]# ls
公共  图片  音乐             initial-setup-ks.cfg
模板  文档  桌面             mysql-5.7.33-linux-glibc2.12-x86_64.tar.gz
视频  下载  anaconda-ks.cfg
[root@localhost ~]# useradd -r -M -s /sbin/nologin mysql
[root@localhost ~]# id mysql
uid=974(mysql) gid=973(mysql)=973(mysql)
[root@localhost ~]# tar xf mysql-5.7.33-linux-glibc2.12-x86_64.tar.gz -C /usr/local/
[root@localhost ~]# cd /usr/local/
[root@localhost local]# ls
bin  games    lib    libexec                              sbin   src
etc  include  lib64  mysql-5.7.33-linux-glibc2.12-x86_64  share
[root@localhost local]# ln -s mysql-5.7.33-linux-glibc2.12-x86_64 mysql
[root@localhost local]# ll
总用量 0
drwxr-xr-x. 2 root root   6 518 2020 bin
drwxr-xr-x. 2 root root   6 518 2020 etc
drwxr-xr-x. 2 root root   6 518 2020 games
drwxr-xr-x. 2 root root   6 518 2020 include
drwxr-xr-x. 2 root root   6 518 2020 lib
drwxr-xr-x. 3 root root  17 53 15:12 lib64
drwxr-xr-x. 2 root root   6 518 2020 libexec
lrwxrwxrwx. 1 root root  35 510 06:44 mysql -> mysql-5.7.33-linux-glibc2.12-x86_64
drwxr-xr-x. 9 root root 129 510 06:43 mysql-5.7.33-linux-glibc2.12-x86_64
drwxr-xr-x. 2 root root   6 518 2020 sbin
drwxr-xr-x. 5 root root  49 53 15:12 share
drwxr-xr-x. 2 root root   6 518 2020 src

更改属主组

[root@localhost local]# chown -R mysql.mysql mysql
[root@localhost local]# ll
总用量 0
drwxr-xr-x. 2 root  root    6 518 2020 bin
drwxr-xr-x. 2 root  root    6 518 2020 etc
drwxr-xr-x. 2 root  root    6 518 2020 games
drwxr-xr-x. 2 root  root    6 518 2020 include
drwxr-xr-x. 2 root  root    6 518 2020 lib
drwxr-xr-x. 3 root  root   17 53 15:12 lib64
drwxr-xr-x. 2 root  root    6 518 2020 libexec
lrwxrwxrwx. 1 mysql mysql  35 510 06:44 mysql -> mysql-5.7.33-linux-glibc2.12-x86_64
drwxr-xr-x. 9 mysql mysql 129 510 06:43 mysql-5.7.33-linux-glibc2.12-x86_64
drwxr-xr-x. 2 root  root    6 518 2020 sbin
drwxr-xr-x. 5 root  root   49 53 15:12 share
drwxr-xr-x. 2 root  root    6 518 2020 src

做一个环境变量

[root@localhost ~]# echo 'export PATH=/usr/local/mysql/bin:$PATH' > /etc/profile.d/mysql.sh
[root@localhost ~]# source /etc/profile.d/mysql.sh
[root@localhost ~]# which mysql
/usr/local/mysql/bin/mysql

创建多个实例目录,这里用端口号表示

[root@localhost ~]# mkdir -p /opt/data/{3306,3307,3308}
[root@localhost ~]# tree /opt/data
/opt/data
├── 3306
├── 3307
└── 3308

3 directories, 0 files
[root@localhost ~]# chown -R mysql.mysql /opt/data
[root@localhost ~]# ll /opt/data
总用量 0
drwxr-xr-x. 2 mysql mysql 6 510 06:54 3306
drwxr-xr-x. 2 mysql mysql 6 510 06:54 3307
drwxr-xr-x. 2 mysql mysql 6 510 06:54 3308

初始化

[root@localhost ~]# mysqld --initialize --user mysql --datadir /opt/data/3306
2021-05-10T10:58:49.391420Z 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:58:49.608833Z 0 [Warning] InnoDB: New log files created, LSN=45790
2021-05-10T10:58:49.642431Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2021-05-10T10:58:49.649239Z 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: b343cae4-b17e-11eb-b39c-000c29486c24.
2021-05-10T10:58:49.649884Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2021-05-10T10:58:50.386596Z 0 [Warning] CA certificate ca.pem is self signed.
2021-05-10T10:58:50.425187Z 1 [Note] A temporary password is generated for root@localhost: *JSU?ZHYj10v

把密码记下来
[root@localhost ~]# echo '*JSU?ZHYj10v' >3306


[root@localhost ~]# mysqld --initialize --user mysql --datadir /opt/data/3307
2021-05-10T11:00:09.839511Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2021-05-10T11:00:10.034725Z 0 [Warning] InnoDB: New log files created, LSN=45790
2021-05-10T11:00:10.063812Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2021-05-10T11:00:10.070422Z 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: e33315a7-b17e-11eb-9855-000c29486c24.
2021-05-10T11:00:10.072074Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2021-05-10T11:00:10.532562Z 0 [Warning] CA certificate ca.pem is self signed.
2021-05-10T11:00:10.598065Z 1 [Note] A temporary password is generated for root@localhost: Rq%+6?amg?)M

[root@localhost ~]# echo 'Rq%+6?amg?)M' >3307


[root@localhost ~]# mysqld --initialize --user mysql --datadir /opt/data/3308
2021-05-10T11:01:14.087025Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2021-05-10T11:01:14.262218Z 0 [Warning] InnoDB: New log files created, LSN=45790
2021-05-10T11:01:14.297752Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2021-05-10T11:01:14.306961Z 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: 097ccf30-b17f-11eb-9bcc-000c29486c24.
2021-05-10T11:01:14.307700Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2021-05-10T11:01:15.619726Z 0 [Warning] CA certificate ca.pem is self signed.
2021-05-10T11:01:15.745869Z 1 [Note] A temporary password is generated for root@localhost: %iUL2gICSn#;
[root@localhost ~]# echo '%iUL2gICSn#;' >3308

安装perl 这里我有,就不需要安装了
如果需要安装的话dnf -y install perl
就用这个安装

[root@localhost ~]# which perl
/usr/bin/perl

添加配置文件

[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   //这里的3306必须是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
Wide character in print at /usr/local/mysql/bin/mysqld_multi line 678.
[root@localhost ~]# ss -antl
State    Recv-Q   Send-Q      Local Address:Port       Peer Address:Port   Process   
LISTEN   0        128               0.0.0.0:111             0.0.0.0:*                
LISTEN   0        32          192.168.122.1:53              0.0.0.0:*                
LISTEN   0        128               0.0.0.0:22              0.0.0.0:*                
LISTEN   0        5               127.0.0.1:631             0.0.0.0:*                
LISTEN   0        80                      *:3306                  *:*                
LISTEN   0        128                  [::]:111                [::]:*                
LISTEN   0        128                  [::]:22                 [::]:*                
LISTEN   0        5                   [::1]:631                [::]:*                
[root@localhost ~]# mysqld_multi start 3307
Wide character in print at /usr/local/mysql/bin/mysqld_multi line 678.
[root@localhost ~]# mysqld_multi start 3308
Wide character in print at /usr/local/mysql/bin/mysqld_multi line 678.
[root@localhost ~]# ss -antl
State    Recv-Q   Send-Q      Local Address:Port       Peer Address:Port   Process   
LISTEN   0        128               0.0.0.0:111             0.0.0.0:*                
LISTEN   0        32          192.168.122.1:53              0.0.0.0:*                
LISTEN   0        128               0.0.0.0:22              0.0.0.0:*                
LISTEN   0        5               127.0.0.1:631             0.0.0.0:*                
LISTEN   0        80                      *:3306                  *:*                
LISTEN   0        80                      *:3307                  *:*                
LISTEN   0        80                      *:3308                  *:*                
LISTEN   0        128                  [::]:111                [::]:*                
LISTEN   0        128                  [::]:22                 [::]:*                
LISTEN   0        5                   [::1]:631                [::]:*                

更改初始化密码

[root@localhost ~]# mysql -uroot -p'*JSU?ZHYj10v' -P3306
mysql: error while loading shared libraries: libncurses.so.5: cannot open shared object file: No such file or directory
[root@localhost ~]# dnf whatprovides libncurses.so.5
CentOS Stream 8 - AppStream                          389 kB/s | 8.5 MB     00:22    
CentOS Stream 8 - BaseOS                             568 kB/s | 2.7 MB     00:04    
CentOS Stream 8 - Extras                             9.1 kB/s | 9.3 kB     00:01    
ncurses-compat-libs-6.1-7.20180224.el8.i686 : Ncurses compatibility libraries
仓库        :baseos
匹配来源:
提供    : libncurses.so.5


[root@localhost ~]# dnf -y install ncurses-compat-libs

在这里需要安装一个这个包。

[root@localhost ~]# mysql -uroot -p'*JSU?ZHYj10v' -P3306
mysql: [Warning] Using a password on the command line interface can be insecure.
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)

这里显示配置文件找不到,所以就要用另一种方式登录

[root@localhost ~]# mysql -uroot -p'*JSU?ZHYj10v' -P3306 -h127.0.0.1
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> 

这样就可以连接上了

更改3306密码再次连接

mysql> set password = password('ZHANGde12+Jun');
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> quit
Bye
[root@localhost ~]# mysql -uroot -p'ZHANGde12+Jun' -P3306 -h127.0.0.1
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 3
Server version: 5.7.33 MySQL Community Server (GPL)

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> 

更改3307密码并连接

mysql> set password = password('ZHANGde12+Jun');
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> quit
Bye
[root@localhost ~]# mysql -uroot -p'ZHANGde12+Jun' -P3307 -h127.0.0.1
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 3
Server version: 5.7.33 MySQL Community Server (GPL)

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> 

更改3308密码并登录

mysql> set password = password('ZHANGde12+Jun');
Query OK, 0 rows affected, 1 warning (0.01 sec)

mysql> quit
Bye
[root@localhost ~]# mysql -uroot -p'ZHANGde12+Jun' -P3308 -h127.0.0.1
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 3
Server version: 5.7.33 MySQL Community Server (GPL)

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> 

设置开机自启

[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

注:该输出结果只显示 SysV 服务,并不包含
原生 systemd 服务。SysV 配置数据
可能被原生 systemd 配置覆盖。 

      要列出 systemd 服务,请执行 'systemctl list-unit-files'。
      查看在具体 target 启用的服务请执行
      'systemctl list-dependencies [target]'。

mysqld_multi.server     0:1:2:3:4:5:6:

开机自启

[root@localhost ~]# reboot

连接断开
连接成功
Activate the web console with: systemctl enable --now cockpit.socket

Last login: Mon May 10 07:30:05 2021
[root@localhost ~]# ss -antl
State    Recv-Q   Send-Q      Local Address:Port       Peer Address:Port   Process   
LISTEN   0        128               0.0.0.0:111             0.0.0.0:*                
LISTEN   0        32          192.168.122.1:53              0.0.0.0:*                
LISTEN   0        128               0.0.0.0:22              0.0.0.0:*                
LISTEN   0        5               127.0.0.1:631             0.0.0.0:*                
LISTEN   0        80                      *:3306                  *:*                
LISTEN   0        80                      *:3307                  *:*                
LISTEN   0        80                      *:3308                  *:*                
LISTEN   0        128                  [::]:111                [::]:*                
LISTEN   0        128                  [::]:22                 [::]:*                
LISTEN   0        5                   [::1]:631                [::]:* 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值