mysql多实例部署

Posted 卑微小胡

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mysql多实例部署相关的知识,希望对你有一定的参考价值。

mysql多实例部署

什么是MySQL多实例?

简单地说,MySQL多实例就是在一台服务器上同时开启多个不同的服务端口(如:3306,3307),同时运行多个MySQL服务进程,这些服务进程通过不同的socket监听不同的服务端口来提供服务。

这些MySQL多实例共用一套MySQL安装程序,使用不同的my.cnf(也可以相同)配置文件、启动程序(也可以相同)和数据文件。在提供服务时,多实例MySQL在逻辑上看来是各自独立的,它们根据配置文件的对应设定值,获得服务器相应数量的硬件资源。

打个比方吧,MySQL多实例就相当于房子的多个卧室,每个实例可以看作一间卧室,整个服务器就是一套房子,服务器的硬件资源(cpu,men,disk)、软件资源(CentOS操作系统)可以看作房子的卫生间、厨房、客厅,是房子的共用资源。若你是北漂的小伙伴,与朋友一起租房子,相信更好理解,大家蜗居在一起,休息在自己的卧室,出来活动肯定是要共用上述公共资源。这样就可以很好的理解MySQL多实例了。

MySQL多实例的优点和缺点

  • 有效利用服务器资源

当单个服务器资源有剩余时,可以充分利用剩余的资源提供更多的服务,且可以实现资源的逻辑隔离。

  • 节约服务器资源

当公司资金紧张,但是数据库又需要各自尽量独立地提供服务,而且,需要主从复制等技术,多实例就再好不过了。

MySQL多实例有它的好处,但也有其弊端,比如,会存在资源互相抢占的问题。

当某个数据库实例并发很高或者有SQL慢查询时,整个实例会消耗大量的系统CPU、磁盘I/O等资源,导致服务器上的其他数据库实例提供服务的质量一起下降。这就相当于大家住在一个房子的不同卧室一样,早晨起来上班,都要刷牙、洗脸等,这样卫生间就会长期占有,其他人就要等待一样。不同实例获取的资源是相对独立的,无法像虚拟化一样完全隔离。

MySQL多实例的生产应用场景

1、资金紧张型公司的选择

若公司资金紧张,公司业务访问量又不是太大,但又希望不同业务的数据库服务各自尽量独立地提供服务而互相不受影响,同时,还需要主从复制等技术提供备份或读写分离服务,那么,多实例就再好不过了。比如:可以通过3台服务器部署915个实例,交叉做主从复制、数据备份及读写分离,这样就可以达到915台服务器每个只装一个数据库才有的效果。这里要强调的是,所谓的尽量独立是相对的。

2、并发访问不是特别大的业务

当公司业务访问量不太大的时候,服务器的资源基本都是浪费的,这时就很适合多实例的应用,如果对SQL语句的优化做得比较好,MySQL多实例会是一个很值得使用的技术,即使并发很大,合理分配好系统资源以及搭配好服务,也不会有太大问题。

mysql多实例部署步骤

下载MySQL安装包并解压安装包至指定路径

该实验使用的是二进制安装包

[root@localhost ~]# wget -c  https://downloads.mysql.com/archives/get/p/23/file/mysql-5.7.33-linux-glibc2.12-x86_64.tar.gz
#下载过程省略
[root@localhost ~]# ls
anaconda-ks.cfg  mysql-5.7.33-linux-glibc2.12-x86_64.tar.gz
[root@localhost ~]# tar xf mysql-5.7.33-linux-glibc2.12-x86_64.tar.gz -C /usr/local/
[root@localhost ~]# ls /usr/local/
bin  games    lib    libexec                              sbin   src
etc  include  lib64  mysql-5.7.33-linux-glibc2.12-x86_64  share

创建软链接或重命名

[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]# mv mysql-5.7.33-linux-glibc2.12-x86_64 mysql	#重命名
[root@localhost local]# ls
bin  etc  games  include  lib  lib64  libexec  mysql  sbin  share  src
# [root@localhost ~]# ln -s /usr/local/mysql-5.7.33-linux-glibc2.12-x86_64/ /usr/local/mysql   #创建软链接
#创建软链接或者重命名二选一即可

创建系统用户MySQL

[root@localhost ~]# useradd -r -M -s /sbin/nologin mysql
[root@localhost ~]# id mysql
uid=995(mysql) gid=992(mysql) groups=992(mysql)

修改属主属组

[root@localhost local]# chown -R mysql.mysql mysql/
[root@localhost local]# ll
total 0
......
drwxr-xr-x. 9 mysql mysql 129 May 10 18:11 mysql
......

添加环境变量

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

创建数据存放目录(多个)并修改属主属组

[root@localhost local]# mkdir -p /opt/data/{3306,3307,3308}
[root@localhost ~]# tree /opt/data/
/opt/data/
├── 3306
├── 3307
└── 3308
3 directories, 0 files
[root@localhost local]# chown -R mysql.mysql /opt/data
[root@localhost local]# ll /opt/
total 0
drwxr-xr-x. 5 mysql mysql 42 May 10 18:33 data
[root@localhost local]# ll /opt/data/
total 0
drwxr-xr-x. 2 mysql mysql 6 May 10 18:33 3306
drwxr-xr-x. 2 mysql mysql 6 May 10 18:33 3307
drwxr-xr-x. 2 mysql mysql 6 May 10 18:33 3308

