文章目录
Mysql5.7 server官方手册
1.环境准备
主从有延迟,不适合实时同步,只能保证最终一致
1.1 server2上安装mysql
在server1上,将二进制编译好的mysql复制到server2上:scp -r mysql/ server2:/usr/local/
顺便复制配置文件:scp my.cnf server2:/etc/
及相关脚本:scp mysqld server2:/etc/init.d/
在server2中添加默认变量:vim .bash_profile
PATH=$PATH:$HOME/bin:/usr/local/mysql/bin
并使之生效:source .bash_profile
删除目录内文件:/usr/local/mysql/data
创建和server1同样的用户:useradd -M -d /usr/local/mysql/data/ -s /sbin/nologin mysql
初始化mysql:mysqld --initialize --user=mysql
启动mysql:/etc/init.d/mysqld start
利用初始化提供的密码进行初始化安全设置:mysql_secure_installation
1.2 配置
- 主从复制是基于mysql的二进制日志,在server1(主)上配置,打开主从功能:
vim /etc/my.conf
[mysqld]
datadir=/usr/local/mysql/data
socket=/usr/local/mysql/data/mysql.sock
symbolic-links=0
server-id=1
log-bin=mysql-bin
0表示拒绝任何id
重启:/etc/init.d/mysqld restart
- 创建一个用来复制的用户
进入server1的数据库:mysql -plee
授权:GRANT REPLICATION SLAVE ON *.* TO 'repl'@'%' IDENTIFIED BY 'lee';
查看master状态:show master status;
3. 配置server2
配置文件:vim /etc/my.cnf
[mysqld]
datadir=/usr/local/mysql/data
socket=/usr/local/mysql/data/mysql.sock
symbolic-links=0
server-id=2
重启数据库:/etc/init.d/mysqld restart
正常情况下需要备份数据,锁表。由于本实验的数据库均为新库,为空表,所以在此略过,若有需要查看官方文档
接下来指明slave端复制源,进入数据库mysql -plee
:
mysql> CHANGE MASTER TO MASTER_HOST='172.25.38.1', MASTER_USER='repl', MASTER_PASSWORD='lee',MASTER_LOG_FILE='mysql-bin.000001',MASTER_LOG_POS=437;
Query OK, 0 rows affected, 2 warnings (0.06 sec)
启动:start slave;
查看slave状态:show slave status\G;
IO接口负责复制Rep日志,SQL负责操作
检测:
在server1上创建一个test库create database test;
在server2上能同步:
2.主从复制
2.1 基于二进制文件的主从复制
配置server2
再开启一个server3,安装mysql,参考上述安装步骤
编辑server2上的配置文件:
[mysqld]
datadir=/usr/local/mysql/data
socket=/usr/local/mysql/data/mysql.sock
symbolic-links=0
server-id=2
log-slave-updates
log-bin=mysql-bin
进入数据库,添加一个远程用户:GRANT REPLICATION SLAVE ON *.* TO 'repl'@'%' IDENTIFIED BY 'lee';
配置server3
编辑server3上的配置文件:
[mysqld]
datadir=/usr/local/mysql/data
socket=/usr/local/mysql/data/mysql.sock
symbolic-links=0
server-id=3
在此首先拷贝server2上的数据库表:mysqldump -uroot -plee test > dump.db
发送给server3:scp dump.db server3:
在server3上先进入数据库,新建一个数据库表:mysqladmin create test -plee
后者进入数据库后:create database test;
(删除数据库表:drop database test;
)
导入至server3数据库:mysql -plee test < dump.db
查看导入成功:
server3可以通过远程登陆:
根据查询server2的master信息:
在server3上,进入数据库配置:change master to master_host='172.25.38.2',master_user='repl',master_password='lee',master_file_log='mysql-bin.000003',master_log_pos=415
进入数据库打开slave模式:start slave;
查看slave信息:show slave status;
此时,在server1上插入数据在server3上即可同步。
2.2 基于Gtid的主从复制
先停止server2,3的slave
编辑server1的mysql文件:
[mysqld]
datadir=/usr/local/mysql/data
socket=/usr/local/mysql/data/mysql.sock
symbolic-links=0
server-id=1
log-bin=mysql-bin
gtid_mode=ON
enforce-gtid-consistency=ON
编辑server2的:
[mysqld]
datadir=/usr/local/mysql/data
socket=/usr/local/mysql/data/mysql.sock
symbolic-links=0
server-id=2
log-slave-updates
log-bin=mysql-bin
gtid_mode=ON
enforce-gtid-consistency=ON
编辑server3的:
[mysqld]
datadir=/usr/local/mysql/data
socket=/usr/local/mysql/data/mysql.sock
symbolic-links=0
server-id=3
gtid_mode=ON
enforce-gtid-consistency=ON
开启slave,此时可以同步。
查看server2和server3的slave状态;
2.3 半同步
在server1数据库中安装插件:mysql> INSTALL PLUGIN rpl_semi_sync_master SONAME 'semisync_master.so';
在server2数据库中安装两个插件:
INSTALL PLUGIN rpl_semi_sync_slave SONAME 'semisync_slave.so';
INSTALL PLUGIN rpl_semi_sync_master SONAME 'semisync_master.so';
在server3数据库中安装一个插件:INSTALL PLUGIN rpl_semi_sync_slave SONAME 'semisync_slave.so';
在server1上打开master:SET GLOBAL rpl_semi_sync_master_enabled = 1;
在server上打开master和slave:
SET GLOBAL rpl_semi_sync_master_enabled = 1;
SET GLOBAL rpl_semi_sync_slave_enabled = 1;
在server3上打开slave:SET GLOBAL rpl_semi_sync_slave_enabled = 1;
查看以rpl开头的变量:show status like 'rpl%';
打开server2,3的IO接口:START SLAVE IO_THREAD;
在server1上插入数据:insert into user_tb values ('user5','5555');
此时server3上能顺利同步:
若关掉server2上的IO接口,再再server1上插入数据:insert into user_tb values ('user6','6666');
则会有10s的延迟:
并且server3上无法同步到数据,打开server2上的IO接口后,server3会自动同步:
3.多主复制
3.1 去中心化集群
首先停止所有节点的mysql,删除data/文件,并初始化:mysqld --initialize --user=mysql
再启动所有节点的mysql:/etc/init.d/mysql start
server1上:
# cat /etc/my.cnf
[mysqld]
datadir=/usr/local/mysql/data
socket=/usr/local/mysql/data/mysql.sock
symbolic-links=0
disabled_storage_engines="MyISAM,BLACKHOLE,FEDERATED,ARCHIVE,MEMORY"
server_id=1
gtid_mode=ON
enforce_gtid_consistency=ON
master_info_repository=TABLE
relay_log_info_repository=TABLE
binlog_checksum=NONE
log_slave_updates=ON
log_bin=binlog
binlog_format=ROW
plugin_load_add='group_replication.so'
transaction_write_set_extraction=XXHASH64
group_replication_group_name="aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa"
group_replication_start_on_boot=off
group_replication_local_address= "172.25.0.1:33061"
group_replication_group_seeds= "172.25.0.1:33061,172.25.0.2:33061,172.25.0.3:33061"
group_replication_bootstrap_group=off
group_replication_ip_whitelist="172.25.0.0/24,127.0.0.1/8"
group_replication_single_primary_mode=OFF
group_replication_enforce_update_everywhere_checks=ON
server2上:
# cat /etc/my.cnf
[mysqld]
datadir=/usr/local/mysql/data
socket=/usr/local/mysql/data/mysql.sock
symbolic-links=0
disabled_storage_engines="MyISAM,BLACKHOLE,FEDERATED,ARCHIVE,MEMORY"
server_id=2
gtid_mode=ON
enforce_gtid_consistency=ON
master_info_repository=TABLE
relay_log_info_repository=TABLE
binlog_checksum=NONE
log_slave_updates=ON
log_bin=binlog
binlog_format=ROW
plugin_load_add='group_replication.so'
transaction_write_set_extraction=XXHASH64
group_replication_group_name="aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa"
group_replication_start_on_boot=off
group_replication_local_address= "172.25.38.2:33061"
group_replication_group_seeds= "172.25.38.1:33061,172.25.38.2:33061,172.25.38.3:33061"
group_replication_bootstrap_group=off
group_replication_ip_whitelist="172.25.38.0/24,127.0.0.1/8"
group_replication_single_primary_mode=OFF
group_replication_enforce_update_everywhere_checks=ON
group_replication_allow_local_disjoint_gtids_join=ON
server3上:
# cat /etc/my.cnf
[mysqld]
datadir=/usr/local/mysql/data
socket=/usr/local/mysql/data/mysql.sock
symbolic-links=0
disabled_storage_engines="MyISAM,BLACKHOLE,FEDERATED,ARCHIVE,MEMORY"
server_id=3
gtid_mode=ON
enforce_gtid_consistency=ON
master_info_repository=TABLE
relay_log_info_repository=TABLE
binlog_checksum=NONE
log_slave_updates=ON
log_bin=binlog
binlog_format=ROW
plugin_load_add='group_replication.so'
transaction_write_set_extraction=XXHASH64
group_replication_group_name="aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa"
group_replication_start_on_boot=off
group_replication_local_address= "172.25.38.3:33061"
group_replication_group_seeds= "172.25.38.1:33061,172.25.0.2:33061,172.25.38.3:33061"
group_replication_bootstrap_group=off
group_replication_ip_whitelist="172.25.38.0/24,127.0.0.1/8"
group_replication_single_primary_mode=OFF
group_replication_enforce_update_everywhere_checks=ON
group_replication_allow_local_disjoint_gtids_join=ON
mysql> alter user root@localhost identified by 'lee'; #改密码
mysql> SET SQL_LOG_BIN=0; #关闭日志
mysql> CREATE USER rpl_user@'%' IDENTIFIED BY 'password'; #创建一个数据库用户
mysql> GRANT REPLICATION SLAVE ON *.* TO rpl_user@'%'; #赋予权限
mysql> FLUSH PRIVILEGES; #刷新权限表
mysql> SET SQL_LOG_BIN=1; #打开日志
mysql> CHANGE MASTER TO MASTER_USER='rpl_user', MASTER_PASSWORD='password' FOR CHANNEL 'group_replication_recovery';
#指定集群master
mysql> SET GLOBAL group_replication_bootstrap_group=ON; #只有第一个节点执行
mysql> START GROUP_REPLICATION; #打开组复制功能
mysql> SET GLOBAL group_replication_bootstrap_group=OFF; #只有第一个节点执行
查看已加入节点SELECT * FROM performance_schema.replication_group_members;
打开慢查询:set global slow_query_log=ON;
3.2 多主复制下的读写分离
首先保证3个mysql集群都在线:
新建一个server4,在上面安装mysql路由转发功能:
安装包:yum install mysql-router-community-8.0.21-1.el7.x86_64.rpm
修改配置,在最后添加:vim /etc/mysqlrouter/mysqlrouter.conf
[routing:ro]
bind_address = 0.0.0.0
bind_port = 7001
destinations = 172.25.38.1:3306,172.25.38.2:3306,172.25.38.3:3306
routing_strategy = round-robin
[routing:rw]
bind_address = 0.0.0.0
bind_port = 7002
destinations = 172.25.38.1:3306,172.25.38.2:3306,172.25.38.3:3306
routing_strategy = first-available
启动转发服务:systemctl start mysqlrouter.service
在server1上添加两个远程登陆用户,分别具有查询权限和部分表的所有权限:
grant select on *.* to user1@'%' identified by 'lee';
grant all on test.* to user2@'%' identified by 'lee';
flush privileges; #刷新权限
在真机上即可登陆:mysql -h 172.25.38.4 -P 7001 -u user1 -p