一般情况下一台linux虚拟机只安装一个mysql实例,但我们知道,mysql实例为单进程,
在有些情况下,如cpu、内存、高速IO存储设备利用率很低,为充分利用硬件资源,需要
一台虚拟机安装多个mysql实例启用多个mysql进程。
特别提示:
*安装多个mysql实例并不是要讲mysql的二进制包解压多次,而只是解压一次,初始化4次。
*my.cnf文件在本文档的最后方,参考使用,根据实际环境配置。
具体流程如下:
一、卸载默认mariadb数据库
1、linux版本为centos7.5,默认安装mariadb数据库,同时/etc/下面有my.cnf文件,检测是否安装mariadb
[root@localhost ~]# rpm -qa|grep mariadb
mariadb-libs-5.5.56-2.el7.x86_64
2、删除maridb数据库
[root@localhost ~]# rpm-e mariadb-libs-5.5.56-2.el7.x86_64 --nodeps
3、已删除maridb数据库
[root@localhost ~]# rpm -qa|grep mariadb
4、同时/etc/my.cnf文件也已经删除
[root@localhost ~]# cd /etc [root@localhost etc]# ls -arthl my.cnf
ls: 无法访问my.cnf: 没有那个文件或目录
二、上传mysql5.7.29二进制包并解压,创建mysql用户和用户组
1、yum install lrzsz包,此包可以在shell下面使用rz、sz命令上传下载文件
[root@localhost etc]# yum install lrzsz
2、通过rz上传mysql-5.7.29-linux-glibc2.12-x86_64.tar.gz到/usr/local/目录下
[root@localhost local]# rz
3、解压mysql-5.7.29-linux-glibc2.12-x86_64.tar.gz
[root@localhost local]# tar -zxvf mysql-5.7.29-linux-glibc2.12-x86_64.tar.gz
4、增加mysql用户组
[root@localhost local]# groupadd mysql
5、增加mysql用户同时加入mysql组
[root@localhost local]# useradd mysql -r -g mysql
6、查看mysql用户是否创建成功
[root@localhost local]# more /etc/passwd
root:x:0:0:root:/root:/bin/bash
......
mysql:x:997:1000::/home/mysql:/bin/bash
三、mysql实例初始化工作
1、创建指向mysql-5.7.29-linux-glibc2.12-x86_64的mysql链接(这里mysql自动生成)
[root@localhost local]# ln -s mysql-5.7.29-linux-glibc2.12-x86_64 mysql
2、查看mysql文件夹是否指向mysql-5.7.29-linux-glibc2.12-x86_64成功
[root@localhost local]# ls mysql
bin docs include lib LICENSE man README share support-files
3、创建初始化4个mysql实例数据存放目录,并设置用户和用户组为mysql;mysql
[root@localhost etc]# cd /home
[root@localhost home]# mkdir data1 data2 data3 data4
[root@localhost home]# chown -R mysql:mysql *
[root@localhost home]# ls
data1 data2 data3 data4
4、设置/usr/local/mysql/bin环境变量,在/etc/profile文件最后加入export PATH=$PATH:/usr/local/mysql/bin
[root@localhost home]# vi /etc/profile
5、同时使用source /etc/profile生效新增环境变量
[root@localhost home]# source /etc/profile
6、echo数据环境变量,查看是否成功
[root@localhost home]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/usr/local/mysql/bin
7、初始化mysql第一个实例
[root@localhost bin]# ./mysqld --initialize --user=mysql --datadir=/home/data1
8、到初始化指定的数据目录查看是否已经初始化成功各个文件
[root@localhost bin]# cd /home/data1
[root@localhost data1]# ls
auto.cnf ca-key.pem client-key.pem ibdata1 mysql public_key.pem slow.log undo002
bin.000001 ca.pem error.log ib_logfile0 performance_schema server-cert.pem sys undo003
bin.index client-cert.pem ib_buffer_pool ib_logfile1 private_key.pem server-key.pem undo001
9、打开msyql数据库error.log日志,这里没有error,warning不影响初始化成功,已经生成数据库密码。
[root@localhost data1]# vi error.log
2020-01-29T11:58:29.448266+08:00 0 [Warning] InnoDB: Using innodb_file_format is deprecated and the parameter may be removed in future releases. See http://dev.mysql.com/doc/refman/5.7/en/innodb-file-format.html
2020-01-29T11:58:29.448318+08:00 0 [Warning] InnoDB: Using innodb_file_format_max is deprecated and the parameter may be removed in future releases. See http://dev.mysql.com/doc/refman/5.7/en/innodb-file-format.html
100
100
2020-01-29T11:58:29.782024+08:00 0 [Warning] InnoDB: New log files created, LSN=48433
2020-01-29T11:58:29.812370+08:00 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2020-01-29T11:58:29.879649+08:00 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: 9c331936-424b-11ea-b848-000c29961e3d.
2020-01-29T11:58:29.880282+08:00 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2020-01-29T11:58:30.537628+08:00 0 [Warning] CA certificate ca.pem is self signed.
2020-01-29T11:58:30.918490+08:00 1 [Note] A temporary password is generated for root@localhost: byuBj8f;Iw)x
10、根据第一个实例初始化情况,同时做完2、3、4个mysql实例并检查是否成功
[root@localhost bin]# ./mysqld --initialize --user=mysql --datadir=/home/data2
[root@localhost bin]# ./mysqld --initialize --user=mysql --datadir=/home/data3
[root@localhost bin]# ./mysqld --initialize --user=mysql --datadir=/home/data4
四、4个mysql实例初始化成功后,开始多实例方面配置并启动停止
1、在/usr/local/mysql目录下开始创建multi.log文件,记录多实例启动相关信息
[root@localhost mysql]# touch multi.log
2、查看4个实例启动情况,4个实例未运行
[root@localhost mysql]# mysqld_multi report
Reporting MySQL servers
MySQL server from group: mysqld1 is not running
MySQL server from group: mysqld2 is not running
MySQL server from group: mysqld3 is not running
MySQL server from group: mysqld4 is not running
3、启动第一个实例,这里的实例与my.cnf中配置[mysqld1]对应
[root@localhost mysql]# mysqld_multi start 1
4、mysql实例进程1启动成功
[root@localhost mysql]# ps -ef|grep mysql
root 4051 1 1 12:20 pts/0 00:00:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --port=3306 --datadir=/home/data1 --socket=/tmp/mysql.sock1 --innodb_log_group_home_dir=/home/data1 --innodb_undo_directory=/home/data1
mysql 5136 4051 8 12:20 pts/0 00:00:00 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/home/data1 --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --innodb-log-group-home-dir=/home/data1 --innodb-undo-directory=/home/data1 --log-error=error.log --pid-file=localhost.localdomain.pid --socket=/tmp/mysql.sock1 --port=3306
root 5166 1658 0 12:20 pts/0 00:00:00 grep --color=auto mysql
5、查看mysql实例进程1端口3306监听情况
[root@localhost mysql]# ss -an|grep mysql
u_str LISTEN 0 128 /tmp/mysql.sock1 28952
[root@localhost mysql]# ss -an|grep 3306
tcp LISTEN 0 128 :::3306
[root@localhost mysql]# ss -an|grep 3306
tcp LISTEN 0 128 :::3306
6、再次查看mysqld_multi report,实例1进程已经启动
[root@localhost mysql]# mysqld_multi report
Reporting MySQL servers
MySQL server from group: mysqld1 is running
MySQL server from group: mysqld2 is not running
MySQL server from group: mysqld3 is not running
MySQL server from group: mysqld4 is not running
7、查看实例1在/home/data1/error.log中生成密码
[root@localhost mysql]# vi /home/data1/error.log
8、通过socket进入mysql实例1,输入在error.log中密码,进入成功,设置与[mysqld_multi]中pass同样密码123456,
msyql5.7首次进入后必须重新设置密码,密码不与pass一致,下次无法用mysqld_multi启动停止,其他3个实例同样配置
[root@localhost mysql]# mysql -S /tmp/mysql.sock1 -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.29-log
Copyright (c) 2000, 2020, 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> set passwd='123456';
ERROR 1193 (HY000): Unknown system variable 'passwd'
mysql> set password='123456';
Query OK, 0 rows affected (5.00 sec)
mysql> exit
Bye
9、启动2、3、4mysql实例进程
[root@localhost mysql]# mysqld_multi start 2
[root@localhost mysql]# mysqld_multi start 3
[root@localhost mysql]# mysqld_multi start 4
[root@localhost mysql]# mysqld_multi report
10、再次查看mysqld_multi report,4个实例已经全部启动
Reporting MySQL servers
MySQL server from group: mysqld1 is running
MySQL server from group: mysqld2 is running
MySQL server from group: mysqld3 is running
MySQL server from group: mysqld4 is running
11、查看4个实例进程情况
[root@localhost mysql]# ps -ef|grep mysql
root 4051 1 0 12:20 pts/0 00:00:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --port=3306 --datadir=/home/data1 --socket=/tmp/mysql.sock1 --innodb_log_group_home_dir=/home/data1 --innodb_undo_directory=/home/data1
mysql 5136 4051 0 12:20 pts/0 00:00:00 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/home/data1 --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --innodb-log-group-home-dir=/home/data1 --innodb-undo-directory=/home/data1 --log-error=error.log --pid-file=localhost.localdomain.pid --socket=/tmp/mysql.sock1 --port=3306
root 5278 1 0 12:23 pts/0 00:00:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --port=3307 --datadir=/home/data2 --socket=/tmp/mysql.sock2 --innodb_log_group_home_dir=/home/data2 --innodb_undo_directory=/home/data2
mysql 6363 5278 2 12:23 pts/0 00:00:00 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/home/data2 --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --innodb-log-group-home-dir=/home/data2 --innodb-undo-directory=/home/data2 --log-error=error.log --pid-file=localhost.localdomain.pid --socket=/tmp/mysql.sock2 --port=3307
root 6396 1 0 12:23 pts/0 00:00:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --port=3308 --datadir=/home/data3 --socket=/tmp/mysql.sock3 --innodb_log_group_home_dir=/home/data3 --innodb_undo_directory=/home/data3
mysql 7481 6396 3 12:23 pts/0 00:00:00 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/home/data3 --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --innodb-log-group-home-dir=/home/data3 --innodb-undo-directory=/home/data3 --log-error=error.log --pid-file=localhost.localdomain.pid --socket=/tmp/mysql.sock3 --port=3308
root 7514 1 0 12:23 pts/0 00:00:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --port=3309 --datadir=/home/data4 --socket=/tmp/mysql.sock4 --innodb_log_group_home_dir=/home/data4 --innodb_undo_directory=/home/data4
mysql 8599 7514 5 12:23 pts/0 00:00:00 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/home/data4 --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --innodb-log-group-home-dir=/home/data4 --innodb-undo-directory=/home/data4 --log-error=error.log --pid-file=localhost.localdomain.pid --socket=/tmp/mysql.sock4 --port=3309
root 8646 1658 0 12:23 pts/0 00:00:00 grep --color=auto mysql
12、top查看MySQL实例运行情况
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
13120 mysql 20 0 1275488 191564 9400 S 0.0 19.2 0:00.23 mysqld
13114 mysql 20 0 1275380 191004 9400 S 0.0 19.1 0:00.25 mysqld
13115 mysql 20 0 1275380 189448 9400 S 0.0 19.0 0:00.22 mysqld
13123 mysql 20 0 1275380 184472 9400 S 0.0 18.5 0:00.22 mysqld
17548 root 20 0 161964 2224 1552 R 0.3 0.2 0:00.02 top
462 root 20 0 37124 2132 1996 S 0.0 0.2 0:00.17 systemd-journal
1462 root 20 0 214424 1776 1656 S 0.0 0.2 0:00.19 rsyslogd
1 root 20 0 128360 1760 572 S 0.0 0.2 0:01.71 systemd
948 root 20 0 225196 1648 1288 S 0.3 0.2 0:01.58 vmtoolsd
8760 root 20 0 113300 1632 1296 S 0.0 0.2 0:00.05 mysqld_safe
8773 root 20 0 113300 1632 1296 S 0.0 0.2 0:00.05 mysqld_safe
8764 root 20 0 113300 1628 1296 S 0.0 0.2 0:00.05 mysqld_safe
8784 root 20 0 113300 1628 1296 S 0.0 0.2 0:00.05 mysqld_safe
1026 root 20 0 358104 1392 376 S 0.0 0.1 0:00.73 firewalld
......
[END] 2020/1/29 12:27:12
13、4个实例分别停止,或者用mysqld_multi stop 全部停止
[root@localhost mysql]# mysqld_multi stop 1
[root@localhost mysql]# mysqld_multi stop 2
[root@localhost mysql]# mysqld_multi stop 3
[root@localhost mysql]# mysqld_multi stop 4
14、查看mysql实例停止情况
[root@localhost mysql]# mysqld_multi report
Reporting MySQL servers
MySQL server from group: mysqld1 is not running
MySQL server from group: mysqld2 is not running
MySQL server from group: mysqld3 is not running
MySQL server from group: mysqld4 is not running
五、mysql5.7.29多实例my.cnf配置文件
上传的my.cnf样本与多实例安装有关的参数块为下方的[mysqld_multi][mysqld1][mysqld2]
[mysqld3][mysqld4],这些参数块上面的参数为生产环境参数,此案例未删除,上面参数
会分别继承到[mysqld1][mysqld2][mysqld3][mysqld4]块中。
[mysqld]
#basic
server-id = 1
port = 3306
user = mysql
datadir = /home/data
tmpdir = /tmp/
#bind_address = 192.168.101.132
autocommit = 1
character_set_server=utf8mb4
skip_name_resolve = 1
transaction_isolation = READ-COMMITTED
explicit_defaults_for_timestamp = 1
sql_mode = "STRICT_TRANS_TABLES,NO_ENGINE_SUBSTITUTION,NO_ZERO_DATE,NO_ZERO_IN_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER"
max_connections = 800
max_connect_errors = 1000
interactive_timeout = 1800
wait_timeout = 1800
tmp_table_size = 67108864
max_allowed_packet = 16777216
join_buffer_size = 134217728
read_buffer_size = 16777216
read_rnd_buffer_size = 33554432
sort_buffer_size = 33554432
#log
log_error = error.log
slow_query_log = 1
slow_query_log_file = slow.log
log_queries_not_using_indexes = 1
log_slow_admin_statements = 1
log_slow_slave_statements = 1
log_throttle_queries_not_using_indexes = 10
expire_logs_days = 30
long_query_time = 3
min_examined_row_limit = 10000
#innodb settings
#innodb_page_size = 8192
innodb_buffer_pool_size = 32g
innodb_buffer_pool_instances = 8
innodb_buffer_pool_load_at_startup = 1
innodb_buffer_pool_dump_at_shutdown = 1
innodb_lru_scan_depth = 2000
innodb_lock_wait_timeout = 5
innodb_io_capacity = 4000
innodb_io_capacity_max = 8000
innodb_flush_method = O_DIRECT
innodb_file_format = Barracuda
innodb_file_format_max = Barracuda
#innodb_log_group_home_dir = /home/data
#innodb_undo_directory = /home/data
innodb_undo_logs = 128
innodb_undo_tablespaces = 3
innodb_flush_neighbors = 1
innodb_log_file_size = 1g
innodb_log_buffer_size = 16777216
innodb_purge_threads = 4
innodb_large_prefix = 1
innodb_thread_concurrency = 64
innodb_print_all_deadlocks = 1
innodb_strict_mode = 1
innodb_sort_buffer_size = 67108864
#replication
master_info_repository = TABLE
relay_log_info_repository = TABLE
log_bin = bin.log
sync_binlog = 1
gtid_mode = on
enforce_gtid_consistency = 1
log_slave_updates
binlog_format = row
relay_log = relay.log
relay_log_recovery = 1
binlog_gtid_simple_recovery = 1
slave_skip_errors = ddl_exist_errors
#semi sync replication
plugin_dir=/usr/local/mysql/lib/plugin
plugin_load = "rpl_semi_sync_master=semisync_master.so;rpl_semi_sync_slave=semisync_slave.so"
loose_rpl_semi_sync_master_enabled = 1
loose_rpl_semi_sync_slave_enabled = 1
loose_rpl_semi_sync_master_timeout = 3000
innodb_buffer_pool_dump_pct = 30
innodb_page_cleaners = 4
innodb_undo_log_truncate = 1
innodb_max_undo_log_size = 50m
innodb_purge_rseg_truncate_frequency = 128
binlog_gtid_simple_recovery=1
log_timestamps=system
transaction_write_set_extraction=MURMUR32
[mysqld_multi]
user=root
pass=123456
mysqld=/usr/local/mysql/bin/mysqld_safe
#mysqladmin=/usr/local/mysql/bin/mysqladmin
log=/usr/local/mysql/multi.log
[mysqld1]
port = 3306
datadir = /home/data1
socket = /tmp/mysql.sock1
innodb_log_group_home_dir = /home/data1
innodb_undo_directory = /home/data1
[mysqld2]
port = 3307
datadir = /home/data2
socket = /tmp/mysql.sock2
innodb_log_group_home_dir = /home/data2
innodb_undo_directory = /home/data2
[mysqld3]
port = 3308
datadir = /home/data3
socket = /tmp/mysql.sock3
innodb_log_group_home_dir = /home/data3
innodb_undo_directory = /home/data3
[mysqld4]
port = 3309
datadir = /home/data4
socket = /tmp/mysql.sock4
innodb_log_group_home_dir = /home/data4
innodb_undo_directory = /home/data4