---
title: "MySQL主从库配置方法"
author: "boris_it"
date: "2013-01-06T18:35:15Z"
tags: []
categories: ["数据库"]
slug: "mysql"
draft: false
lastmod: "2013-01-06T18:37:40Z"
id: 1763993
url: "https://borisit.iteye.com/blog/1763993"
---

MySQL主从库配置方法(针对MyISAM数据引擎)

1、主库配置(192.168.1.200)

a、my.cnf中【mysqld】节配置

server-id = 1  //master服务ID，必须唯一*
log-bin   = mysql-bin    //同步日志文件，必须打开*


b、登录mysql创建同步账户并授予权限：
grant replication slave,reload,super on *.* to myslave@'192.168.1.201' identified by '123456';


2、slave配置(192.168.1.201)

a、my.cnf中【mysqld】节

server-id = 2  //master服务ID，必须唯一*
log-bin   = mysql-bin    //同步日志文件，必须打开*
master-host     =   192.168.1.200      //主库地址*
master-user     =   myslave            //同步的用户名*
master-password =   123456	            //同步的密码*
master-port     =  3306		   //主库端口*
master-connect-retry=60                 //同步失败重连时间
replicate-do-db = testslave             //执行同步的数据库*

以下配置为可选配置，根据实际需求调整
replicate-ignore-db = mysql             //不同步的数据库
replicate-do-table = user               //执行同步的表
replicate-ignore-table = city           //不同步的表
replicate-wild-do-table = testslave.a%  //执行同步的多个表
replicate-wild_ignore-table = mysql.b%  //不同步的多个表



3、重新启动主、从库

4、同步数据库

a、登录主库并执行：
mysql>flush tables with read lock;

mysql> show master status;
+------------------+----------+--------------+------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000008 |      106 |              |                  | 
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)

//此处需要记录下来同步的开始位置106


再打开一个窗口，将主库的数据文件拷贝到从库中：

scp testslave/* root@192.168.1.201:/usr/mysql/var/testslave/

拷贝完成之后，返回前一个窗口解锁数据库只读。

mysql>unlock tables;


b、登录从库mysql：

mysql>flush tables;

mysql>stop slave;

mysql>CHANGE MASTER TO MASTER_HOST='192.168.1.200', MASTER_PORT=3306, MASTER_USER='myslave', 
MASTER_PASSWORD='123456',MASTER_LOG_FILE='mysql-bin.000008', MASTER_LOG_POS=106;

mysql>start slave;

mysql> show slave status\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.1.200
                  Master_User: myslave
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000008
          Read_Master_Log_Pos: 106
               Relay_Log_File: ccone2-relay-bin.000002
                Relay_Log_Pos: 251
        Relay_Master_Log_File: mysql-bin.000008
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB: ccone
          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: 106
              Relay_Log_Space: 407
              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)

ERROR: 
No query specified


查看Slave_IO_Running: Yes 和Slave_SQL_Running: Yes  2个都为yes则证明主从同步正常，
否则证明同步有问题，可查看日志文件修复。


5、如果做双向同步，则可在此基础上将主从库再反向配置即可实现。