需求背景:
后臺(tái)統(tǒng)計(jì)數(shù)據(jù)和普通用戶都集中在同一個(gè)時(shí)間點(diǎn)訪問(wèn),并且統(tǒng)計(jì)數(shù)據(jù)大部分都是實(shí)時(shí)統(tǒng)計(jì)的,因而對(duì)服務(wù)器的壓力,特別是數(shù)據(jù)庫(kù)的壓力特別大。
第一個(gè)想到的改良思路是,先剝離后臺(tái)統(tǒng)計(jì)和普通用戶的操作,讓他們分別訪問(wèn)不同的服務(wù)器,只要數(shù)據(jù)是一致即可,這樣的壓力就分開(kāi)了。那第一步實(shí)現(xiàn)的方式就是 Mysql 主從備份。
當(dāng)前環(huán)境:
服務(wù)器:
阿里云ECS服務(wù)器
Mysql 版本
mysql? Ver 14.14 Distrib 5.1.73, for redhat-linux-gnu (x86_64) using readline 5.1
主要流程
1 配置Mysql主服務(wù)器
配置 只有備份權(quán)限的帳號(hào)
grant replication slave on *.* to 'copyuser1'@'ip 地址' identified by 'copyuser2016' with grant option;
導(dǎo)出數(shù)據(jù)庫(kù)
mysqldump -u root -p wx_wdy > /home/wx_wdy.sql
修改 my.cnf 配置文檔
[mysqld]
log_bin=mysql-bin
binlog-do-db=foodcase ?//需要同步的db
binlog-ignore-db=mysql ?//不需要同步的 db
binlog-ignore-db=wx_wdy
binlog-ignore-db=test
binlog-ignore-db=information_schema
server_id=1 ?//主服務(wù)器
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
character-set-server=utf8
user=mysql
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
?重啟mysql,并查看配置
mysql> show master status;

File :mysql-bin.000003
Position:13751?
這兩個(gè)值非常關(guān)鍵
后續(xù)從服務(wù)器配置就需要用上
2 配置Mysql從服務(wù)器
創(chuàng)建數(shù)據(jù)庫(kù)?
create database foodcase;
使用數(shù)據(jù)庫(kù)?
use foodcase;
導(dǎo)入數(shù)據(jù)?
source /home/db.sql;
測(cè)試是否可以連接主服務(wù)器?
[root@iZ94fzapvqdZ home]# mysql -u copyuser -h ip地址 -p
修改 my.cnf 配置文檔
[mysqld]
log_bin=mysql-bin
replicate-do-db=foodcase ?//要同步的 db
replicate-ignore-db=mysql //不需要同步的 db
replicate-ignore-db=wx_wdy
replicate-ignore-db=test
replicate-ignore-db=information_schema
read_only=1 //只讀
server_id=2 ?//從服務(wù)器
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
character-set-server=utf8
user=mysql
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
重啟 mysql,并關(guān)閉從服務(wù)器同步,配置,再打開(kāi)
mysql> slave stop;
配置和主服務(wù)器關(guān)聯(lián)讀取,上面得到的兩個(gè)字段就可以用上了
mysql> change master to master_host=' IP 地址', MASTER_USER='copy', MASTER_PASSWORD='copypassword', MASTER_LOG_FILE='mysql-bin.000003', MASTER_LOG_POS=13751;
打開(kāi)slave
mysql> slave start;
查看slave同步信息
mysql> SHOW SLAVE STATUS\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: IP 地址
Master_User: copyuser1
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000003
Read_Master_Log_Pos: 638775
Relay_Log_File: mysqld-relay-bin.000006
Relay_Log_Pos: 374949
Relay_Master_Log_File: mysql-bin.000003
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB: foodcase
Replicate_Ignore_DB: mysql,wx_wdy,test,information_schema
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: 638775
Relay_Log_Space: 386287
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
上面的 ?Slave_IO_Running,Slave_SQL_Running 都為 Yes 說(shuō)明配置成功了!
3. 測(cè)試
主服務(wù)器建一個(gè)表,同時(shí)在從服務(wù)器就看到了,這樣就是同步成功了。