【干货】MySQL 5.7 多实例(多进程)配置教程

前言

大学把数据库的课全翘了,之后可能是报应吧,后面数据库相关的东西怎么都学不会。最近有个MySQL多开的需求,正好借这个机会再复习一遍MySQL安装配置。
写这篇博客还有另一个原因,现在网上搜到的MySQL多开教程都不是针对5.7版本的,直接按那些教程做很多地方会报错。

MySQL多实例的原理

MySQL本来就可以多实例运行,只要修改启动脚本和配置文件,把端口,basedirdatadir文件夹分开,两个实例就可以互不影响运行。但是这种方式太过繁杂,所以MySQL官方提供了一个mysqld_multi的程序来辅助实现多实例运行,本篇教程就使用这种方式。

一,创建并初始化data文件夹

几个实例要分开运行,必然要把数据库文件放到不同文件夹中,所以第一步是要建立各个实例的数据文件夹,这里假设我们要运行三个实例,端口分别是3306,3307,3308,为了方便维护,我们把数据文件夹也按照端口号来命名:

mkdir /mnt/data/mysql/3306
mkdir /mnt/data/mysql/3307
mkdir /mnt/data/mysql/3308

chown mysql:mysql /mnt/data/mysql/3306
chown mysql:mysql /mnt/data/mysql/3307
chown mysql:mysql /mnt/data/mysql/3308

建好文件夹后,我们还要初始化数据文件夹,旧的命令是:

mysql_install_db --basedir=/usr/local/mysql --datadir=/mnt/data/mysql/3308 --user=mysql

这个命令已经不能再用!!!
按照官方教程,新的命令应该是:

mysqld --initialize-insecure --user=mysql --basedir=/opt/mysql/mysql --datadir=/opt/mysql/mysql/data

但是这个命令也会报错!!!
正确的做法是:

mysqld --defaults-file=/opt/mysql/mysql/etc/my.cnf --initialize-insecure --user=mysql

其中的原因我没有细究,可能是mysqld --initialize-insecure 还需要几个参数,.cnf文件里有提供这几个参数。
新建几个.cnf文件,用来初始化数据文件夹,.cnf文件内容如下:

[client]
port        = 3308
socket      = /tmp/mysql3308.sock

[mysqld]
port        = 3308
socket      = /tmp/mysql3308.sock
datadir = /mnt/data/mysql/3308
skip-external-locking
key_buffer_size = 256M
max_allowed_packet = 1M
table_open_cache = 1024
sort_buffer_size = 4M
net_buffer_length = 8K
read_buffer_size = 4M
read_rnd_buffer_size = 512K
myisam_sort_buffer_size = 64M
thread_cache_size = 128
query_cache_size = 128M
tmp_table_size = 128M
explicit_defaults_for_timestamp = true
max_connections = 500
max_connect_errors = 100
open_files_limit = 65535
log-bin=/mnt/data/mysql/3308/mysql-bin
binlog_format=mixed
server-id   = 3308
expire_logs_days = 10
early-plugin-load = ""
default_storage_engine = InnoDB
innodb_data_home_dir = /mnt/data/mysql/3308
innodb_data_file_path = ibdata1:10M:autoextend
innodb_log_group_home_dir = /mnt/data/mysql/3308
innodb_buffer_pool_size = 1024M
innodb_log_file_size = 256M
innodb_log_buffer_size = 8M
innodb_flush_log_at_trx_commit = 1
innodb_lock_wait_timeout = 50
transaction-isolation=READ-COMMITTED

[mysqldump]
quick
max_allowed_packet = 16M

[mysql]
no-auto-rehash

[myisamchk]
key_buffer_size = 256M
sort_buffer_size = 4M
read_buffer = 2M
write_buffer = 2M

[mysqlhotcopy]
interactive-timeout

把上面配置复制下来,创建/mnt/data/mysql/3308.cnf文件,然后运行:

mysqld --defaults-file=/mnt/data/mysql/3308.cnf --initialize-insecure --user=mysql

运行完之后,3308.cnf文件就可以不要了,后续的配置不需要3308.cnf文件,实例的参数会在my.cnf中集中配置。
重复这几个步骤,把3307、3306目录初始化好。
注意,data目录在初始化前,必须为空,不然初始化是会报错

二,配置my.cnf文件

mysqld_multi需要的my.cnf文件与一般配置文件不同,这里先给出配置样例:

[mysqld_multi]
mysqld     = /usr/local/mysql/bin/mysqld_safe
mysqladmin = /usr/local/mysql/bin/mysqladmin
[mysqld1]
port        = 3306
socket      = /tmp/mysql3306.sock
datadir = /mnt/data/mysql/3306
skip-external-locking
key_buffer_size = 256M
max_allowed_packet = 1M
table_open_cache = 1024
sort_buffer_size = 4M
net_buffer_length = 8K
read_buffer_size = 4M
read_rnd_buffer_size = 512K
myisam_sort_buffer_size = 64M
thread_cache_size = 128
query_cache_size = 128M
tmp_table_size = 128M
explicit_defaults_for_timestamp = true
max_connections = 500
max_connect_errors = 100
open_files_limit = 65535
log-bin=/mnt/data/mysql/3306/mysql-bin
binlog_format=mixed
server-id   = 3306
expire_logs_days = 10
early-plugin-load = ""
default_storage_engine = InnoDB
innodb_data_home_dir = /mnt/data/mysql/3306
innodb_data_file_path = ibdata1:10M:autoextend
innodb_log_group_home_dir = /mnt/data/mysql/3306
innodb_buffer_pool_size = 1024M
innodb_log_file_size = 256M
innodb_log_buffer_size = 8M
innodb_flush_log_at_trx_commit = 1
innodb_lock_wait_timeout = 50
transaction-isolation=READ-COMMITTED
user       = mysql

