关于mysql的主从复制原理,网上教程较多,不再赘述!
主要步骤就是:
1.主服务器开启mysql的bin-log日志和添加server-id来区分服务器节点, 在配置文件/etc/my.cnf里面配置
2.从服务器的服务节点配置,至于bin-log日志可以开启,也可以选择不开启
3.创建一个从服务器用来可以连接的用户rep1,名称随意,授权replication slave权限,远程登录ip根据需要修改。
注意:网上流传的创建语法
不知是数据库版本问题,还是语法更新的缘故,亲测不成功!
8.0.11之后移除了grant 添加用户的功能,也就是说不能通过 grant all …… 来添加一个 host 为 % 的root账号了。
mysql> grant replication slave on *.* to ‘rep1’@‘%’ identified by ‘123456’;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '%’ identified by ‘123456’' at line 1
建议分两步走: 先创建用户并设置密码,再授权
mysql> grant replication slave on *.* to ‘rep1’@‘%’ identified by ‘123456’;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '%’ identified by ‘123456’' at line 1
mysql> create user 'rep1' identified by '123456';
Query OK, 0 rows affected (0.03 sec)
mysql> grant replication slave on *.* to 'rep1'@'%';
Query OK, 0 rows affected (0.01 sec)
#授予所有权限,方便从服务器的数据库客户端使用该账号远程登录主服务器的数据库
mysql> grant all on *.* to rep1@'%';
Query OK, 0 rows affected (0.01 sec)
可以在从服务器上进行测试,看能否用该用户,登录主服务器的数据库,语法:
[root@oracle mysql]# mysql -urep1 -p123456 -h192.168.197.134
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 38
Server version: 8.0.15 MySQL Community Server - GPL
Copyright (c) 2000, 2019, 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>
4.登录从服务器,同步配置参数
先在主服务器上执行show master status,查看主数据库信息
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000003 | 3058 | | | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
mysql>
登录从服务器的数据库,执行
mysql> change master to
-> master_host='192.168.197.134',
-> master_port=3306,
-> master_user='rep1',
-> master_password='123456',
-> master_log_file='mysql-bin.000003',
-> master_log_pos=3058;
Query OK, 0 rows affected, 1 warning (0.02 sec)
mysql>
具体含义如下,借用一张图
在从数据库上开启slave进程,命令start slave
观察slave进程的状态,判定是否搭建成功
mysql> start slave;
Query OK, 0 rows affected (0.01 sec)
mysql> show slave status \G
5.主从复制的的验证
注意: 如果此前没有将主数据库的数据导入到从数据库,即两边数据库的内容不一致,不要在数据库执行删除操作,或者进入从数据库不存在的database,进行操作,否则从库在进行读取bin-log日志的时候会因为没有找到这些信息而报错,会中止slave进程!
在主数据库创建一个数据库,进入该库,创建一个表,插入几条数据
观察从数据库,有没有同步进行这些操作,结果就是从服务器上的数据库里面会自动同步这些数据
主数据库上执行:
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| test |
+--------------------+
5 rows in set (0.01 sec)
mysql> create database xeshi;
Query OK, 1 row affected (0.05 sec)
mysql> use xeshi;
Database changed
mysql> create table test1
-> (id int(30),
-> name varchar(20),
-> age int(5),
-> data datetime
-> );
Query OK, 0 rows affected (0.03 sec)
mysql> insert into test1 values(001,'xiehao',24,null);
Query OK, 1 row affected (0.04 sec)
mysql> insert into test1 values(002,'zhangsan',25,null);
Query OK, 1 row affected (0.01 sec)
mysql> select * from test1;
+------+----------+------+------+
| id | name | age | data |
+------+----------+------+------+
| 1 | xiehao | 24 | NULL |
| 2 | zhangsan | 25 | NULL |
+------+----------+------+------+
2 rows in set (0.00 sec)
mysql>
从数据库上进行查看,数据有没有同步:
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| test |
+--------------------+
5 rows in set (0.00 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| test |
| xeshi |
+--------------------+
6 rows in set (0.00 sec)
mysql> use xeshi;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> show tables;
+-----------------+
| Tables_in_xeshi |
+-----------------+
| test1 |
+-----------------+
1 row in set (0.00 sec)
mysql> select * from test1;
+------+----------+------+------+
| id | name | age | data |
+------+----------+------+------+
| 1 | xiehao | 24 | NULL |
| 2 | zhangsan | 25 | NULL |
+------+----------+------+------+
2 rows in set (0.00 sec)
mysql>
至此,主从复制已经搭建完成!!!!!!!!
6.过程中遇到的坑
6.1.mysql重启失败
原因:配置文件/etc/my.cnf的内容以及权限问题744最好,前一篇博客有记录该问题
6.2.主数据库授权replication slave时创建用户语法报错
原因:8.0.11之后移除了grant 添加用户的功能
解决办法:先创建用户,再授权,本篇文章中间有详细描述
6.3.从服务器,启动完成slave进程之后,观察slave状态,发现
原因: 可以查看show slave status \G状态下面有,报错原因:
Last_IO_Error: Fatal error: The slave I/O thread stops because master and slave have equal MySQL server UUIDs; these UUIDs must be different for replication to work." 这个错误提示
原因就是我的 从服务器 上的mysql数据库,是直接从 主服务器 上拷贝过来的。
解决办法: 参考 https://www.cnblogs.com/datalife/p/8026928.html
最近在部署MySQL主从复制架构的时候,碰到了"Last_IO_Error: Fatal error: The slave I/O thread stops because master and slave have equal MySQL server UUIDs; these UUIDs must be different for replication to work." 这个错误提示。即主从架构中使用了相同的UUID。检查server_id系统变量,已经是不同的设置,那原因是?接下来为具体描述。
master_mysql> show variables like 'server_id';
slave_mysql> show variables like 'server_id';
查看是不同的。
但是查看/mysql/data/auto.cnf发现里面的UUID是哦相同的。原因是mysql是直接从节点1上拷贝过来而导致。
解决:mv /mysql/data/auto.cnf /mysql/data/auto.cnf.bak 重启mysql解决
6.4其他各种错误代码及原因,可以参考该文章
https://blog.51cto.com/wujianwei/1934135 较为详细