CentOS搭建MySQL主从复制,读写分离

MySQL主从复制的优点:
1、 如果主服务器出现问题, 可以快速切换到从服务器提供的服务,保证高可用性
2、 可以在从服务器上执行查询操作, 降低主服务器的访问压力
3、 可以在从服务器上执行备份, 以避免备份期间影响主服务器的服务

注意事项:
1、server-id必须唯一,一般使用ip的后三位
2、从库Slave_IO_Running:NO 可能原因:帐号无权限操作
3、Can’t execute the query because you have a conflicting read lock,解锁下即可 unlock tables;
4、一般只有更新不频繁的数据或者对实时性要求不高的数据可以通过从服务器查询, 实时性要求高的数据仍然需要从主数据库获得
5、修改完主从服务器的配置需要重启mysql:service mysqld restart

主机A: 192.168.10.111
从机B: 192.168.10.124

请先分别安装mysql,版本需一致,装了即可跳过

yum install mysql mysql-server #输入y即可自动安装,直到安装完成

1、先登录主机 A,在主服务器上,设置一个从数据库的账户,使用REPLICATION SLAVE(从复制)赋予权限,如:
mysql>GRANT REPLICATION SLAVE ON *.* TO ‘backup’@’192.168.10.124’ IDENTIFIED BY ‘123456’
赋予从机权限,有多台从机,就执行多次。
mysql>flush privileges;

2、 打开主机A的my.cnf,输入如下:(修改主数据库的配置文件my.cnf,开启BINLOG,并设置server-id的值,修改之后必须重启mysql服务)

server-id               = 1    #主机标示,整数
log_bin                 = /var/log/mysql/mysql-bin.log   #确保此文件可写,开启bin-log
read-only              =0  #主机,读写都可以
binlog-do-db         =test   #需要备份数据,多个写多行
binlog-ignore-db    =mysql #不需要备份的数据库,多个写多行
可以通过mysql>show variables like 'log_%'; 验证二进制日志是否已经启动。

3、现在可以停止主数据的的更新操作,并生成主数据库的备份,我们可以通过mysqldump到处数据到从数据库,当然了,你也可以直接用cp命令将数据文件复制到从数据库去,注意在导出数据之前先对主数据库进行READ LOCK,以保证数据的一致性
mysql> flush tables with read lock;
Query OK, 0 rows affected (0.19 sec)

然后mysqldump导出数据:
mysqldump -h127.0.0.1 -p3306 -uroot -p test > /data/backup/test.sql

4、得到主服务器当前二进制日志名和偏移量,这个操作的目的是为了在从数据库启动后,从这个点开始进行数据的恢复。
mysql> show master status;
+——————+———-+————–+——————+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+——————+———-+————–+——————+
| mysql-bin.000007 |      517 | test         | mysql            |
+——————+———-+————–+——————+
1 row in set (0.00 sec)

最好在主数据库备份完毕,恢复写操作。
mysql> unlock tables;
Query OK, 0 rows affected (0.28 sec)

5、将刚才主数据备份的test.sql复制到从数据库(navicat、phpmyadmin、命令行都可以),进行导入。

6、修改从数据库的my.cnf,增加server-id参数,指定复制使用的用户,主数据库服务器的ip,端口以及开始执行复制日志的文件和位置。打开从机B的my.cnf,输入(修改之后必须重启mysql服务)

server-id       = 2
log_bin         = /var/log/mysql/mysql-bin.log
master-host     =192.168.10.111
master-user     =backup
master-pass     =123456
master-port     =3306
master-connect-retry=60 #如果从服务器发现主服务器断掉,重新连接的时间差(秒)
replicate-do-db =test #只复制某个库
replicate-ignore-db=mysql #不复制某个库

7、在从服务器上,启动slave进程

mysql> start slave;

8、在从服务器进行show salve status验证

