主從復(fù)制是mysql數(shù)據(jù)庫熱備份的一個常用的做法,這樣可以保存數(shù)據(jù)庫任何一方出現(xiàn)問題可及時更換上,下面小編來分享一篇關(guān)于從線上環(huán)境配置主從的mysql主從復(fù)制安裝配置實例.
主機(jī)172.16.0.21,172.16.0.22,centos6.2,分別使用yum安裝mysql
給21上的mysql新建庫:
CREATE DATABASE `replytest1` /*!40100 DEFAULT CHARACTER SET utf8 */;
建表:
- CREATE TABLE `replytest1`.`test1` (
- `id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
- `test1col` VARCHAR(45) NULL,
- PRIMARY KEY (`id`));
插入數(shù)據(jù):
- INSERT INTO `replytest1`.`test1` (`test1col`) VALUES (’1′);
- INSERT INTO `replytest1`.`test1` (`test1col`) VALUES (’2′);
修改主庫配置文件:
server-id = 1 //數(shù)據(jù)庫ID號,為1時表示為Master,其中master_id必須為1到232–1之間的一個正整數(shù)值;
log-bin=mysql-bin //啟用二進(jìn)制日志;
binlog-do-db=data //需要同步的二進(jìn)制數(shù)據(jù)庫名;可以寫多行
binlog-ignore-db=mysql //不同步的二進(jìn)制數(shù)據(jù)庫名;這個同步后聽說很麻煩
log-slave-updates //把更新的記錄寫到二進(jìn)制文件中;
slave-skip-errors
創(chuàng)建負(fù)責(zé)復(fù)制的用戶:
- ###GRANT ALL PRIVILEGES ON *.* TO reply@172.16.0.1 IDENTIFIED BY ‘reply’ WITH GRANT OPTION;
- grant replication slave on *.* to test@192.168.2.119 identified by ‘********’
- flush privileges;
鎖定主庫:FLUSH TABLES WITH READ LOCK;
導(dǎo)出主表數(shù)據(jù):解鎖并檢查當(dāng)前的master狀態(tài)及復(fù)制到哪里.
UNLOCK TABLES;Show master status;
從庫設(shè)置,修改配置文件,代碼如下:
- #從庫設(shè)置
- server-id=2
- master-host=172.16.0.1
- master-user=reply
- master-password=reply
- master-connect-retry=600 #如果發(fā)現(xiàn)主服務(wù)器斷線,重新連接的時間差;
- replicate-ignore-db=mysql
- replicate-ignore-db=test
- log-slave-update
- slave-skip-errors
- #從庫設(shè)置結(jié)束
重啟mysql,在從服務(wù)器上執(zhí)行stop slave,reset master命令,重置成主數(shù)據(jù)庫,代碼如下:
- mysql> stop slave;
- Query OK, 0 rows affected, 1 warning (0.00 sec)
- mysql> reset master;
- change master to master_host=’172.16.0.21′,master_user=’reply’, master_password=’reply’, master_log_file=’mysql-bin.000001′, master_log_pos=106;
- Slave start;
- mysql> show slave statusG
- *************************** 1. row ***************************
- Slave_IO_State: Waiting for master to send event
- Master_Host: 172.16.0.21
- Master_User: reply
- Master_Port: 3306
- Connect_Retry: 600
- Master_Log_File: mysql-bin.000001
- Read_Master_Log_Pos: 106
- Relay_Log_File: mysqld-relay-bin.000002
- Relay_Log_Pos: 251
- Relay_Master_Log_File: mysql-bin.000001
- Slave_IO_Running: Yes --Vevb.com
- Slave_SQL_Running: Yes
- Replicate_Do_DB:
- Replicate_Ignore_DB: mysql,test
- 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: 106
- Relay_Log_Space: 407
- 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)
新聞熱點
疑難解答
圖片精選