本篇内容主要讲解“MYSQL怎么搭建主从”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“MYSQL怎么搭建主从”吧!
1,在虚拟机上新建三个虚拟机,分别是vm4 192.168.56.4(主), vm2 192.168.56.88(从), vm3 192.168.56.100(从),系统都是redhat 6.5。数据库版本分别是 vm4:mysql 5.7.12, vm2 : mysql 5.7.12, vm3:mysql5.6.23
2,从库也开binlog,不配置log_slave_updates参数(因为会变成主库)
3,按照常规步骤做完后,在从库上show slave status\G,看到Slave_IO_Running进程的状态是Connecting
错误是Last_IO_Error: error connecting to master 'root@192.168.56.4:3306' - retry-time: 30 retries: 1
从这里看不出来什么东西,所以查看错误日志。
错误日志如下
2017-06-22 10:50:27 2063 [ERROR] Slave I/O: error connecting to master 'root@192.168.56.4:3306' - retry-time: 30 retries: 1, Error_code: 2003
2017-06-22 10:50:27 2063 [Warning] Slave SQL: If a crash happens this configuration does not guarantee that the relay log info will be consistent, Error_code: 0
2017-06-22 10:50:27 2063 [Note] Slave SQL thread initialized, starting replication in log 'binlog.000007' at position 450, relay log './vm3-relay-bin.000001' position: 4
发现是slave没有脸上master
在从库使用mysql -h 192.168.56.4 -P 3306 -uroot -p测试,发现确实是连不上master
4,百度错误号Error_code: 2003,
网上说有以下几种可能
1),网络不通
2),密码不对
3),pos不对
4),防火墙没有关
我测试可以ping通,密码也对,pos也对。
5,我的问题是没有关闭防火墙,关闭防火墙后再次测试。
发现报错如下:
[root@vm4 ~]# mysql -h 192.168.56.100 -uroot -p
Enter password:
ERROR 1130 (HY000): Host '192.168.56.4' is not allowed to connect to this MySQL server
原因是账号不允许从远程登录。
6,修改权限使账号可以从远程登录(每个节点都修改)
mysql> select host,user from mysql.user;
+----------------+-----------+
| host | user |
+----------------+-----------+
| 192.168.56.100 | repl |
| 192.168.56.100 | root |
| 192.168.56.88 | repl |
| 192.168.56.88 | root |
| localhost | mysql.sys |
| localhost | root |
+----------------+-----------+
6 rows in set (0.00 sec)
mysql> grant all privileges on *.* to 'root'@'%' identified by 'root' with grant option;
Query OK, 0 rows affected, 1 warning (0.01 sec)
mysql> select host,user from mysql.user;
+----------------+-----------+
| host | user |
+----------------+-----------+
| % | root |
| 192.168.56.100 | repl |
| 192.168.56.100 | root |
| 192.168.56.88 | repl |
| 192.168.56.88 | root |
| localhost | mysql.sys |
| localhost | root |
+----------------+-----------+
7 rows in set (0.00 sec)
7,再次测试,slave_io和slave_sql都是yes状态,没有问题。搭建成功
到此,相信大家对“MYSQL怎么搭建主从”有了更深的了解,不妨来实际操作一番吧!这里是亿速云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!