操做系統 Centos 6.4 x86_64node
軟件版本 mysql 5.5.37mysql
一 主-從複製 (異步)sql
當主-從服務器都剛完成mysql安裝,沒有數據vim
主服務器 192.168.200.127api
從服務器 192.168.200.128服務器
1 時間同步session
[root@node1 ~]# ntpdate ntp.api.bz [root@node2 ~]# ntpdate ntp.api.bz
2 配置masterless
1 修改配置文件異步
sql_log_bin=on log_bin=mysql-bin server-id=1 innodb_file_per_table=1
2受權複製用戶ide
mysql> grant replication slave on *.* to identified by ; Query OK, 0 rows affected (0.00 sec) mysql> flush privileges; Query OK, 0 rows affected (0.00 sec)
3重啓mysql服務
[root@node1 ~]# service mysqld restart Shutting down MySQL. SUCCESS! Starting MySQL.. SUCCESS!
3 配置slave
1 修改配置文件
[root@node2 ~]# vim /etc/my.cnf [mysqld] relay_log = relay-log server-id = 10
2 重啓mysql服務
[root@node2 ~]# service mysqld restart Shutting down MySQL. SUCCESS! Starting MySQL.. SUCCESS!
3 鏈接主服務器
主服務器
mysql> show master status; +------------------+----------+--------------+------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | +------------------+----------+--------------+------------------+ | mysql-bin.000002 | 107 | | | +------------------+----------+--------------+------------------+ 1 row in set (0.00 sec)
從服務器
mysql> change master to master_host='192.168.200.127',master_user='user',master_password='redhat'; Query OK, 0 rows affected (0.01 sec) mysql> start slave; mysql> show slave status\G *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.200.127 Master_User: user Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000002 Read_Master_Log_Pos: 186 Relay_Log_File: relay-log.000003 Relay_Log_Pos: 332 Relay_Master_Log_File: mysql-bin.000002 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: 186 Relay_Log_Space: 857 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 set (0.00 sec)
4 主從複製測試
master
mysql> create database db; mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | db | | mysql | | performance_schema | | test | +--------------------+ 5 rows in set (0.00 sec
slave
mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | db | | mysql | | performance_schema | | test | +--------------------+ 5 rows in set (0.00 sec)
二 當主服務器已有數據
1 master 備份
[root@node1 ~]# mysqldump -uroot --all-databases --lock-all-table --master-data=2 --events > all.sql
2 複製sql文件至從服務器
[root@node1 ~]# scp all.sql 192.168.200.128:/tmp/
3 salve 恢復
mysql> source /tmp/all.sql
4 查看備份二進制文件位置
[root@node1 ~]# less all.sql -- CHANGE MASTER TO MASTER_LOG_FILE=, MASTER_LOG_POS=809;
5 複製
mysql> change master to master_host='192.168.200.127',master_user='user',master_password='redhat',master_log_file='mysql-bin.000002,master_log_pos=809; mysql> start slave; mysql> show slave status\G *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.200.127 Master_User: user Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000002 Read_Master_Log_Pos: 809 Relay_Log_File: relay-log.000002 Relay_Log_Pos: 253 Relay_Master_Log_File: mysql-bin.000002 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: 809 Relay_Log_Space: 403 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 set (0.00 sec)
三 半同步複製
1 安裝插件
mysql> INSTALL PLUGIN rpl_semi_sync_master SONAME 'semisync_slave.so' ; mysql> SET GLOBAL rpl_semi_sync_master_enabled = 1; mysql> SET GLOBAL rpl_semi_sync_master_timeout = 2000;
2 修改配置文件
[root@node1 ~]# vim /etc/my.cnf rpl_semi_sync_master_enabled = 1 rpl_semi_sync_master_timeout = 2000
3 重啓服務
[root@node1 ~]# service mysqld restart Shutting down MySQL. SUCCESS! Starting MySQL.. SUCCESS!
slave
1安裝插件
mysql> INSTALL PLUGIN rpl_semi_sync_slave SONAME ; mysql> SET GLOBAL rpl_semi_sync_slave_enabled = 1; mysql> STOP SLAVE IO_THREAD; mysql> START SLAVE IO_THREAD;
2修改配置文件
[root@node2 mysql]# vim /etc/my.cnf rpl_semi_sync_slave_enabled = 1
3 重啓服務
[root@node2 mysql]# service mysqld restart Shutting down MySQL. SUCCESS! Starting MySQL.. SUCCESS!
4 查看狀態
master
mysql> show global status like 'rep_semi%'; +--------------------------------------------+-------+ | Variable_name | Value | +--------------------------------------------+-------+ | Rpl_semi_sync_master_clients | 1 | | Rpl_semi_sync_master_net_avg_wait_time | 0 | | Rpl_semi_sync_master_net_wait_time | 0 | | Rpl_semi_sync_master_net_waits | 0 | | Rpl_semi_sync_master_no_times | 0 | | Rpl_semi_sync_master_no_tx | 0 | | Rpl_semi_sync_master_status | ON | | Rpl_semi_sync_master_timefunc_failures | 0 | | Rpl_semi_sync_master_tx_avg_wait_time | 0 | | Rpl_semi_sync_master_tx_wait_time | 0 | | Rpl_semi_sync_master_tx_waits | 0 | | Rpl_semi_sync_master_wait_pos_backtraverse | 0 | | Rpl_semi_sync_master_wait_sessions | 0 | | Rpl_semi_sync_master_yes_tx | 0 | +--------------------------------------------+-------+ 14 rows in set (0.00 sec)
slave
mysql> show global status like 'rpl_semi%' ; +----------------------------+-------+ | Variable_name | Value | +----------------------------+-------+ | Rpl_semi_sync_slave_status | ON | +----------------------------+-------+ 1 row in set (0.00 sec)
半同步複製完成