mysql> SHOW SLAVE STATUS\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.10.111
                  Master_User: root
                  Master_Port: 3306
                Connect_Retry: 5
              Master_Log_File: mysql-bin.000007
          Read_Master_Log_Pos: 263
               Relay_Log_File: mysqld-relay-bin.000002
                Relay_Log_Pos: 408
        Relay_Master_Log_File: mysql-bin.000007
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB: test
          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: 263
              Relay_Log_Space: 564
              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:
1 row in set (0.00 sec)

提示
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
说明配置成功了

9、测试主从服务器是否能同步 
插入 修改 删除 增加字段 修改字段 增加表自己测试都可以

[linuxidc@server22 ~]$mysql -uroot -p123456
mysql> create database test;
mysql> create table user(id int);
mysql> insert into user values(1),(2),(3),(4),(5),(6);
Query OK, 2 rows affected (0.00 sec)
Records: 2  Duplicates: 0  Warnings: 0

mysql> select * from user;
+----+
| id |
+----+
|  1 |
|  2 |
+----+
2rows in set (0.00 sec)


mysql> select * from user;
+----+
| id |
+----+
|  1 |
|  2 |
|  3 |
|  4 |
|  5 |
|  6 |
+----+
6 rows in set (0.00 sec)


mysql> update user set id=11 where id=1;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0


mysql> select * from user;
+----+
| id |
+----+
| 11 |
|  2 |
|  3 |
|  4 |
|  5 |
|  6 |
+----+
6 rows in set (0.00 sec)


mysql> delete from user where id=2;
Query OK, 1 row affected (0.00 sec)


mysql> select * from user;
+----+
| id |
+----+
| 11 |
|  3 |
|  4 |
|  5 |
|  6 |
+----+
5 rows in set (0.00 sec)


mysql> alter table user add name varchar(50);
Query OK, 5 rows affected (0.01 sec)
Records: 5  Duplicates: 0  Warnings: 0


mysql> select * from user;
+----+------+
| id | name |
+----+------+
| 11 | NULL |
|  3 | NULL |
|  4 | NULL |
|  5 | NULL |
|  6 | NULL |
+----+------+
5 rows in set (0.00 sec)
mysql> ALTER TABLE user  MODIFY COLUMN name VARCHAR(200);
Query OK, 5 rows affected (0.01 sec)
Records: 5  Duplicates: 0  Warnings: 0


mysql> desc user;
+-------+--------------+------+-----+---------+-------+
| Field | Type         | Null | Key | Default | Extra |
+-------+--------------+------+-----+---------+-------+
| id    | int(11)      | NO   |     | NULL    |       |
| name  | varchar(200) | YES  |     | NULL    |       |
+-------+--------------+------+-----+---------+-------+
2 rows in set (0.00 sec)

mysql> create table user2(id int);
Query OK, 0 rows affected (0.01 sec)

mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| test           |
| user           |
| user2          |
+----------------+
3 rows in set (0.00 sec)
mysql>

在从服务器查看是否同步过来 如果一致说明成功

mysql> use test;
Database changed
mysql> select * from user;
+----+
| id |
+----+
|  1 |
|  2 |
|  3 |
|  4 |
|  5 |
|  6 |
+----+
2 rows in set (0.00 sec)

mysql> select * from user;
+----+
| id |
+----+
|  1 |
|  2 |
|  3 |
|  4 |
|  5 |
|  6 |
+----+
6 rows in set (0.00 sec)

mysql> select * from user;
+----+
| id |
+----+
| 11 |
|  2 |
|  3 |
|  4 |
|  5 |
|  6 |
+----+
6 rows in set (0.00 sec)

mysql> select * from user;
+----+
| id |
+----+
| 11 |
|  3 |
|  4 |
|  5 |
|  6 |
+----+
5 rows in set (0.00 sec)

mysql> select * from user;
+----+------+
| id | name |
+----+------+
| 11 | NULL |
|  3 | NULL |
|  4 | NULL |
|  5 | NULL |
|  6 | NULL |
+----+------+
5 rows in set (0.00 sec)
mysql> desc user;
+-------+--------------+------+-----+---------+-------+
| Field | Type         | Null | Key | Default | Extra |
+-------+--------------+------+-----+---------+-------+
| id    | int(11)      | NO   |     | NULL    |       |
| name  | varchar(200) | YES  |     | NULL    |       |
+-------+--------------+------+-----+---------+-------+
2 rows in set (0.00 sec)

mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| test           |
| user           |
| user2          |
+----------------+
3 rows in set (0.00 sec)

mysql>

mysql binlog日志查看: 

show binlog events\G;

*************************** 12. row ***************************
   Log_name: mysql-bin.000007
        Pos: 985
 Event_type: Query
  Server_id: 1
End_log_pos: 1075
       Info: use `test`; delete from user where id=2
*************************** 13. row ***************************
   Log_name: mysql-bin.000007
        Pos: 1075
 Event_type: Query
  Server_id: 1
End_log_pos: 1175
       Info: use `test`; alter table user add name varchar(50)
*************************** 14. row ***************************
   Log_name: mysql-bin.000007
        Pos: 1175
 Event_type: Query
  Server_id: 1
End_log_pos: 1287
       Info: use `test`; ALTER TABLE user  MODIFY COLUMN name VARCHAR(200)
*************************** 15. row ***************************
   Log_name: mysql-bin.000007
        Pos: 1287
 Event_type: Query
  Server_id: 1
End_log_pos: 1376
       Info: use `test`; create table user2(id int)
15 rows in set (0.00 sec)

文章来源网络,作者:运维,如若转载,请注明出处:https://shuyeidc.com/wp/221040.html<

(0)
运维的头像运维
上一篇2025-04-14 20:37
下一篇 2025-04-14 20:39

相关推荐

  • 个人主题怎么制作?

    制作个人主题是一个将个人风格、兴趣或专业领域转化为视觉化或结构化内容的过程,无论是用于个人博客、作品集、社交媒体账号还是品牌形象,核心都是围绕“个人特色”展开,以下从定位、内容规划、视觉设计、技术实现四个维度,详细拆解制作个人主题的完整流程,明确主题定位:找到个人特色的核心主题定位是所有工作的起点,需要先回答……

    2025-11-20
    0
  • 社群营销管理关键是什么?

    社群营销的核心在于通过建立有温度、有价值、有归属感的社群,实现用户留存、转化和品牌传播,其管理需贯穿“目标定位-内容运营-用户互动-数据驱动-风险控制”全流程,以下从五个维度展开详细说明:明确社群定位与目标社群管理的首要任务是精准定位,需明确社群的核心价值(如行业交流、产品使用指导、兴趣分享等)、目标用户画像……

    2025-11-20
    0
  • 香港公司网站备案需要什么材料?

    香港公司进行网站备案是一个涉及多部门协调、流程相对严谨的过程,尤其需兼顾中国内地与香港两地的监管要求,由于香港公司注册地与中国内地不同,其网站若主要服务内地用户或使用内地服务器,需根据服务器位置、网站内容性质等,选择对应的备案路径(如工信部ICP备案或公安备案),以下从备案主体资格、流程步骤、材料准备、注意事项……

    2025-11-20
    0
  • 如何企业上云推广

    企业上云已成为数字化转型的核心战略,但推广过程中需结合行业特性、企业痛点与市场需求,构建系统性、多维度的推广体系,以下从市场定位、策略设计、执行落地及效果优化四个维度,详细拆解企业上云推广的实践路径,精准定位:明确目标企业与核心价值企业上云并非“一刀切”的方案,需先锁定目标客户群体,提炼差异化价值主张,客户分层……

    2025-11-20
    0
  • PS设计搜索框的实用技巧有哪些?

    在PS中设计一个美观且功能性的搜索框需要结合创意构思、视觉设计和用户体验考量,以下从设计思路、制作步骤、细节优化及交互预览等方面详细说明,帮助打造符合需求的搜索框,设计前的规划明确使用场景:根据网站或APP的整体风格确定搜索框的调性,例如极简风适合细线条和纯色,科技感适合渐变和发光效果,电商类则可能需要突出搜索……

    2025-11-20
    0

发表回复

您的邮箱地址不会被公开。必填项已用 * 标注