mysql配置主從數(shù)據(jù)庫
MySQL的主從同步是一個很成熟的架構(gòu),優(yōu)點為:
①在從服務(wù)器可以執(zhí)行查詢工作(即我們常說的讀功能),降低主服務(wù)器壓力;
②在從主服務(wù)器進(jìn)行備份,避免備份期間影響主服務(wù)器服務(wù);
③當(dāng)主服務(wù)器出現(xiàn)問題時,可以切換到從服務(wù)器。
所以我在項目部署和實施中經(jīng)常會采用這種方案.
#我們在配置之前兩個服務(wù)器的數(shù)據(jù)庫不相同的話我們可以先遷移數(shù)據(jù)庫是兩邊的數(shù)據(jù)先相同!然后在配置主從
+遷移數(shù)據(jù)庫
mysqldump-h127.0.0.1-uroot-ppass myweb|mysql--host=***.***.***.*** -u數(shù)據(jù)庫用戶名 -p數(shù)據(jù)庫密碼 -C serweb
#將數(shù)據(jù)庫轉(zhuǎn)移到新服務(wù)器。此例為將本地數(shù)據(jù)庫myweb復(fù)制到遠(yuǎn)程數(shù)據(jù)庫名為serweb中,其中遠(yuǎn)程數(shù)據(jù)庫必須有名為serweb的數(shù)據(jù)庫
#在遷移過程中可能會報以下錯誤
mysqldump: Got error: 1045: Access denied for user 'csfdp'@'%' (using password: YES)
這時我們只要給用戶權(quán)限就好
grant all privileges on *.* to 'root'@'%' identified by 'root' with grant option;
使兩邊的數(shù)據(jù)相同后我們就可以配置主從同步了。
+ 數(shù)據(jù)庫目錄及其它
my.cnf配置文件 ????/etc/my.cnf
mysql數(shù)據(jù)庫位置 ???datadir=/usr/local/nginx/mysql
主數(shù)據(jù)庫:192.168.253.130
從數(shù)據(jù)庫:192.168.253.130
一、設(shè)置主庫
1、修改主庫my.cnf,主要是設(shè)置個不一樣的id和logbin(為了方便識別id我們可以設(shè)置為域名最后幾位數(shù)字)
[root@localhost etc]#vi /etc/my.cnf
# 記住這部分一定要配置在[mysqld]后面,否則無法找到從節(jié)點,各個配置項的含義可自己查閱文檔
log-bin=mysql-bin
binlog_format=mixed
server-id = 130
2、顯示主庫信息
mysql> show master status;
+------------------+----------+--------------+------------------+
| File? ? ? ? ? ? | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000045 |? ? 2575 |? ? ? ? ? ? ? |? ? ? ? ? ? ? ? ? |
+------------------+----------+--------------+------------------+
# 看到這個信息說明mysql的二進(jìn)制文件是從000045開始 ?磁盤的偏移量在2575
二、設(shè)置從庫
1、在220節(jié)點上修改從庫my.cnf(位置一樣)
log-bin=mysql-bin(開啟二進(jìn)制日志!根據(jù)個人習(xí)慣可開啟或不開啟,本人建議還是開啟,以防數(shù)據(jù)丟失時可以恢復(fù))
relay-log=mysql-relay(從庫的relay日志)
binlog_format=mixed()
server-id = 131
三、授權(quán)
#我們配置完主庫和從庫以后!我們要給兩個服務(wù)器簡歷鏈接!所以我們要授權(quán),出庫給從庫授權(quán)一個user從庫使用這個user建立起鏈接
1、grant replication slave on *.* to 'username'@'%' identified by 'your_password';
三、這是回到從庫設(shè)置
1、mysql> change master to ? ? ? ? ? ? ? ? ??
? ? ? ? ? ? ? ? ? ? master_host='192.168.253.130',
? ? ? ? ? ? ? ? ? ? master_user='username',
? ? ? ? ? ? ? ? ? ? master_password='your_password',
? ? ? ? ? ? ? ? ? ? master_log_file='mysql-bin.000045',master_log_pos=2575;
2、開啟
mysql>start slave;
3、查看從庫信息
mysql> show slave status \G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.253.130
Master_User: administrotar
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000045
Read_Master_Log_Pos: 2575
Relay_Log_File: mysql-relay.000002
Relay_Log_Pos: 1222
Relay_Master_Log_File: mysql-bin.000045
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: 2575
Relay_Log_Space: 1374
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:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 130
接下來就是測試了!可自己測試