您当前的位置:首页 > 电脑百科 > 数据库 > MYSQL

CentOS环境下MySQL 5.7主从复制搭建

时间:2019-10-21 17:35:00  来源:  作者:

软件

  1. 数据库: MySQL 5.7
  2. 系统:centos 7.6
CentOS环境下MySQL 5.7主从复制搭建

 

主从复制架构

CentOS环境下MySQL 5.7主从复制搭建

MySQL主从复制

主从复制配置

  • 我创建了两台MySQL服务器分别是Master(IP:172.16.20.199)、Slave(IP:172.16.20.26);
  • 修改Master的MySQL配置:
 [root@localhost ~]# vim /etc/my.cnf
 #将server-id修改为1 默认为1
 #开启blog日志,在server-id上面新增
 # binlog 配置
server-id=1 #注意主从server-id不能一样
log-slave-updates=true #一般用于联机复制中,
log-bin=master-bin #指定binlog日志名
[root@localhost ~]# service mysqld restart
Redirecting to /bin/systemctl restart mysqld.service
CentOS环境下MySQL 5.7主从复制搭建

修改Master的ID号和开启blog日志

  • 修改Slave的MySQL配置:
 [root@localhost ~]# vim /etc/my.cnf
 #将server-id修改为2(默认为1)
 #开启blog日志,在server-id上面新增
 # binlog 配置
server-id=2 #server-id修改为2
log-bin=mysql-bin #slave的二进制日志
relay-log=relay-log-bin #中继日志文件名
relay-log-index=slave-relay-bin.index #中继日志的索引文件名
[root@localhost ~]# service mysqld restart
Redirecting to /bin/systemctl restart mysqld.service
CentOS环境下MySQL 5.7主从复制搭建

修改Slave的ID号和开启blog日志

配置主数据库

  • 登录主数据库
[root@localhost ~]# mysql -u root -p
Enter password: 
Welcome to the MySQL monitor. Commands end with ; or g.
Your MySQL connection id is 2
Server version: 5.7.27 Source distribution
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> 
  • 授权从数据库登录主数据库
mysql> GRANT REPLICATION SLAVE ON *.* TO 'root'@'172.16.20.226' IDENTIFIED BY '111111';
mysql> FLUSH PRIVILEGES;

查询master的状态

mysql> show master status;
+-------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+-------------------+----------+--------------+------------------+-------------------+
| master-bin.000001 | 154 | | | |
+-------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
mysql> 

修改从数据库配置

  • 登录从数据库
[root@localhost ~]# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor. Commands end with ; or g.
Your MySQL connection id is 2
Server version: 5.7.27-log Source distribution
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>
  • 修改master信息
CHANGE MASTER TO
MASTER_HOST='172.16.20.199',
MASTER_USER='root',
MASTER_PASSWORD='111111',
MASTER_LOG_FILE='master-bin.000001',
MASTER_LOG_POS=154;
#配置主节点
mysql> CHANGE MASTER TO
 MASTER_HOST='172.16.20.199',
 MASTER_USER='root',
 MASTER_PASSWORD='111111',
 MASTER_LOG_FILE='master-bin.000001',
 MASTER_LOG_POS=154;
