一、mysql主從方式:
同步:主伺服器有資料寫入,儲存在硬碟,記錄二進位日誌一份。二進位日誌完整發送給從伺服器記錄於中繼日誌中,從伺服器回應主伺服器已收到,
非同步:主伺服器寫入資料,儲存磁碟,記錄二進位日誌,直接反饋用戶端;至於從伺服器收到與否,不予理會
mysql的複製預設為非同步模式
二、配置主從伺服器
1.先在兩台機器上分別安裝好mysql,版本需要一致,然後配置,啟動
2.主伺服器配置
1).查看mysql是否正常啟動
netstat -nltp | grep 3306
2)修改設定檔,開啟二進位日誌
2.1.vim /etc/my.cnf
將
log-bin=mysql-bin改為log-bin=/usr/local/mysql/var/binlogs/master-bin
儲存
2.2.建立二進位日誌目錄
mkdir -p /usr/local/mysql/var/binlogs/
2.3.對目錄許可權進行授權
chown mysql.mysql /usr/local/mysql/var/binlogs/
2.4.重啟mysql服務
service mysqld restart
3)建立有複製許可權的帳號
mysql>grant replication slave,replication client on *.* to 'replication'@'192.168.5.10' identified by 'Looking_s123';
mysql>flush privileges;
3.從伺服器配置
1)查看mysql是否正常啟動
netstat -ntlp | grep 3306
2)修改設定檔
vim /etc/my.cnf
將二進位記錄檔注釋
log-bin=mysql-bin
binlog_format=mixed
修改server-id
將
server-id =1
修改為
server-id =2
開啟中繼日誌
relay-log =/usr/local/mysql/var/relaylogs/relaylogs
建立中繼日誌目錄及授權
mkdir -p /usr/local/mysql/var/relaylogs/
chown -R mysql.mysql /usr/local/mysql/var/relaylogs/
3)重啟服務,確保中繼日誌啟動
service mysqld restart
4)查看中繼日誌啟動狀態
mysql> show global variables like '%relay%';
+-----------------------+------------------------------------------+
| Variable_name | Value |
+-----------------------+------------------------------------------+
| max_relay_log_size | 0 |
| relay_log | /usr/local/mysql/var/relaylogs/relaylogs |
| relay_log_index | |
| relay_log_info_file | relay-log.info |
| relay_log_purge | ON |
| relay_log_space_limit | 0 |
+-----------------------+------------------------------------------+
6 rows in set (0.00 sec)
5)串連主伺服器
mysql>change master to master_host='192.168.5.11',master_user='replication',master_password='Looking_s123';
6)手動啟動複製線程
mysql>start slave;
7)查看主從狀態
mysql> show slave status \G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.5.11
Master_User: replication
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: master-bin.000003
Read_Master_Log_Pos: 317
Relay_Log_File: relaylogs.000005
Relay_Log_Pos: 463
Relay_Master_Log_File: master-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: 317
Relay_Log_Space: 999
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
三.測試
在主伺服器上建立資料庫和表,在從伺服器上查看複製效果,如果同步了,就說明配置成功了!