首页 > 代码库 > mysql replication配置

mysql replication配置

mysql replication配置
1、修改主机(master)的配置文件(my.cnf)
server_id = 1
log_bin = mysql-bin

2、修改从机(slave)配置文件
#主备环境下需要唯一
server_id = 2

3、主机创建复制分发用户,并授权进行复制分发
CREATE USER ‘repl‘@‘%.mydomain.com‘ IDENTIFIED BY ‘slavepass‘;
GRANT REPLICATION SLAVE ON *.* TO ‘repl‘;

4、重新启动主机,使配置生效

5、获取主机复制分发点
FLUSH TABLES WITH READ LOCK;
SHOW MASTER STATUS;
UNLOCK TABLES;
获得类似如下结果
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000003 | 73 | test | manual,mysql |
+------------------+----------+--------------+------------------+
其中file/position字段需要在从机使用


6、重新启动从机

7、设置从机的主机信息
语句类似如下
CHANGE MASTER TO MASTER_HOST=‘master_host_name‘,MASTER_USER=‘replication_user_name‘,MASTER_PASSWORD=‘replication_password‘,MASTER_LOG_FILE=‘recorded_log_file_name‘,MASTER_LOG_POS=recorded_log_position;
其中
MASTER_LOG_FILE=‘recorded_log_file_name‘中recorded_log_file_name为show master status中的file字段内容
MASTER_LOG_POS=recorded_log_position中recorded_log_position为show master status中的Position字段内容

SHOW SLAVE STATUS得到类似如下内容
+----------------------------------+-----------------+-------------+-------------+---------------+------------------+---------------------+----------------------------+---------------+-----------------------+------------------+-------------------+-----------------+---------------------+--------------------+------------------------+-------------------------+-----------------------------+------------+------------+--------------+---------------------+-----------------+-----------------+----------------+---------------+--------------------+--------------------+--------------------+-----------------+-------------------+----------------+-----------------------+-------------------------------+---------------+---------------+----------------+----------------+-----------------------------+------------------+--------------------------------------+--------------------------------------------------------+-----------+---------------------+-----------------------------------------------------------------------------+--------------------+-------------+-------------------------+--------------------------+----------------+--------------------+--------------------+-------------------+---------------+
| Slave_IO_State | Master_Host | Master_User | Master_Port | Connect_Retry | Master_Log_File | Read_Master_Log_Pos | Relay_Log_File | Relay_Log_Pos | Relay_Master_Log_File | Slave_IO_Running | Slave_SQL_Running | Replicate_Do_DB | Replicate_Ignore_DB | Replicate_Do_Table | Replicate_Ignore_Table | Replicate_Wild_Do_Table | Replicate_Wild_Ignore_Table | Last_Errno | Last_Error | Skip_Counter | Exec_Master_Log_Pos | Relay_Log_Space | Until_Condition | Until_Log_File | Until_Log_Pos | Master_SSL_Allowed | Master_SSL_CA_File | Master_SSL_CA_Path | Master_SSL_Cert | Master_SSL_Cipher | Master_SSL_Key | Seconds_Behind_Master | Master_SSL_Verify_Server_Cert | Last_IO_Errno | Last_IO_Error | Last_SQL_Errno | Last_SQL_Error | Replicate_Ignore_Server_Ids | Master_Server_Id | Master_UUID | Master_Info_File | SQL_Delay | SQL_Remaining_Delay | Slave_SQL_Running_State | Master_Retry_Count | Master_Bind | Last_IO_Error_Timestamp | Last_SQL_Error_Timestamp | Master_SSL_Crl | Master_SSL_Crlpath | Retrieved_Gtid_Set | Executed_Gtid_Set | Auto_Position |
+----------------------------------+-----------------+-------------+-------------+---------------+------------------+---------------------+----------------------------+---------------+-----------------------+------------------+-------------------+-----------------+---------------------+--------------------+------------------------+-------------------------+-----------------------------+------------+------------+--------------+---------------------+-----------------+-----------------+----------------+---------------+--------------------+--------------------+--------------------+-----------------+-------------------+----------------+-----------------------+-------------------------------+---------------+---------------+----------------+----------------+-----------------------------+------------------+--------------------------------------+--------------------------------------------------------+-----------+---------------------+-----------------------------------------------------------------------------+--------------------+-------------+-------------------------+--------------------------+----------------+--------------------+--------------------+-------------------+---------------+
| Waiting for master to send event | 192.168.153.128 | repl | 3306 | 60 | mysql-bin.000019 | 800 | localhost-relay-bin.000003 | 676 | mysql-bin.000019 | Yes | Yes | | | | | | | 0 | | 0 | 800 | 853 | None | | 0 | No | | | | | | 0 | No | 0 | | 0 | | | 1 | aaf47eb1-fa78-11e3-a424-000c2932455f | /home/jiangyx/mysql/replication/slave/data/master.info | 0 | NULL | Slave has read all relay log; waiting for the slave I/O thread to update it | 86400 | | | | | | | | 0 |
+----------------------------------+-----------------+-------------+-------------+---------------+------------------+---------------------+----------------------------+---------------+-----------------------+------------------+-------------------+-----------------+---------------------+--------------------+------------------------+-------------------------+-----------------------------+------------+------------+--------------+---------------------+-----------------+-----------------+----------------+---------------+--------------------+--------------------+--------------------+-----------------+-------------------+----------------+-----------------------+-------------------------------+---------------+---------------+----------------+----------------+-----------------------------+------------------+--------------------------------------+--------------------------------------------------------+-----------+---------------------+-----------------------------------------------------------------------------+--------------------+-------------+-------------------------+--------------------------+----------------+--------------------+--------------------+-------------------+---------------+
其中Slave_IO_Running / Slave_SQL_Running都为yes表示成功

注意:
1、如果slave是通过拷贝整个mysql的data,那么需要修改data目录下的auto.cnf文件的uuid,以确保主机和备机的uuid不相同。否则show slave status中会有报错信息“Fatal error: The slave I/O thread stops because master and slave have equal MySQL server UUIDs; these UUIDs must be different for replication to work. ”

参考:
1、http://dev.mysql.com/doc/refman/5.6/en/replication-howto.html