Query OK, 0 rows affected, 2 warnings (0.02 sec)
mysql> start slave;
Query OK, 0 rows affected (0.04 sec)
mysql> show slave status;
+----------------+---------------+-------------+-------------+---------------+-------------------+---------------------+----------------------+---------------+-----------------------+------------------+-------------------+-----------------+---------------------+--------------------+------------------------+-------------------------+-----------------------------+------------+------------+--------------+---------------------+-----------------+-----------------+----------------+---------------+--------------------+--------------------+--------------------+-----------------+-------------------+----------------+-----------------------+-------------------------------+---------------+--------------------------------------------------------------------------------------------------------------------------------------------------------+----------------+----------------+-----------------------------+------------------+-------------+-----------------------------------+-----------+---------------------+--------------------------------------------------------+--------------------+-------------+-------------------------+--------------------------+----------------+--------------------+--------------------+-------------------+---------------+----------------------+--------------+--------------------+
| Slave_IO_State | Master_Host | Master_User | Master_Port | Connect_Retry | Master_Log_File | Read_Master_Log_Pos | Relay_Log_File | Relay_Log_Pos | Relay_Master_Log_File | Slave_IO_Running | Slave_SQL_Running | Replicate_Do_DB | Replicate_Ignore_DB | Replicate_Do_Table | Replicate_Ignore_Table | Replicate_Wild_Do_Table | Replicate_Wild_Ignore_Table | Last_Errno | Last_Error | Skip_Counter | Exec_Master_Log_Pos | Relay_Log_Space | Until_Condition | Until_Log_File | Until_Log_Pos | Master_SSL_Allowed | Master_SSL_CA_File | Master_SSL_CA_Path | Master_SSL_Cert | Master_SSL_Cipher | Master_SSL_Key | Seconds_Behind_Master | Master_SSL_Verify_Server_Cert | Last_IO_Errno | Last_IO_Error | Last_SQL_Errno | Last_SQL_Error | Replicate_Ignore_Server_Ids | Master_Server_Id | Master_UUID | Master_Info_File | SQL_Delay | SQL_Remaining_Delay | Slave_SQL_Running_State | Master_Retry_Count | Master_Bind | Last_IO_Error_Timestamp | Last_SQL_Error_Timestamp | Master_SSL_Crl | Master_SSL_Crlpath | Retrieved_Gtid_Set | Executed_Gtid_Set | Auto_Position | Replicate_Rewrite_DB | Channel_Name | Master_TLS_Version |
+----------------+---------------+-------------+-------------+---------------+-------------------+---------------------+----------------------+---------------+-----------------------+------------------+-------------------+-----------------+---------------------+--------------------+------------------------+-------------------------+-----------------------------+------------+------------+--------------+---------------------+-----------------+-----------------+----------------+---------------+--------------------+--------------------+--------------------+-----------------+-------------------+----------------+-----------------------+-------------------------------+---------------+--------------------------------------------------------------------------------------------------------------------------------------------------------+----------------+----------------+-----------------------------+------------------+-------------+-----------------------------------+-----------+---------------------+--------------------------------------------------------+--------------------+-------------+-------------------------+--------------------------+----------------+--------------------+--------------------+-------------------+---------------+----------------------+--------------+--------------------+
| | 172.16.20.199 | root | 3306 | 60 | master-bin.000001 | 154 | relay-log-bin.000001 | 4 | master-bin.000001 | No | Yes | | | | | | | 0 | | 0 | 154 | 154 | None | | 0 | No | | | | | | NULL | No | 1593 | 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. | 0 | | | 1 | | /usr/local/mysql/data/master.info | 0 | NULL | Slave has read all relay log; waiting for more updates | 86400 | | 191021 04:59:03 | | | | | | 0 | | | |
+----------------+---------------+-------------+-------------+---------------+-------------------+---------------------+----------------------+---------------+-----------------------+------------------+-------------------+-----------------+---------------------+--------------------+------------------------+-------------------------+-----------------------------+------------+------------+--------------+---------------------+-----------------+-----------------+----------------+---------------+--------------------+--------------------+--------------------+-----------------+-------------------+----------------+-----------------------+-------------------------------+---------------+--------------------------------------------------------------------------------------------------------------------------------------------------------+----------------+----------------+-----------------------------+------------------+-------------+-----------------------------------+-----------+---------------------+--------------------------------------------------------+--------------------+-------------+-------------------------+--------------------------+----------------+--------------------+--------------------+-------------------+---------------+----------------------+--------------+--------------------+
1 row in set (0.00 sec)
mysql>

找到字段Slave_IO_Running和Slave_SQL_Running,如果Slave_IO_Running=找到字段Slave_IO_Running和Slave_SQL_Running,如果Slave_IO_Running=No

Slave_SQL_Running=Yes

当前Slave_IO_Running:值为No,向下查看错误信息:

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.

修改auto.cnf中的UUID值,随意更改一个值即可。

[root@localhost ~]# vim /usr/local/mysql/data/auto.cnf
server-uuid=修改一下
[root@localhost ~]# systemctl restart mysqld
 再次登录MySQL查看从节点信息
Slave_SQL_Running 和Slave_IO_Running值是否为Yes状态,表示配置成功

验证从是否同步数据

  • 在主服务器上新建数据库test
mysql> create database test;
Query OK, 1 row affected (0.01 sec)
mysql> use test;
Database changed
  • 创建test表,并插入数据
mysql> mysql> CREATE TABLE test
 -> (
 -> id int,
 -> name varchar(255)
 -> )
 -> ;
Query OK, 0 rows affected (0.06 sec)
mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| test |
+----------------+
1 row in set (0.00 sec)
  • 在slave上查看是否更新数据
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| test |
+--------------------+
5 rows in set (0.03 sec)
mysql> use test;
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_test |
+----------------+
| test |
+----------------+
1 row in set (0.00 sec)
mysql> 

恭喜主从复制配置成功!!!



