MySQL 5.7 主从复制(主从同步)
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MySQL 5.7 主从复制(主从同步)相关的知识,希望对你有一定的参考价值。
1、说明:
IP 计算机名 角色 192.168.1.222 mysql-001 master 192.168.1.233 MySQL-002 slave 系统:CentOS 6.* 或 7.* MySQL版本:5.7
2、master配置文件设置如下
一般mysql配置文件在/etc/my.cnf
(如果找不到的话也有可能在这些目录下:/etc/mysql/my.cnf,/usr/local/mysql/etc/my.cnf,~/.my.cnf)
[[email protected] ~]# vim /etc/my.cnf [mysqld] basedir=/usr/local/mysql datadir=/data/mysqldata socket=/tmp/mysql.sock user=mysql port=3306 #作为master的配置 server-id=1 # 服务器id (主从必须不一样) binlog-do-db=employees # 要给从机同步的库 binlog-ignore-db=mysql # 不给从机同步的库(多个写多行) binlog-ignore-db=information_schema binlog-ignore-db=performance_schema binlog-ignore-db=sys log-bin=mysql-bin # 打开日志(主机需要打开),这个mysql-bin也可以自定义; expire_logs_days=90 # 自动清理 90 天前的log文件,可根据需要修改 #重启数据库 CentOS 6.*: [[email protected] ~]# service mysqld restart [[email protected] ~]# service mysqld status SUCCESS! MySQL running (15607) CentOS 7.*: [[email protected] ~]# systemctl restart mysqld.service [[email protected] ~]# systemctl status mysqld.service mysqld.service - MySQL Server Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled) Active: active (running) since 四 2018-05-17 11:42:02 CST; 2h 5min ago Docs: man:mysqld(8) http://dev.mysql.com/doc/refman/en/using-systemd.html Main PID: 29959 (mysqld) CGroup: /system.slice/mysqld.service └─29959 /opt/mysql/bin/mysqld --defaults-file=/etc/my.cnf 5月 17 11:42:02 tcloud-118 systemd[1]: Started MySQL Server. 测试log_bin是否成功开启 [[email protected] ~]# mysql -uroot -p mysql> show variables like '%log_bin%'; +---------------------------------+---------------------------------+ | Variable_name | Value | +---------------------------------+---------------------------------+ | log_bin | ON | | log_bin_basename | /opt/mysql/logs/mysql-bin | | log_bin_index | /opt/mysql/logs/mysql-bin.index | | log_bin_trust_function_creators | ON | | log_bin_use_v1_row_events | OFF | | sql_log_bin | ON | +---------------------------------+---------------------------------+ 6 rows in set (0.00 sec) mysql> 可以看到log_bin为ON;
3、master的数据库中建立主从同步账号backup:
backup为用户名,192.168.1.%表示只允许192.168.1网段的客户端连接,123456为密码;
mysql> grant replication slave on *.* to 'backup'@'192.168.1.%' identified by '123456'; mysql> select Host,User,authentication_string from mysql.user; +--------------+---------------+-------------------------------------------+ | Host | User | authentication_string | +--------------+---------------+-------------------------------------------+ | localhost | root | *6C362347EBEAA7DF44F6D34884615A35095E80EB | | localhost | mysql.session | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE | | localhost | mysql.sys | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE | | 192.168.1.% | backup | *9BB58B7F11A03B83C396FF506F3DF45727E79614 | +--------------+---------------+-------------------------------------------+ 5 rows in set (0.00 sec) mysql> [email protected]%账户已建立;
4、拷贝数据
重启MySQL服务并设置读取锁定,读取锁定的意思是只能读取,不能更新,以便获得一个一致性的快照;
mysql> show master status; +------------------+----------+--------------+------------------+------------------------------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | +------------------+----------+--------------+------------------+------------------------------------------+ | mysql-bin.000002 | 154 | | mysql | 3f46a487-5984-11e8-8edd-00163e000d73:1-8 | +------------------+----------+--------------+------------------+------------------------------------------+ 1 row in set (0.00 sec) mysql>
查看主服务器上当前的二进制日志名和偏移量值这里的file 和position 要和从上的一致
导出master(192.168.1.222)上的数据,然后导入slave 中
master: [[email protected] ~]# mysqldump -uroot -p employees > /opt/employees.bak [[email protected] ~]# yum install openssh-clients -y #(注:slave也需要安装) slave: [[email protected] ~]# yum install openssh-clients -y [[email protected] ~]# scp /opt/employees.bak [email protected]:/opt/ [email protected]'s password: #输入密码 employees.bak 100%
5、配置slave(192.168.1.233)
[[email protected] ~]# vim /etc/my.cnf [mysqld] basedir=/usr/local/mysql # mysql路径 datadir=/data/mysqldata # mysql数据目录 socket=/tmp/mysql.sock user=mysql port=3306 # slave配置 server_id=2 # MySQLid 后面2个从服务器需设置不同 skip_slave_start=1 # 复制进程不会随着数据库的启动而启动,需手动启动; #加上以下参数可以避免更新不及时,SLAVE 重启后导致的主从复制出错。 read_only = 1 master_info_repository=TABLE relay_log_info_repository=TABLE #relay_log_recovery=1 # 从机禁止写 #super_read_only=1 # 从机禁止写
然后导入到mysql数据库中,slave上的employees数据库不存在则先创建,然后再导入
mysql> create database employees; [[email protected] ~]# mysql -uroot -p -hlocalhost employees < /opt/employees.bak [[email protected] ~]# service mysqld restart #重启数据库 CentOS 6.*: [[email protected] ~]# service mysqld restart [[email protected] ~]# service mysqld status SUCCESS! MySQL running (15604) CentOS 7.*: [[email protected] ~]# systemctl restart mysqld.service [[email protected] ~]# systemctl status mysqld.service mysqld.service - MySQL Server Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled) Active: active (running) since 四 2018-05-17 11:42:02 CST; 2h 5min ago Docs: man:mysqld(8) http://dev.mysql.com/doc/refman/en/using-systemd.html Main PID: 29959 (mysqld) CGroup: /system.slice/mysqld.service └─29959 /opt/mysql/bin/mysqld --defaults-file=/etc/my.cnf 5月 17 11:42:02 tcloud-118 systemd[1]: Started MySQL Server.
登录slave数据库,并做如下设置
[[email protected] ~]# mysql -uroot -p123456 mysql> stop slave; mysql> change master to -> master_host='192.168.1.222', # master的ip -> master_user='backup', # 备份用户名 -> master_password='123456', # 密码 -> master_log_file='mysql-bin.000002', # 上面截图,且要与master的参数一致 -> master_log_pos=154; # 上面截图,且要与master的参数一致 mysql> start slave; mysql> show slave status \G # 查看slave从机的状态
图中2个yes 代表设置成功
6、关闭掉主数据库的读取锁定,并测试
mysql> unlock tables;
在marster中创建一个新表再查看slave中是否有数据
master: mysql> use employees; mysql> create table test001(id int auto_increment primary key,name varchar(20) not null); mysql> insert into test001 values(null,'will'); mysql> insert into test001 values(null,'jim'); mysql> insert into test001 values(null,'tom'); slave: mysql> use employees; mysql> show tables;
有图
mysql> select * from test001;
测试2:重启关闭从数据库,主删除test001表,然后主从数据库都重启看是否正常
mysql> drop table test001;
以上实验证明主从同步成功!!!
以上是关于MySQL 5.7 主从复制(主从同步)的主要内容,如果未能解决你的问题,请参考以下文章