配置主從數(shù)據(jù)庫的想法,是從一次面試開始,被面試官問到,不懂就被斃了。參考下面文章終于配置成功。
Mysql復(fù)制環(huán)境90%以上都是一個(gè)Master帶一個(gè)或者多個(gè)Slave的架構(gòu)模式,主要用于讀壓力比較大的應(yīng)用的數(shù)據(jù)庫端廉價(jià)擴(kuò)展解決方案。因?yàn)橹?要master和slave的壓力不是太大(尤其是slave端壓力)的話,異步復(fù)制的延時(shí)一般都很少很少。尤其是自slave端的復(fù)制方式改成兩個(gè)進(jìn)程 處理之后,更是減小了slave端的延時(shí)。而帶來的效益是,對(duì)于數(shù)據(jù)實(shí)時(shí)性要求不是特別的敏感度的應(yīng)用,只需要通過廉價(jià)的pc server來擴(kuò)展slave的數(shù)量,將讀壓力分散到多臺(tái)slave的機(jī)器上面,即可解決數(shù)據(jù)庫端的讀壓力瓶頸。這在很大程度上解決了目前很多中小型網(wǎng)站 的數(shù)據(jù)庫壓力瓶頸問題,甚至有些大型網(wǎng)站也在使用類似方案解決數(shù)據(jù)庫瓶頸。
主從庫是兩臺(tái)服務(wù)器上的兩個(gè)數(shù)據(jù)庫,主庫以最快的速度做增刪改操作+最新數(shù)據(jù)的查詢操作; 從庫負(fù)責(zé)查詢較舊數(shù)據(jù),做一些對(duì)實(shí)效性要求較小的分析,報(bào)表生成的工作。這樣做將數(shù)據(jù)庫的壓力分擔(dān)到兩臺(tái)服務(wù)器上從而保證整個(gè)系統(tǒng)響應(yīng)的及時(shí)性。
主從同步數(shù)據(jù)庫,其實(shí)就是集群數(shù)據(jù)庫的概念,在客戶端看上去數(shù)據(jù)庫只有一個(gè),但是實(shí)質(zhì)上不止一個(gè),可能是多個(gè)數(shù)據(jù)庫在維持,實(shí)質(zhì)上說這個(gè)技巧是為了保證web平臺(tái)的穩(wěn)定而
使用的,這個(gè)技巧并不是用來防止數(shù)據(jù)出錯(cuò)(數(shù)據(jù)出錯(cuò)站在mysql的層面來看是根本沒法避免的,只能靠備份來預(yù)防)是為了提高可靠性。
配置過程還參考了http://www.cnblogs.com/xiazh/archive/2011/04/22/1971182.html
配置時(shí)還請(qǐng)注意:
過程 1.1. Master 設(shè)置步驟
配置 my.cnf 文件
確保主服務(wù)器主機(jī)上my.cnf文件的[mysqld]部分包括一個(gè)log-bin選項(xiàng)。該部分還應(yīng)有一個(gè)server-id=Master_id選項(xiàng)
# vim /etc/mysql/my.cnf server-id = 1 log_bin = /var/log/mysql/mysql-bin.log expire_logs_days = 10 max_binlog_size = 100M binlog_do_db = test binlog_ignore_db = mysql
bind-address默認(rèn)是127.0.0.1你必須更改它,否則Slave將無法鏈接到 Master
#bind-address = 127.0.0.1 bind-address = 0.0.0.0
重啟服務(wù)器
neo@netkiller:~$ sudo /etc/init.d/mysql reload * Reloading MySQL database server mysqld [ OK ]
建議使用reload,如果不起作用再用restart
登錄slave服務(wù)器,測(cè)試主庫3306工作情況,如果看到下面相關(guān)信息表示工作正常。
# telnet 192.168.1.246 3306 Trying 192.168.1.246... Connected to 192.168.1.246. Escape character is '^]'. I 5.1.61-0ubuntu0.11.10.1-log1W<gs/*'#}p<u[J=5//:
創(chuàng)建賬戶并授予REPLICATION SLAVE權(quán)限
mysql> GRANT REPLICATION SLAVE ON *.* TO 'replication'@'%.mydomain.com' IDENTIFIED BY 'slavepass'; mysql> FLUSH PRIVILEGES;
GRANT REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO replication@'192.168.245.131' IDENTIFIED BY 'slavepass'
鎖表禁止寫入新數(shù)據(jù)
mysql> FLUSH TABLES WITH READ LOCK;
查看Master 工作狀態(tài)
mysql> SHOW MASTER STATUS; +------------------+----------+--------------+------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | +------------------+----------+--------------+------------------+ | mysql-bin.000002 | 106 | test | mysql | +------------------+----------+--------------+------------------+ 1 row in set (0.00 sec)
如果顯示下面內(nèi)容表示,配置不正確
mysql> SHOW MASTER STATUS; Empty set (0.02 sec)
取得快照并記錄日志名和偏移量后,可以在主服務(wù)器上重新啟用寫活動(dòng)
mysql> UNLOCK TABLES;
過程 1.2. Slave 設(shè)置步驟
配置my.cnf
從服務(wù)器的ID必須與主服務(wù)器的ID不相同,如果設(shè)置多個(gè)從服務(wù)器,每個(gè)從服務(wù)器必須有一個(gè)唯一的server-id值,必須與主服務(wù)器的以及其它從服務(wù)器的不相同。
# vim /etc/mysql/my.cnf [mysqld] server-id = 2
# service mysql restart mysql start/running, process 22893
指定 master 相關(guān)參數(shù)
在從服務(wù)器上執(zhí)行下面的語句,用你的系統(tǒng)的實(shí)際值替換選項(xiàng)值
mysql> CHANGE MASTER TO -> MASTER_HOST='master_host_name', -> MASTER_USER='replication_user_name', -> MASTER_PASSWORD='replication_password', -> MASTER_LOG_FILE='recorded_log_file_name', -> MASTER_LOG_POS=recorded_log_position;
CHANGE MASTER TO MASTER_HOST='192.168.245.129', MASTER_USER='replication', MASTER_PASSWORD='slavepass';
mysql> CHANGE MASTER TO MASTER_HOST='192.168.245.129', MASTER_USER='repl', MASTER_PASSWORD='slavepass'; Query OK, 0 rows affected (0.14 sec)
啟動(dòng)從服務(wù)器線程
mysql> START SLAVE; Query OK, 0 rows affected (0.00 sec)
SLAVE STATUS
mysql> SHOW SLAVE STATUS\G *************************** 1. row *************************** Slave_IO_State: Connecting to master Master_Host: 192.168.245.129 Master_User: repl Master_Port: 3306 Connect_Retry: 60 Master_Log_File: Read_Master_Log_Pos: 4 Relay_Log_File: mysqld-relay-bin.000002 Relay_Log_Pos: 98 Relay_Master_Log_File: 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: 0 Relay_Log_Space: 98 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: NULL 1 row in set (0.00 sec)
登錄 master
復(fù)制進(jìn)程的信息
SHOW PROCESSLIST語句可以提供在主服務(wù)器上和從服務(wù)器上發(fā)生的關(guān)于復(fù)制的信息
mysql> SHOW PROCESSLIST\G *************************** 1. row *************************** Id: 62 User: replication Host: ken-hx409.local:36465 db: NULL Command: Binlog Dump Time: 679 State: Has sent all binlog to slave; waiting for binlog to be updated Info: NULL *************************** 2. row *************************** Id: 64 User: root Host: localhost db: NULL Command: Query Time: 0 State: NULL Info: SHOW PROCESSLIST 2 rows in set (0.00 sec)
登錄從庫,查看復(fù)制線程
mysql> SHOW PROCESSLIST\G *************************** 1. row *************************** Id: 273 User: root Host: localhost db: NULL Command: Query Time: 0 State: NULL Info: SHOW PROCESSLIST *************************** 2. row *************************** Id: 276 User: system user Host: db: NULL Command: Connect Time: 2 State: Waiting for master to send event Info: NULL *************************** 3. row *************************** Id: 277 User: system user Host: db: NULL Command: Connect Time: 2 State: Has read all relay log; waiting for the slave I/O thread to update it Info: NULL 3 rows in set (0.00 sec)
如果沒有復(fù)制進(jìn)程,請(qǐng)使用start slave;啟動(dòng)
mysql> SHOW PROCESSLIST\G *************************** 1. row *************************** Id: 273 User: root Host: localhost db: NULL Command: Query Time: 0 State: NULL Info: SHOW PROCESSLIST 1 row in set (0.02 sec) mysql> start slave; Query OK, 0 rows affected (0.00 sec)
登錄 master
mysql> insert into foo(id,data) values(2,'Hello world!!!'); Query OK, 1 row affected (0.00 sec)
登錄 slave
mysql> select * from foo;
在master服務(wù)器上插入一條記錄,你可以立刻在slave服務(wù)器上看到變化。
數(shù)據(jù)庫已經(jīng)存在的情況下怎么遷移
Master 鎖表禁止寫入新數(shù)據(jù)
mysql> FLUSH TABLES WITH READ LOCK;
Slave 停止復(fù)制進(jìn)程
mysql> stop slave;
備份Master數(shù)據(jù)庫
mysqldump yourdb | gzip > yourdb.sql.gz
恢復(fù)數(shù)據(jù)庫
如果使用mysqldump備份主服務(wù)器的數(shù)據(jù),將轉(zhuǎn)儲(chǔ)文件裝載到從服務(wù)器
# zcat yourdb.sql.gz | mysql -u root -p yourdb
啟動(dòng) Slave 復(fù)制進(jìn)程
mysql> start slave;
解除 Master 表鎖定
mysql> UNLOCK TABLES;
MyIASM引擎可以采用下面方法
備份數(shù)據(jù)庫
# tar zcvf mysql-snapshot.tar.gz /var/lib/mysql/neo
復(fù)制給從數(shù)據(jù)庫
scp mysql-snapshot.tar.gz neo@192.168.245.131:/tmp
snapshot 恢復(fù)
$ tar zxvf mysql-snapshot.tar.gz $ cd /var/lib/mysql $ mv /tmp/var/lib/mysql/neo . $ sudo chown mysql.mysql -R neo
重新啟動(dòng)Mysql
$ sudo /etc/init.d/mysql restart
有興趣可以看看mysqlhotcopy
復(fù)制賬號(hào)權(quán)限
grant replication slave on *.* to 'replication'@'192.168.1.%' identified by '000000';
主庫數(shù)據(jù)庫操作賬號(hào)權(quán)限
grant DELETE, INSERT, SELECT, UPDATE ON your_user.* to yourdb@'your_host' identified by 'password' with grant option;
從庫數(shù)據(jù)庫操作賬號(hào)權(quán)限
grant SELECT ON your_user.* to yourdb@'your_host' identified by 'password' with grant option;
從庫必須收回寫操作
執(zhí)行下面語句
stop slave; set global sql_slave_skip_counter =1 ; start slave;
Seconds_Behind_Master 值從NULL變?yōu)榇笥诘扔?是表示已經(jīng)同步
Seconds_Behind_Master: NULL Seconds_Behind_Master: 8893
my.cnf 文件加入下面的內(nèi)容
cp /etc/mysql/my.cnf /etc/mysql/my.cnf.old vim /etc/mysql/my.cnf [mysqld] server-id = 1 log-bin=/data/mysql/binlog/binlog binlog-do-db = test binlog-ignore-db=mysql log-slave-updates sync_binlog=1 auto_increment_offset=1 auto_increment_increment=2 replicate-do-db = test replicate-ignore-db = mysql,information_schema
創(chuàng)建復(fù)制權(quán)限
grant replication slave on *.* to 'replication'@'192.168.1.%' identified by '000000'; flush privileges;
mysql>flush tables with read lock; mysql> show master status\G *************************** 1. row *************************** File: binlog.000007 Position: 107 Binlog_Do_DB: test Binlog_Ignore_DB: mysql 1 row in set (0.00 sec)
創(chuàng)建復(fù)制權(quán)限
grant replication slave on *.* to 'replication'@'192.168.1.%' identified by '000000'; flush privileges;
my.cnf 文件加入下面的內(nèi)容
[mysqld] server-id = 2 log-bin = /data/mysql/binlog/binlog replicate-do-db = test replicate-ignore-db = mysql,information_schema binlog-do-db = test binlog-ignore-db=mysql log-slave-updates sync_binlog=1 auto_increment_offset=2 auto_increment_increment=2
B 與 A 配置文件不同的兩處。
server-id = 2 auto_increment_offset=2
mysql> show master status\G *************************** 1. row *************************** File: binlog.000005 Position: 107 Binlog_Do_DB: test Binlog_Ignore_DB: mysql 1 row in set (0.00 sec)
Master A,首先鎖表為只讀狀態(tài)
mysqldump --databases test > /tmp/test.sql
Master B 創(chuàng)建一個(gè)與Master A同名的空數(shù)據(jù)庫,然后將備份文件恢復(fù)到數(shù)據(jù)庫中
# mysql mysql> CREATE DATABASE test; mysql>\q # scp 192.168.1.1:/tmp/test.sql ./ # mysql -uroot -p test < /tmp/test.sql
master-A
mysql>change master to master_host='192.168.1.2', master_user='replication', master_password='000000', master_log_file='binlog.000005', master_log_pos=107;
master-B
mysql>change master to master_host='192.168.1.1', master_user='replication', master_password='000000', master_log_file='binlog.000007', master_log_pos=107;
Master A 解鎖
mysql> UNLOCK TABLES;
分別在Master A與B 上運(yùn)行
mysql>show slave status\G; Slave_IO_Running: Yes Slave_SQL_Running: Yes
缺省expire-logs-days為30天。這里設(shè)為7天,可根據(jù)自己情況調(diào)整。
[mysqld] expire-logs-days = 7
mysql> SHOW VARIABLES LIKE "have_dynamic_loading"; +----------------------+-------+ | Variable_name | Value | +----------------------+-------+ | have_dynamic_loading | YES | +----------------------+-------+ 1 row in set (0.00 sec) mysql>
master > INSTALL PLUGIN rpl_semi_sync_master SONAME ‘libsemisync_master.so’; slave-x > INSTALL PLUGIN rpl_semi_sync_slave SONAME ‘libsemisync_slave.so’; master > SET GLOBAL rpl_semi_sync_master_enabled=1; slave-x > SET GLOBAL rpl_semi_sync_slave_enabled=1;
CREATE USER 'replication'@'192.168.1.%' IDENTIFIED BY 'password'; GRANT ALL PRIVILEGES ON *.* TO 'replication'@'192.168.1.%' IDENTIFIED BY 'password' WITH GRANT OPTION MAX_QUERIES_PER_HOUR 0 MAX_CONNECTIONS_PER_HOUR 0 MAX_UPDATES_PER_HOUR 0 MAX_USER_CONNECTIONS 0 ;
master_heartbeat_period
STOP SLAVE; CHANGE MASTER TO master_heartbeat_period= milliseconds; START SLAVE;
mysql> CHANGE MASTER TO MASTER_HOST='xxx.xxx.xxx.xxx', MASTER_USER='root', MASTER_PASSWORD='password', MASTER_PORT=3306, MASTER_CONNECT_RETRY=10, MASTER_LOG_FILE='mysql-bin.000004', MASTER_LOG_POS=107;
跳過出問題的MySQL實(shí)例
CHANGE MASTER TO MASTER_HOST=xxx.xxx.xxx.xxx IGNORE_SERVER_IDS=y
編輯my.cnf加入
# On Master [mysqld] rpl_semi_sync_master_enabled=1 rpl_semi_sync_master_timeout=1000 # 1 second # On Slave [mysqld] rpl_semi_sync_slave_enabled=1
聯(lián)系客服