初始化各实例并保存实例的临时密码

#初始化3306实例并保存实例的临时密码
[root@localhost ~]# mysqld --initialize --user mysql --datadir /opt/data/3306
2021-05-10T10:40:27.012542Z 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:40:27.276762Z 0 [Warning] InnoDB: New log files created, LSN=45790
2021-05-10T10:40:27.316542Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2021-05-10T10:40:27.374076Z 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: 2241f270-b17c-11eb-aaa8-000c29a69e07.
2021-05-10T10:40:27.375023Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2021-05-10T10:40:27.801874Z 0 [Warning] CA certificate ca.pem is self signed.
2021-05-10T10:40:28.227520Z 1 [Note] A temporary password is generated for root@localhost: ;pAqp+CBu0u-
[root@localhost ~]# echo ';pAqp+CBu0u-' > 3306
#初始化3307实例并保存实例的临时密码
[root@localhost ~]# mysqld --initialize --user mysql --datadir /opt/data/3307
2021-05-10T10:42:05.331039Z 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:42:05.592377Z 0 [Warning] InnoDB: New log files created, LSN=45790
2021-05-10T10:42:05.659455Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2021-05-10T10:42:05.720872Z 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: 5ce07b07-b17c-11eb-ac12-000c29a69e07.
2021-05-10T10:42:05.721673Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2021-05-10T10:42:06.496585Z 0 [Warning] CA certificate ca.pem is self signed.
2021-05-10T10:42:06.637760Z 1 [Note] A temporary password is generated for root@localhost: efugC:UZV8p6
[root@localhost ~]# echo 'efugC:UZV8p6' > 3307
#初始化3308实例并保存实例的临时密码
[root@localhost ~]# mysqld --initialize --user mysql --datadir /opt/data/3308
2021-05-10T10:42:49.677550Z 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:42:49.947637Z 0 [Warning] InnoDB: New log files created, LSN=45790
2021-05-10T10:42:50.002173Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2021-05-10T10:42:50.010049Z 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: 77467924-b17c-11eb-a2b7-000c29a69e07.
2021-05-10T10:42:50.011283Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2021-05-10T10:42:50.384492Z 0 [Warning] CA certificate ca.pem is self signed.
2021-05-10T10:42:50.572440Z 1 [Note] A temporary password is generated for root@localhost: jot>MZi3no9d
[root@localhost ~]# echo 'jot>MZi3no9d' > 3308
[root@localhost ~]# ls
3306  3308             mysql-5.7.33-linux-glibc2.12-x86_64.tar.gz
3307  anaconda-ks.cfg

安装perl与ncurses-compat-libs

[root@localhost ~]# dnf -y install perl
#安装过程省略
[root@localhost ~]# dnf -y install ncurses-compat-libs
#安装过程省略

配置配置文件

[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

启动服务

[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             [::]:*              
[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       80                   *:3306              *:*              
LISTEN  0       80                   *:3307              *:*              
LISTEN  0       80                   *:3308              *:*              
LISTEN  0       128               [::]:22             [::]:*

修改密码

#修改实例3306密码
[root@localhost ~]# cat 3306
e&pDa0d=FkXg
[root@localhost ~]# mysql -uroot -p'e&pDa0d=FkXg' -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('填入你要修改的密码');
Query OK, 0 rows affected, 1 warning (0.00 sec)
#修改实例3307密码
[root@localhost ~]# cat 3307
ubTokkB3/cJI
[root@localhost ~]# mysql -uroot -p'ubTokkB3/cJI' -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('填入你要修改的密码');
Query OK, 0 rows affected, 1 warning (0.00 sec)
#修改实例3308密码
[root@localhost ~]# cat 3308
)g2Rjhmsn51,
[root@localhost ~]# mysql -uroot -p')g2Rjhmsn51,' -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('填入你要修改的密码');
Query OK, 0 rows affected, 1 warning (0.00 sec)
#然后依次退出重新登录你刚刚修改的密码看可以登录不,我这里成功了就不演示了

设置开机自启

[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
[root@localhost ~]# reboot		#重启测试
[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       80                   *:3306              *:*              
LISTEN  0       80                   *:3307              *:*              
LISTEN  0       80                   *:3308              *:*              
LISTEN  0       128               [::]:22             [::]:*

总结

mysql多实例部署步骤:

  1. 下载MySQL安装包并解压安装包至指定路径
  2. 创建软链接或重命名
  3. 创建系统用户MySQL
  4. 修改属主属组
  5. 添加环境变量
  6. 创建数据存放目录(多个)并修改属主属组
  7. 初始化各实例并保存实例的临时密码
  8. 安装perl与ncurses-compat-libs
  9. 配置配置文件
  10. 启动服务
  11. 修改密码
  12. 设置开机自启

以上是关于mysql多实例部署的主要内容,如果未能解决你的问题,请参考以下文章

MySQL运维-多实例部署

Mysql多实例部署

MySQL-5.6.36-多实例-部署(编译版)

MySQL多实例部署

MySQL单机多实例部署详解

mysql 多实例部署