MySQL主从复制配置

MySQL主从复制配置

说明:

该文档简述一个简单的MySQL主从复制模式的配置过程。

 一、  环境

系统平台:CentOS 5.5_x64

MySQL5.5.28

 

主库:192.168.100.146

备库:192.168.100.7

二、步骤

2.1 安装mysql

在主备两台PC上安装MySQL,详细的安装过程请参阅CentOS5.5_x64上源码安装mysql-5.5.28,安装后确保可以正常连接使用。

注:该例MySQL中不存在应用数据,若存在应用数据则另需首先按照中所述配置。

2.2 配置主服务器

2.2.1 检查配置文件

检查文件/etc/my.cnf,确保以下两项配置正确:

log-bin = mysql-bin   --表明启用了二进制日志记录
service-id = 1

2.2.2 创建复制账户

# mysql
mysql> grant replication slave on *.* to 'highgo'@'192.168.100.7' identified by 'highgo';

此时便创建了一个提供给备库用于复制的账户highgo

2.2.3 查询主库状态并记录

在主服务器上执行:

记录下二进制日志文件名和偏移量,即:mysql-bin.000006   1218

2.3 配置从服务器

2.3.1 检查配置文件

检查文件/etc/my.cnf,确保以下两项配置正确:

log-bin = mysql-bin   --表明启用了二进制日志记录
service-id = 2

2.3.2 重新启动

# service mysqld restart

2.3.2 创建连接信息

# mysql
mysql> change master to 
-> master_host=’192.168.100.146’,
-> master_user=’highgo’,
-> master_password=’highgo’,
-> master_master_log_file=’ mysql-bin.000006’,
-> master_log_pos=1218;

2.3.3 启动从服务器

mysql> start slave;

2.3.4 检查从库信息

mysql> show slave status \G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.100.146
                  Master_User: highgo
                  Master_Port: 3306
                Connect_Retry: 30
              Master_Log_File: mysql-bin.000006
          Read_Master_Log_Pos: 1218
               Relay_Log_File: test-relay-bin.000003
                Relay_Log_Pos: 627
        Relay_Master_Log_File: mysql-bin.000006
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              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: 1218
              Relay_Log_Space: 1665
              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)

注:主要检查Slave_IO_Running、Slave_SQL_Running都为Yes说明已正确同步。

2.4 测试

在主库上创建数据后,查看从库是否同步。 

2.5 同步应用数据

2.5.1 锁表

在主服务器上执行

mysql> flush tables with read lock;

2.5.2查询主库状态并记录

mysql> show master status;

记录下二进制日志文件与偏移量。

2.5.3 拷贝data

将整个data目录复制到备库中。建议先打包再拷贝,然后在备库中解压。

2.5.4 解锁

mysql> unlock tables;

你可能感兴趣的:(mysql,复制,主从)