mysql主从同步配置

本文介绍了MySQL主从同步的配置与测试。准备两台服务器,阐述了主从库的作用及主从同步可实现数据自动备份。详细说明了主库和从库的配置步骤,包括启用日志、用户授权等,最后通过操作库表记录测试主从同步效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

准备两台服务器192.168.4.10(master)  192.168.4.20(slave)

主master库:被客户端存储数据访问的库

从slave库:同步主库的数据到本机

Mysql主从同步的作用:实现数据的自动备份

主库配置步骤:

1 启用binlog日志

2 用户授权

3 查看正在使用的binlog日志信息

 

从库配置步骤:

1 验证授权用户

2 指定server_id

3 指定主库信息

4 查看从库状态信息

 

客户端验证主从同步配置

#mysql -h主库IP -u用户名 -p密码  库名

MySQL>建表插入记录

在从本机查看是否有同样记录

 

开始操作

1:备份你想要备份的mysql库,这里以mytest库为例子.

[root@pc10 ~]# mysql -uroot -p123456
mysql> reset master;       重置binlog日志
[root@pc10 ~]# mysqldump -uroot -p123456 --all-databases > /root/mytest.sql   用mysqldump工具备份
[root@pc10 ~]# ls -lh /root/mytest.sql
-rw-r--r--. 1 root root 758K 3月  17 18:09 /root/mytest.sql
2;在salve(192.168.4.20)上导入备份的库

[root@pc10 ~]# scp /root/mytest.sql root@192.168.4.20:/   先将备份库拷贝给salve

[root@pc20 ~]# mysql -uroot -p < /mytest.sql      导入到slave的mysql
Enter password: 
 

3:配置mysql master

[root@pc10 ~]# vim /etc/my.cnf    打开配置文件写入以下

[mysqld]

log_bin=dbsvr1-bin        启用binlog日志,并指定文件前缀
server_id = 10                     指定服务器ID号

[root@pc10 ~]# systemctl restart mysqld    重启mysql

4:新建一个备份用户,授予复制权限

需要的权限为replication slave,允许从slave服务器访问;

mysql> grant replication slave on *.* to 'replicater'@'192.168.4.20' identified by '123456';

检查master服务器的同步状态

mysql> show master status\G
*************************** 1. row ***************************
             File: dbsvr1-bin.000001              记住当前的日志文件名
         Position: 1355                              记住当前的位置
     Binlog_Do_DB: 
 Binlog_Ignore_DB: 
Executed_Gtid_Set: 
1 row in set (0.00 sec)

5: 配置mysql slave从服务器

[root@pc20 ~]# vim /etc/my.cnf

[mysqld]

log_bin=dbsvr2-bin     启动SQL日志,并指定文件名前缀
server_id = 20               指定服务器ID号,不要与master相同

[root@pc20 ~]# systemctl restart mysqld  重启mysql

6:通过change master语句指定master服务器的IP地址,同步用户名/密码,起使日志文件,偏移位置

mysql> change master to master_host='192.168.4.10',
    -> master_user='replicater',
    -> master_password='123456',
    -> master_log_file='dbsvr1-bin.000001',
    -> master_log_pos=1355;

7:执行start slave 启动复制

mysql> start slave;
 

一单启动slave复制,需要修改master信息时,应先执行stop slave停止复制,然后重新修改,启动复制.

通过上述连接操作,MASTER服务器的设置信息自动存为master.info文件,以后每次MySQL服务程序时会自动调用并更新,无需重复设置。查看master.info文件的开头部分内容,可验证相关设置:

[root@pc20 ~]# ls -lh /var/lib/mysql/master.info 
-rw-r-----. 1 mysql mysql 133 3月  17 18:36 /var/lib/mysql/master.info
[root@pc20 ~]# head /var/lib/mysql/master.info
25
dbsvr1-bin.000001
1355
192.168.4.10
replicater
123456
3306
60
0
 

8:检查slave服务器的同步状态

通过SHOW SLAVE STATUS语句可查看从服务器状态,确认其中的IO线程、SQL线程正常运行,才能成功同步:

mysql> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.4.10
                  Master_User: replicater
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: dbsvr1-bin.000001
          Read_Master_Log_Pos: 1355
               Relay_Log_File: pc20-relay-bin.000002
                Relay_Log_Pos: 1222
        Relay_Master_Log_File: dbsvr1-bin.000001
             Slave_IO_Running: Yes          IO线程已运行
            Slave_SQL_Running: Yes         SQL线程已运行

              Replicate_Do_DB: 
          Replicate_Ignore_DB: 
           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: 1355
              Relay_Log_Space: 1428
              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: 10
                  Master_UUID: 6541231b-487a-11e9-9503-52540067d0aa
             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)
 

9.测试主从同步效果

在master上操作库表记录

mysql> create database newdb;
mysql> create table newtable(id int(4));

mysql> insert into newtable values(1234),(5678);

在从库上应可以看到这些表的修改记录;

mysql> select * from newtable;
+------+
| id   |
+------+
| 1234 |
| 5678 |
| 1234 |
| 5678 |
+------+
4 rows in set (0.00 sec)
在master机器上可以查看slave主机的信息

mysql> show slave hosts;
+-----------+------+------+-----------+--------------------------------------+
| Server_id | Host | Port | Master_id | Slave_UUID                           |
+-----------+------+------+-----------+--------------------------------------+
|        20 |      | 3306 |        10 | dc2fcb82-4892-11e9-ac42-5254006f38ce |
+-----------+------+------+-----------+--------------------------------------+
1 row in set (0.00 sec)
 

这样,最简单的主从同步就完成了.

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值