1、環境介紹
OS:CentOS6.7_x64
MySQL:5.1.73
2、MySQL安裝
yum -y install mysql-server
注意事項:
主從設備的MySQL版本、硬件配置要一致,否則可能會導致一系列問題,如從庫來不及消費主庫的日誌,會引起從庫日誌文件堆積等!
3、主從配置
主要操作步驟:
配置binlog和serverid;
建立同步帳號(master),並授權;
根據master的狀態,配置slave同步;
啓動slave的複製功能;
查看主從複製狀態slave IO和SQL的running都爲yes,即爲配置成功;
測試。
3.1 修改主、從服務器的mysql配置文件my.ini添加如下配置
[mysqld] log-bin=mysql-bin //[不是必須]啓用二進制日誌 server-id=112 //[必須]服務器唯一ID,默認是1,一般取IP最後一段
3.2 重啓兩臺服務器的mysql
/etc/init.d/mysql restart
3.3 在主服務器上建立帳戶並授權slave
[jumper@lb01 ~]$ mysql Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 3 Server version: 5.1.73-log Source distribution Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> GRANT REPLICATION SLAVE ON *.* to 'mysync'@'%' identified by 'q123456'; Query OK, 0 rows affected (0.00 sec) mysql> FLUSH PRIVILEGES; Query OK, 0 rows affected (0.00 sec)
注意:
同步帳號一般不用root;
在實際工作中,只能授權單個IP,不能是通配符的形式授權;
如果有多個ip,就每個ip單獨執行一遍授權語句;
3.4 在主服務器上查看mysql狀態
mysql> show master status; +------------------+----------+--------------+------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | +------------------+----------+--------------+------------------+ | mysql-bin.000001 | 476 | | | +------------------+----------+--------------+------------------+ 1 row in set (0.00 sec)
注:執行完此步驟後不要再操作主服務器MYSQL,防止主服務器狀態值變化
3.5 配置從服務器
mysql> change master to master_host='10.86.87.254',master_user='mysync',master_password='q123456', master_log_file='mysql-bin.000001',master_log_pos=476; Query OK, 0 rows affected (0.02 sec) mysql> start slave; //啓動複製功能 Query OK, 0 rows affected (0.00 sec)
3.6 查看主從服務器複製功能狀態
mysql> show slave status\G *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 10.86.87.254 //主服務器地址 Master_User: mysync //授權帳戶名,儘量避免使用root Master_Port: 3306 //數據庫端口,部分版本沒有此行 Connect_Retry: 60 Master_Log_File: mysql-bin.000001 Read_Master_Log_Pos: 476 //同步讀取二進制日誌的位置,大於等於Exec_Master_Log_Pos Relay_Log_File: mysqld-relay-bin.000002 Relay_Log_Pos: 251 Relay_Master_Log_File: mysql-bin.000001 Slave_IO_Running: Yes //此狀態必須YES Slave_SQL_Running: Yes //此狀態必須YES ...
注:Slave_IO及Slave_SQL進程必須正常運行,即YES狀態,否則都是錯誤的狀態(如:其中一個NO均屬錯誤)。
主從服務器配置完成。
4、測試
4.1 主服務器Mysql
建立數據庫,並在這個庫中建表插入一條數據
mysql> create database test_db; Query OK, 1 row affected (0.00 sec) mysql> use test_db; Database changed mysql> create table test_tb(id int(3),name char(10)); Query OK, 0 rows affected (0.00 sec) mysql> insert into test_tb values(001,'bobu'); Query OK, 1 row affected (0.00 sec) mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema| | test_db | | mysql | | test | +--------------------+ 4 rows in set (0.00 sec)
從服務器Mysql查詢
mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema| | test_db | //I'M here,大家看到了吧 | mysql | | test | +--------------------+ 4 rows in set (0.00 sec) mysql> use test_db Database changed mysql> select * from test_tb; //查看主服務器上新增的具體數據 +--------+------+ | id | name| +--------+------+ | 1 | bobu| +--------+------+ 1 row in set (0.00 sec)