1、兩台機器,安裝好版本一致的MySQL
192.168.29.128 (master) MySQL-5.6.21
192.168.29.129 (slave) MySQL-5.6.21
2、配置master
配置binary log和server ID
[root@Luxh-01 ~]# vi /etc/my.cnf
配置如下:
[mysqld]
log-bin=mysql-bin server-id=1
重啟MySQL
3、配置slave
[mysqld] server-id=2
4、在master上創建用於復制的賬號
mysql> create user 'repl'@'%' identified by '123456'; Query OK, 0 rows affected (0.13 sec) mysql> grant replication slave on *.* to 'repl'@'%' ; Query OK, 0 rows affected (0.02 sec)
5、在master上查看 binary log文件名和 position
mysql> show master status; +------------------+----------+--------------+------------------+-------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | +------------------+----------+--------------+------------------+-------------------+ | mysql-bin.000003 | 120 | | | | +------------------+----------+--------------+------------------+-------------------+ 1 row in set (0.00 sec)
6、配置slave
mysql> change master to master_host='192.168.29.128',master_user='repl',master_password='123456',master_port=3306,master_log_file='mysql-bin.000003',master_log_pos=120,master_connect_retry=10; Query OK, 0 rows affected, 2 warnings (0.30 sec)
使用上面創建的用於復制的賬號,master_log_file使用master上查詢出來的文件名,master_log_pos也必須使用master上查詢出來的position
7、在slave上啟動復制功能
mysql> start slave; Query OK, 0 rows affected (0.01 sec)
8、查看slave的狀態
mysql> show slave status\G; *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.29.128 Master_User: repl Master_Port: 3306 Connect_Retry: 10 Master_Log_File: mysql-bin.000003 Read_Master_Log_Pos: 120 Relay_Log_File: Luxh-02-relay-bin.000002 Relay_Log_Pos: 283 Relay_Master_Log_File: mysql-bin.000003 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: 120 Relay_Log_Space: 458 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 Master_UUID: 9567210c-684f-11e4-b062-000c29398b4c Master_Info_File: /var/lib/mysql/master.info SQL_Delay: 0 SQL_Remaining_Delay: NULL Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it Master_Retry_Count: 86400 Master_Bind: Last_IO_Error_Timestamp: Last_SQL_Error_Timestamp: Master_SSL_Crl: Master_SSL_Crlpath: Retrieved_Gtid_Set: Executed_Gtid_Set: Auto_Position: 0 1 row in set (0.00 sec)
9、配置完畢。測試一下效果。
1)在master上執行以下語句
mysql> create database web_db; Query OK, 1 row affected (0.04 sec) mysql> use web_db; Database changed mysql> create table t_user(id int primary key auto_increment,username varchar(32)); Query OK, 0 rows affected (0.13 sec) mysql> insert into t_user(username) values('lihuai'); Query OK, 1 row affected (0.05 sec) mysql> select * from t_user; +----+----------+ | id | username | +----+----------+ | 1 | lihuai | +----+----------+ 1 row in set (0.00 sec)
2)在slave上查看,看是否已成功復制
mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | test | | web_db | +--------------------+ 5 rows in set (0.05 sec) mysql> use web_db; Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A Database changed mysql> show tables; +------------------+ | Tables_in_web_db | +------------------+ | t_user | +------------------+ 1 row in set (0.00 sec) mysql> select * from t_user; +----+----------+ | id | username | +----+----------+ | 1 | lihuai | +----+----------+ 1 row in set (0.00 sec)
10、以上配置是針對全新安裝的MySQL服務器做主從復制,還可以針對已有數據的數據庫、或者已有的主從結構上再添加slave
具體配置,可以參考官網文檔,非常詳細。
http://dev.mysql.com/doc/refman/5.6/en/replication-howto.html