Tags:MySQL 5.7   点击:()  评论:()
声明:本站部分内容及图片来自互联网,转载是出于传递更多信息之目的,内容观点仅代表作者本人,如有任何标注错误或版权侵犯请与我们联系(Email:2595517585@qq.com),我们将及时更正、删除,谢谢。
▌相关推荐
MySQL 5.7已经推出多年了,各家针对不同版本也梳理了不同的参数模板。在实践的过程中,也发现了一些潜在的问题,有些参数开始的时候没有注意到,结果想开启的时候发现是只读变量,要...【详细内容】
2021-03-01  Tags: MySQL 5.7  点击:(161)  评论:(0)  加入收藏
场景说明要连接到某服务的数据库,但是忘记了root密码,试了几次都无法登陆 找回密码步骤1.在/etc/my.cnf [mysqld] 配置部分添加"skip-grant-tables"vim /etc/my.cnf[mysqld]sk...【详细内容】
2021-02-08  Tags: MySQL 5.7  点击:(162)  评论:(0)  加入收藏
本文详述MySQL 5.7 模拟update误操作后进行数据恢复的全过程,希望对大家有帮助。...【详细内容】
2020-06-24  Tags: MySQL 5.7  点击:(53)  评论:(0)  加入收藏
软件 数据库: MySQL 5.7 系统:CentOS 7.6 主从复制架构MySQL主从复制主从复制配置 我创建了两台MySQL服务器分别是Master(IP:172.16.20.199)、Slave(IP:172.16.20.26); 修改Master...【详细内容】
2019-10-21  Tags: MySQL 5.7  点击:(97)  评论:(0)  加入收藏
虽然我们经常在较大规模的系统上运行 MySQL ,但我们常常在最小的云实例上运行 MySQL,或者只在我们的笔记本电脑上运行它。在这些情况下,MySQL 8 和 MySQL 5.7 的内存消耗非常重要。...【详细内容】
2019-08-22  Tags: MySQL 5.7  点击:(811)  评论:(0)  加入收藏
▌简易百科推荐
作者:雷文霆 爱可生华东交付服务部 DBA 成员,主要负责Mysql故障处理及相关技术支持。爱好看书,电影。座右铭,每一个不曾起舞的日子,都是对生命的辜负。 本文来源:原创投稿 *爱可生...【详细内容】
2021-12-24  爱可生    Tags:MySQL   点击:(7)  评论:(0)  加入收藏
生成间隙(gap)锁、临键(next-key)锁的前提条件 是在 RR 隔离级别下。有关Mysql记录锁、间隙(gap)锁、临键锁(next-key)锁的一些理论知识之前有写过,详细内容可以看这篇文章...【详细内容】
2021-12-14  python数据分析    Tags:MySQL记录锁   点击:(18)  评论:(0)  加入收藏
binlog 基本认识 MySQL的二进制日志可以说是MySQL最重要的日志了,它记录了所有的DDL和DML(除了数据查询语句)语句,以事件形式记录,还包含语句所执行的消耗的时间,MySQL的二...【详细内容】
2021-12-14  linux上的码农    Tags:mysql   点击:(13)  评论:(0)  加入收藏
为查询优化你的查询 大多数的MySQL服务器都开启了查询缓存。这是提高性最有效的方法之一,而且这是被MySQL的数据库引擎处理的。当有很多相同的查询被执行了多次的时候,这些查...【详细内容】
2021-12-09  元宇宙iwemeta    Tags:mysql   点击:(15)  评论:(0)  加入收藏
测试的目的和原因,公司有很多程序员,每个程序员对数据库和表结构都有自己的理解。而且每个程序员的理解往往是以效率考虑。既然都是为了效率考虑,那么我就来测试一下究竟哪种使...【详细内容】
2021-12-08  吴彬的分享    Tags:Mysql数据库   点击:(14)  评论:(0)  加入收藏
当你们考虑项目并发的时候,我在部署环境,当你们在纠结使用ArrayList还是LinkedArrayList的时候,我还是在部署环境。所以啊,技术不止境,我在部环境。今天这篇文章缕一下在同一台服...【详细内容】
2021-12-08  秃头码哥    Tags:MySQL数据库   点击:(17)  评论:(0)  加入收藏
对于数据分析来说,MySQL使用最多的是查询,比如对数据进行排序、分组、去重、汇总及字符串匹配等,如果查询的数据涉及多个表,还需要要对表进行连接,本文就来说说MySQL中常用的查询...【详细内容】
2021-12-06  笨鸟学数据分析    Tags:MySQL   点击:(21)  评论:(0)  加入收藏
在学习SQL语句之前,首先需要区分几个概念,我们常说的数据库是指数据库软件,例如MySQL、Oracle、SQL Server等,而本文提到的数据库是指数据库软件中的一个个用于存储数据的容器。...【详细内容】
2021-11-24  笨鸟学数据分析    Tags:SQL语句   点击:(23)  评论:(0)  加入收藏
概述以前参加过一个库存系统,由于其业务复杂性,搞了很多个应用来支撑。这样的话一份库存数据就有可能同时有多个应用来修改库存数据。比如说,有定时任务域xx.cron,和SystemA域...【详细内容】
2021-11-05  Java云海    Tags:分布式锁   点击:(32)  评论:(0)  加入收藏
MySQL的进阶查询 一、 按关键字排序 使用ORDERBY语句来实现排序排序可针对一个或多个字段ASC:升序,默认排序方式 【升序是从小到大】DESC:降序 【降序是从大到小】ORDER BY的...【详细内容】
2021-11-05  Java热点    Tags:SQL语句   点击:(28)  评论:(0)  加入收藏
最新更新
栏目热门
栏目头条