萬盛學電腦網

 萬盛學電腦網 >> 數據庫 >> mysql教程 >> mysql主從復制安裝配置2 — 從線上環境配置主從

mysql主從復制安裝配置2 — 從線上環境配置主從

主從復制是mysql數據庫熱備份的一個常用的做法,這樣可以保存數據庫任何一方出現問題可及時更換上,下面小編來分享一篇關於從線上環境配置主從的mysql主從復制安裝配置實例。

主機
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`));

插入數據

INSERT INTO `replytest1`.`test1` (`test1col`) VALUES (’1′);
INSERT INTO `replytest1`.`test1` (`test1col`) VALUES (’2′);

——————————————————-

修改主庫配置文件

server-id = 1 //數據庫ID號, 為1時表示為Master,其中master_id必須為1到232–1之間的一個正整數值;
log-bin=mysql-bin //啟用二進制日志;
binlog-do-db=data //需要同步的二進制數據庫名;可以寫多行
binlog-ignore-db=mysql //不同步的二進制數據庫名;這個同步後聽說很麻煩
log-slave-updates //把更新的記錄寫到二進制文件中;
slave-skip-errors

創建負責復制的用戶

###GRANT ALL PRIVILEGES ON *.* TO [email protected] IDENTIFIED BY ‘reply’ WITH GRANT OPTION;
grant replication slave on *.* to [email protected] identified by ‘********’

flush privileges;

鎖定主庫

FLUSH TABLES WITH READ LOCK;

導出主表數據

 

解鎖 並 檢查當前的master狀態及復制到哪裡

UNLOCK TABLES;Show master status;

從庫設置

修改配置文件

#從庫設置
server-id=2
master-host=172.16.0.1
master-user=reply
master-password=reply
master-connect-retry=600 #如果發現主服務器斷線,重新連接的時間差;
replicate-ignore-db=mysql
replicate-ignore-db=test
log-slave-update
slave-skip-errors
#從庫設置結束

重啟mysql

在從服務器上執行stop slave,reset master命令,重置成主數據庫

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
            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)

copyright © 萬盛學電腦網 all rights reserved