参考:https://www.cnblogs.com/kevingrace/p/6256603.html
参考:https://www.cnblogs.com/kevingrace/p/10228694.html
参考:http://www.cnblogs.com/gl-developer/p/6170423.html
环境描述:
CentOS7.5
master(node1):10.0.3.49
slave(node2):10.0.3.55
注意事项:
1.时间同步
2.网络畅通,能互相ping到
3.数据库授权对方连接
4.关闭selinuxx
5.同步前,双方数据库数据要一致
一、安装数据库(都要)
[root@node2 ~]# yum install yum-utils -y
[root@node2 ~]# rpm -ivh https://dev.mysql.com/get/mysql80-community-release-el7-1.noarch.rpm
[root@node2 ~]# yum-config-manager --disable mysql80-community
[root@node2 ~]# yum-config-manager --enable mysql57-community
[root@node2 ~]# yum install mysql-community-server -y
启动mysql服务,修改初始密码
[root@node2 ~]# grep password /var/log/mysqld.log
2019-01-08T08:42:53.031646Z 1 [Note] A temporary password is generated for root@localhost: y0oagT8rqV<g
[root@node2 ~]# mysql -uroot -p'y0oagT8rqV<g'
mysql> set global validate_password_policy=0;
Query OK, 0 rows affected (0.00 sec)
mysql> set global validate_password_length=1;
Query OK, 0 rows affected (0.00 sec)
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY '123456';
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges
-> ;
Query OK, 0 rows affected (0.00 sec)
二、主数据库的操作(node1机器)
修改my.cnf,加上下面几行
server-id=1 #数据库唯一ID,主从的标识号绝对不能重复
log-bin=mysql-bin #开启二进制日志
binlog-do-db=huanqiu #只同步huanqiu数据库
binlog-ignore-db=mysql #不同步mysql库
#以下几行可用没有,网上资料有上面两行即可
#binlog-do-db=huanqiu #需要同步的数据库。如果是多个同步库,就以此格式另写几行即可。如果不指明对某个具体库同步,就去掉此行,表示同步所有库(除了ignore忽略的库)。
#binlog-ignore-db=mysql #不同步mysql系统数据库。如果是多个不同步库,就以此格式另写几行;也可以在一行,中间逗号隔开。
#sync_binlog = 1 #确保binlog日志写入后与硬盘同步
#binlog_checksum = none #跳过现有的采用checksum的事件,mysql5.6.5以后的版本中binlog_checksum=crc32,而低版本都是binlog_checksum=none
#binlog_format = mixed #bin-log日志文件格式,设置为MIXED可以防止主键重复。
重启数据库
[root@node1 ~]# systemctl restart mysqld
添加数据库,插入两条数据:
[root@node1 ~]# mysql -uroot -p123456
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> CREATE DATABASE huanqiu CHARACTER SET utf8 COLLATE utf8_general_ci;
Query OK, 1 row affected (0.00 sec)
mysql> use huanqiu;
Database changed
mysql> create table if not exists haha (id int(10) PRIMARY KEY AUTO_INCREMENT,name varchar(50) NOT NULL);
Query OK, 0 rows affected (0.01 sec)
mysql> insert into huanqiu.haha values(1,"wangshibo"),(2,"guohuihui");
Query OK, 2 rows affected (0.04 sec)
Records: 2 Duplicates: 0 Warnings: 0
mysql> select * from huanqiu.haha;
+----+-----------+
| id | name |
+----+-----------+
| 1 | wangshibo |
| 2 | guohuihui |
+----+-----------+
2 rows in set (0.00 sec)
导出数据库,同步到slave机器上:
在导出数据之前,看看呀先在数据库中执行flush tables with read lock;数据库只读锁定,保证双方在同步环境实现前的数据一致
在从数据库配置好后再使用unlock tables解锁
[root@node1 ~]# mysqldump -uroot huanqiu -p > /opt/huanqiu.sql
[root@node1 ~]# rsync -e "ssh -p22" -avz /opt/huanqiu.sql 10.0.3.55:/opt/
添加用于同步数据的账号,设置数据同步权限
mysql> CREATE USER slave@'10.0.3.55' IDENTIFIED BY 'slavepass';
Query OK, 0 rows affected (0.00 sec)
mysql> grant replication slave,replication client on *.* to slave@'10.0.3.55';
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)
mysql> show grants for slave@'10.0.3.55';
+---------------------------------------------------------------------------+
| Grants for [email protected] |
+---------------------------------------------------------------------------+
| GRANT REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO 'slave'@'10.0.3.55' |
+---------------------------------------------------------------------------+
1 row in set (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
查看主服务器的master状态,待会儿要用到:
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000003 | 1927 | huanqiu | mysql | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
三、在slave数据库上的 操作(node2)
修改my.cnf ,记得重启数据库
[root@node2 ~]# vim /etc/my.cnf
...
server-id=2 #数据库唯一ID,不能和朱数据库一样
log-bin=mysql-bin #开启二进制日志
replicate-do-db=huanqiu #需要同步的数据库名
replicate-ignore-db=mysql #不同步的数据库
slave-skip-errors=all #跳过所有的错误,继续执行复制操作
[root@node2 ~]# systemctl restart mysqld
创建数据库并导入数据库文件
[root@node2 ~]# mysql -uroot -p123456
mysql> create database huanqiu character set utf8 collate utf8_general_ci;
Query OK, 1 row affected (0.00 sec)
mysql> use huanqiu;
Database changed
mysql> source /opt/huanqiu.sql
mysql> select * from huanqiu.haha;
+----+-----------+
| id | name |
+----+-----------+
| 1 | wangshibo |
| 2 | guohuihui |
+----+-----------+
2 rows in set (0.00 sec)
配置主从同步命令:
执行同步之前要先关闭slave,执行完主同步指令之后再开启slave,再查看slave状态显示IO和SQL线程状态均为YES时,表示主从已经实现同步
mysql> stop slave;
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> change master to master_host='10.0.3.49',master_user='slave',master_password='slavepass',master_log_file='mysql-bin.000003',master_log_pos=1927;
Query OK, 0 rows affected, 2 warnings (0.02 sec)
mysql> start slave;
Query OK, 0 rows affected (0.00 sec)
mysql> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 10.0.3.49
Master_User: slave
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000003
Read_Master_Log_Pos: 1927
Relay_Log_File: node2-relay-bin.000002
Relay_Log_Pos: 320
Relay_Master_Log_File: mysql-bin.000003
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB: huanqiu
Replicate_Ignore_DB: mysql
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 1927
Relay_Log_Space: 527
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 1
Master_UUID: 0e21b00b-1320-11e9-9f26-000c29cfbe2f
Master_Info_File: /var/lib/mysql/master.info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set:
Executed_Gtid_Set:
Auto_Position: 0
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:
1 row in set (0.00 sec)
ERROR:
No query specified
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| huanqiu |
| mysql |
| performance_schema |
| sys |
+--------------------+
5 rows in set (0.00 sec)
四、测试
在主数据库上插入数据
mysql> insert into huanqiu.haha values(100,"shanxi");
Query OK, 1 row affected (0.00 sec)
在从数据库上查看:
mysql> select * from huanqiu.haha;
+-----+-----------+
| id | name |
+-----+-----------+
| 1 | wangshibo |
| 2 | guohuihui |
| 100 | shanxi |
+-----+-----------+
3 rows in set (0.00 sec)
下面是mysql主主模式的操作:
原理不说了,看大佬的博客去:https://www.cnblogs.com/kevingrace/p/6256603.html
一、主服务器的操作:
修改my,cnf并重启服务,相比主从模式多了最后两行
[root@node1 ~]# vim /etc/my.cnf
server-id=1 #数据库唯一ID,主从的标识号绝对不能重复
log-bin=mysql-bin #开启二进制日志
binlog-do-db=huanqiu #只同步huanqiu数据库
binlog-ignore-db=mysql #不同步mysql库
auto-increment_increment=2 #设置每次增长的量为2
auto-increment_offset=1 #主服务器的初始偏移量为1
[root@node1 ~]# systemctl restart mysqld
数据库同步授权,保证对方对方机器能连到本机
授权node2能连到本机的数据库,做好防火墙规则:
mysql> CREATE USER slave@'10.0.3.55' IDENTIFIED BY 'slavepass';
Query OK, 0 rows affected (0.00 sec)
mysql> grant replication slave,replication client on *.* to slave@'10.0.3.55';
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)
mysql> show grants for slave@'10.0.3.55';
+---------------------------------------------------------------------------+
| Grants for [email protected] |
+---------------------------------------------------------------------------+
| GRANT REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO 'slave'@'10.0.3.55' |
+---------------------------------------------------------------------------+
1 row in set (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
最好将数据库锁住,仅仅允许读,保证数据一致性,等主主环境部署后再解锁
mysql> flush tables with read lock;
查看master状态,待会儿要用:
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000004 | 154 | huanqiu | mysql | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
二、node2的操作
修改配置文件my.cnf
[root@node2 ~]# vim /etc/my.cnf
server-id=2 #数据库唯一ID,不能和朱数据库一样
log-bin=mysql-bin #开启二进制日志
replicate-do-db=huanqiu #需要同步的数据库名
replicate-ignore-db=mysql #不同步的数据库
slave-skip-errors=all #跳过所有的错误,继续执行复制操作
auto-increment-increment=2 #自增长为2
auto-increment-offset=2 #初始偏移量为2,另一个数据库是1,刚好错开
[root@node2 ~]# systemctl restart mysqld
数据库授权:
[root@node2 ~]# mysql -uroot -p123456
mysql> create user slave@'10.0.3.49' identified by 'slavepass';
mysql> show grants for slave@'10.0.3.49';
+---------------------------------------------------------------------------+
| Grants for [email protected] |
+---------------------------------------------------------------------------+
| GRANT REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO 'slave'@'10.0.3.49' |
+---------------------------------------------------------------------------+
1 row in set (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)
查看这个主节点的状态:
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000002 | 957 | | | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
三、执行主主同步操作
现在node2上进行(server-id为2):
现在slave数据库上同步master的设置
mysql> unlock tables;
Query OK, 0 rows affected (0.00 sec)
mysql> stop slave;
Query OK, 0 rows affected (0.01 sec)
mysql> change master to master_host='10.0.3.49',master_user='slave',master_password='slavepass',master_log_file='mysql-bin.000004',master_log_pos=154;
mysql> start slave;
Query OK, 0 rows affected (0.01 sec)
mysql> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 10.0.3.49
Master_User: slave
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000004
Read_Master_Log_Pos: 154
Relay_Log_File: node2-relay-bin.000007
Relay_Log_Pos: 320
Relay_Master_Log_File: mysql-bin.000004
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB: huanqiu
Replicate_Ignore_DB: mysql
再在master上做同步slave的设置:
mysql> unlock tables;
Query OK, 0 rows affected (0.00 sec)
mysql> stop slave;
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> change master to master_host='10.0.3.55',master_user='slave',master_password='slavepass',master_log_file='mysql-bin.000002',master_log_pos=957;
Query OK, 0 rows affected, 2 warnings (0.02 sec)
mysql> start slave;
Query OK, 0 rows affected (0.00 sec)
mysql> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 10.0.3.55
Master_User: slave
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000002
Read_Master_Log_Pos: 957
Relay_Log_File: node1-relay-bin.000002
Relay_Log_Pos: 320
Relay_Master_Log_File: mysql-bin.000002
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
IO和SQL线程显示YES表示操作没问题
四、测试
node1上增加数据:
mysql> select * from huanqiu.haha;
+-----+-----------+
| id | name |
+-----+-----------+
| 1 | wangshibo |
| 2 | guohuihui |
| 100 | shanxi |
+-----+-----------+
3 rows in set (0.03 sec)
mysql> insert into huanqiu.haha values(15,"tangquan");
Query OK, 1 row affected (0.00 sec)
node2上查看:
mysql> select * from huanqiu.haha;
+-----+-----------+
| id | name |
+-----+-----------+
| 1 | wangshibo |
| 2 | guohuihui |
| 15 | tangquan |
| 100 | shanxi |
+-----+-----------+
4 rows in set (0.00 sec)
node2上删除数据:
mysql> delete from huanqiu.haha where id=100;
Query OK, 1 row affected (0.00 sec)
mysql> select * from huanqiu.haha;
+----+-----------+
| id | name |
+----+-----------+
| 1 | wangshibo |
| 2 | guohuihui |
| 15 | tangquan |
+----+-----------+
3 rows in set (0.00 sec)
node1上查看:
mysql> select * from huanqiu.haha;
+----+-----------+
| id | name |
+----+-----------+
| 1 | wangshibo |
| 2 | guohuihui |
| 15 | tangquan |
+----+-----------+
3 rows in set (0.00 sec)
到此结束,文章一大篇,看起来很头疼,按照教程一步一步做完,其实很简单...
再次感谢散尽浮华博主