当前位置:Gxlcms > 数据库问题 > MySQL主从复制

MySQL主从复制

时间:2021-07-01 10:21:17 帮助过:29人阅读

本实验需要两台MySQL数据库服务器Master和slave,Master为主服务器,slave为从服务器,初始状态时,Master和slave中的数据信息相同,当Master 中的数据发生变化时,slave也跟着发生相应的变化,使得master和slave的数据信息同步,达到备份的目的。

mysql主从同配置步骤:
1)准备两台数据库环境,或者单台多实例环境,能正常启动和登录。
2)配置my.cnf文件,主库配置log-bin和server-id参数,从库配置server-id,不能和主库及其他库一样,一般不开启从库log-bin功能。注意:配置参数后要重启生效。 

master:log-bin = /data/3306/mysql-bin

              server-id=1

slave:      server-id=3 

3)登录主库增加用于从库连接主库同步的账户例如:rep,并授权replication slave 同步的权限

grant replication slave on *.* to ‘rep‘@‘10.0.0.%‘ identified by ‘chen‘;
flush privileges;

4)登录主库整个库锁表 flush table with read lock,然后查看show master status查看binlog的位置参数状态

+------------------+----------+--------------+------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000001 |      639 |              |                  |
+------------------+----------+--------------+------------------+

5)新窗口linux命令行备份或导出原有的数据库数据,并拷贝到从库所在的服务器目录如果数据量很大,并且允许停机,可以停机打包,而不用mysqldump
6)解锁主库,unlock tables;
7)把主库导出的原有数据恢复到从库
8)根据主库的show master status查看binlog的位置状态,在从库执行change master to .....
9)从库开启同步开关,start slave  
start slave 
10)从库show slave status\\G 检查同步状态,并在主库进行更新测试。
mysql> show slave status\\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 10.0.0.2
                  Master_User: rep
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000001
          Read_Master_Log_Pos: 639
               Relay_Log_File: relay-bin.000002
                Relay_Log_Pos: 425
        Relay_Master_Log_File: mysql-bin.000001
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes

              Replicate_Do_DB:
          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: 639
              Relay_Log_Space: 575
              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: 1
1 row in set (0.00 sec)

mysql>

完成上诉步骤后即可实现数据的主从同步,即在主库创建库表,在从库可可是实时同步。 
如需了解MySQL多实例可点击:http://purify.blog.51cto.com/10572011/1795031

本文出自 “叫醒你的不是闹钟而是梦想” 博客,请务必保留此出处http://purify.blog.51cto.com/10572011/1795297

MySQL主从复制

标签:mysql主从复制

人气教程排行