[mysqld2]
port        = 3307
socket      = /tmp/mysql3307.sock
datadir = /mnt/data/mysql/3307
skip-external-locking
key_buffer_size = 256M
max_allowed_packet = 1M
table_open_cache = 1024
sort_buffer_size = 4M
net_buffer_length = 8K
read_buffer_size = 4M
read_rnd_buffer_size = 512K
myisam_sort_buffer_size = 64M
thread_cache_size = 128
query_cache_size = 128M
tmp_table_size = 128M
explicit_defaults_for_timestamp = true
max_connections = 500
max_connect_errors = 100
open_files_limit = 65535
log-bin=/mnt/data/mysql/3307/mysql-bin
binlog_format=mixed
server-id   = 3307
expire_logs_days = 10
early-plugin-load = ""
default_storage_engine = InnoDB
innodb_data_home_dir = /mnt/data/mysql/3307
innodb_data_file_path = ibdata1:10M:autoextend
innodb_log_group_home_dir = /mnt/data/mysql/3307
innodb_buffer_pool_size = 1024M
innodb_log_file_size = 256M
innodb_log_buffer_size = 8M
innodb_flush_log_at_trx_commit = 1
innodb_lock_wait_timeout = 50
transaction-isolation=READ-COMMITTED
user       = mysql

[mysqld3]
port        = 3308
socket      = /tmp/mysql3308.sock
datadir = /mnt/data/mysql/3308
skip-external-locking
key_buffer_size = 256M
max_allowed_packet = 1M
table_open_cache = 1024
sort_buffer_size = 4M
net_buffer_length = 8K
read_buffer_size = 4M
read_rnd_buffer_size = 512K
myisam_sort_buffer_size = 64M
thread_cache_size = 128
query_cache_size = 128M
tmp_table_size = 128M
explicit_defaults_for_timestamp = true
max_connections = 500
max_connect_errors = 100
open_files_limit = 65535
log-bin=/mnt/data/mysql/3308/mysql-bin
binlog_format=mixed
server-id   = 3308
expire_logs_days = 10
early-plugin-load = ""
default_storage_engine = InnoDB
innodb_data_home_dir = /mnt/data/mysql/3308
innodb_data_file_path = ibdata1:10M:autoextend
innodb_log_group_home_dir = /mnt/data/mysql/3308
innodb_buffer_pool_size = 1024M
innodb_log_file_size = 256M
innodb_log_buffer_size = 8M
innodb_flush_log_at_trx_commit = 1
innodb_lock_wait_timeout = 50
transaction-isolation=READ-COMMITTED
user       = mysql

[mysqldump]
quick
max_allowed_packet = 16M

[mysql]
no-auto-rehash

[myisamchk]
key_buffer_size = 256M
sort_buffer_size = 4M
read_buffer = 2M
write_buffer = 2M

[mysqlhotcopy]
interactive-timeout

用于mysqld_multi的配置文件和一般MySQL配置不同,没有[mysqld]段,取而代之的是[mysqld1][mysqld2]等配置段,每个配置段代表一个MySQL实例。
先备份原来的.cnf文件

cp /etc/my.cnf /etc/my.cnf.bak

再修改my.cnf文件

nano /etc/my.cnf

将上面配置复制进去,退出保存。

三,多个MySQL实例管理

1,启动多个MySQL实例

做好以上两个步骤之后,就可以启动MySQL了。启动时需要一个启动脚本,这个脚本一般在/usr/local/mysql/support-files目录下mysqld_multi.server,将这个脚本复制到init.d目录下

cp  /usr/local/mysql/support-files/mysqld_multi.server /etc/rc.d/init.d/mysqld_multi

之后可以用命令

/etc/rc.d/init.d/mysqld_multi start 1-3

启动三个MySQL实例,注意这里的数字和my.cnf中的[mysqldN]对应,1-3就是启动[mysqld1][mysqld2][mysqld3]配置段的MySQL实例。
运行命令

netstat -ano | egrep "3307|3308|3306"

查看端口,看MySQL有没有正常启动,如果没有启动或报错,一般报错详细日志存在各个实例data目录下的 主机名.err文件中,打开此文件查找错误原因,逐步排错就可以了。

2,关闭多个MySQL实例

一般教程上关闭的命令是

/etc/rc.d/init.d/mysqld_multi stop 1-3

但是这个命令经常不起作用,所以一般我是用

killall -u mysql

或者

kill -9 the-mysql-pid

来关闭的。

四,root账户管理

因为第一节中我们初始化data目录的时候,用的是--initialize-insecure参数,所以我们初始化的数据库,root账户是没有密码的!要先改密码:

mysqladmin -uroot password 'Choose-Your-Password' -S /tmp/mysql3306.sock
mysqladmin -uroot password 'Choose-Your-Password' -S /tmp/mysql3307.sock
mysqladmin -uroot password 'Choose-Your-Password' -S /tmp/mysql3308.sock

或者:

mysqladmin -uroot password 'Choose-Your-Password' -P3306 -h127.0.0.1
mysqladmin -uroot password 'Choose-Your-Password' -P3307 -h127.0.0.1
mysqladmin -uroot password 'Choose-Your-Password' -P3308 -h127.0.0.1

如果需要开启root远程登录权限:

mysql -uroot -p'Your-Password' -P3306 -h127.0.0.1

use mysql
update user set host='%' where user='root';
flush privileges;
select user,host,authentication_string from user;

你可能感兴趣的:(数据库)