不一样的思路,hosts文件在Mysql主从中的应用
本人水平有限,如有不足希望看到的人多多指教
目錄
1.1 實驗環境介紹
1.2進入從庫 Slave 1
1.3這里就假設Slave 1 為同步最快的,將其提升為主庫
1.4簡要說明(真對同步授權表主從結構)
1.5 從庫一切完畢,修改hosts文件
1.6下面我們就再設置另一個slave 讓他跟新的主庫同步
1.7下面測試我們的成果
?
?
神奇的hosts解析
???? 可以寫個shell腳本來實現下面的功能,這樣能盡量減少網站停機時間
實驗環境介紹
系統 Centos 5.5 32
數據庫 Mysql 5.0.16
環境介紹
10.0.0.57 ? mysql主庫
10.0.0.105? SLvae 1 模擬主庫宕機后提升為Master
10.0.0.103? Slave 2 做從庫
最近看群里人說
Mysql 主從同步 主庫掛了怎么辦?
1)網站測試環境
模擬用戶訪問網站
2)此刻我停掉主庫模擬主機宕機
root@mysql zongjie 16:30:07 # ifconfig eth0
eth0????? Link encap:Ethernet? HWaddr 00:0C:29:D6:F6:C7?
????????? inet addr:10.0.0.57? Bcast:10.0.0.255? Mask:255.255.255.0
????????? inet6 addr: fe80::20c:29ff:fed6:f6c7/64 Scope:Link
????????? UP BROADCAST RUNNING MULTICAST? MTU:1500? Metric:1
????????? RX packets:37460 errors:0 dropped:0 overruns:0 frame:0
????????? TX packets:38576 errors:0 dropped:0 overruns:0 carrier:0
????????? collisions:0 txqueuelen:1000
????????? RX bytes:5513260 (5.2 MiB)? TX bytes:24159718 (23.0 MiB)
????????? Interrupt:59 Base address:0x2000
?
root@mysql zongjie 16:30:14 # /etc/init.d/mysqld stop
Shutting down MySQL.....?????????????????????????????????? [確定]
用戶訪問
?
1.2進入從庫 Slave 1
①進入從庫操作。建議不要立刻停掉從庫,如果從主庫復制來的sql語句并未完全執行完,此時stop slave 肯定會丟失數據
②提升主庫的從庫選擇,選擇與主庫延遲最小的從庫提為主庫
可以查看
mysql> show processlist\G;
*************************** 1. row ***************************
???? Id: 1
?? User: system user
?? Host:
???? db: NULL
Command: Connect
?? Time: 30371
? State: Reconnecting after a failed master event read
這個參數表示,正在等待重新連接主庫
?? Info: NULL
*************************** 2. row ***************************
???? Id: 2
?? User: system user
?? Host:
???? db: NULL
Command: Connect
?? Time: -310998
? State: Has read all relay log; waiting for the slave I/O thread to update it
這個 自然就是本地的sql 線程了,這里的意思是等待I/O進程更新。
?? Info: NULL
*************************** 3. row ***************************
???? Id: 4
?? User: rep
?? Host: 10.0.0.105:56720
???? db: NULL
Command: Binlog Dump
?? Time: 30320
? State: Has sent all binlog to slave; waiting for binlog to be updated
因為這里我數據并不是很大,所以這里顯示已經,主服務器已經把二進制文件的更新發到從服務器上了,
線程現在很空閑,意思就是本地已經保了主服務器上所有的Binlog了
這樣,即使主庫掛了,但此時從庫上的數據是相對完整的。即使sql進程執行慢,也不用擔心數據的大量丟失
?? Info: NULL
*************************** 4. row ***************************
???? Id: 6
?? User: root
?? Host: localhost
???? db: NULL
Command: Query
?? Time: 0
? State: NULL
?? Info: show processlist
4 rows in set (0.00 sec)
?
ERROR:
No query specified
?
mysql> show slave status\G;
*************************** 1. row ***************************
?????????????? Slave_IO_State: Reconnecting after a failed master event read
????????????????? Master_Host: 10.0.0.57
????????????????? Master_User: rep
????????????????? Master_Port: 3306
??????????????? Connect_Retry: 60
????????????? Master_Log_File: mysql-bin.000007
????????? Read_Master_Log_Pos: 10552
?????????????? Relay_Log_File: relay-bin.000019
??????????????? Relay_Log_Pos: 10026
??????? Relay_Master_Log_File: mysql-bin.000007
????????? ???Slave_IO_Running: No
??????????? Slave_SQL_Running: Yes
????????????? Replicate_Do_DB:
????????? Replicate_Ignore_DB: mysql
?????????? 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: 10552
????????????? Relay_Log_Space: 10991
????????????? 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
Master_SSL_Verify_Server_Cert: No
??????????????? Last_IO_Errno: 2013
?????????? ?????Last_IO_Error: error reconnecting to master 'rep@10.0.0.57:3306' - retry-time: 60? retries: 86400
?????????????? Last_SQL_Errno: 0
?????????????? Last_SQL_Error:
1 row in set (0.01 sec)
?
ERROR:
No query specified
#################################Slave 2 #####################
mysql> show slave status\G;
*************************** 1. row ***************************
???????????? Slave_IO_State: Reconnecting after a failed master event read
???????????? ???Master_Host: 10.0.0.57
??????????????? Master_User: rep
??????????????? Master_Port: 3306
????????????? Connect_Retry: 60
??????????? Master_Log_File: mysql-bin.000007
??????? Read_Master_Log_Pos: 10552
???????????? Relay_Log_File: mysqld-relay-bin.000017
????????????? Relay_Log_Pos: 10018
????? Relay_Master_Log_File: mysql-bin.000007
??? ???????Slave_IO_Running: No
????????? 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: 10552
??????????? Relay_Log_Space: 10018
??????????? 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)
可以發現 i/o 進程已經停止了
下面就開始進行操作
1.3這里就假設Slave 1 為同步最快的,將其提升為主庫
①停掉slave
mysql> system ifconfig eth0
eth0????? Link encap:Ethernet? HWaddr 00:0C:29:89:45:B9?
????????? inet addr:10.0.0.105? Bcast:255.255.255.255? Mask:255.255.255.0
????????? inet6 addr: fe80::20c:29ff:fe89:45b9/64 Scope:Link
????????? UP BROADCAST RUNNING MULTICAST? MTU:1500? Metric:1
????????? RX packets:49614 errors:0 dropped:0 overruns:0 frame:0
????????? TX packets:32316 errors:0 dropped:0 overruns:0 carrier:0
????????? collisions:0 txqueuelen:1000
????????? RX bytes:17092794 (16.3 MiB)? TX bytes:5457395 (5.2 MiB)
????????? Interrupt:59 Base address:0x2000
?
mysql> stop slave; ????????
Query OK, 0 rows affected (0.00 sec)
②開啟從庫log-bin選項(從庫不建議開啟bin log文件)
[root@MYSQL 3307]# grep log-bin /data/3306/my.cnf
log-bin = /data/3306/mysql-bin
這里可能需要重啟下從庫
[root@MYSQL 3307]# /data/3306/mysql restart
Restarting MySQL...
Stoping MySQL...
Enter password:
Starting MySQL..
授權一個同步的用戶
mysql> grant replication slave on *.* to 'slave'@'10.0.0.%' identified by '123';
Query OK, 0 rows affected (0.01 sec)
1.4簡要說明(真對同步授權表主從結構)
###########簡要說明###################
如果這里有同步主庫的授權庫,可以不用重新授權一個用戶,
此時只需要在Slave 2 上進行如下設置即可
mysql> change master to MASTER_HOST='10.0.0.105',MASTER_PORT=3306;
ERROR 1198 (HY000): This operation cannot be performed with a running slave; run STOP SLAVE first
mysql> stop slave ;
Query OK, 0 rows affected (0.09 sec)
?
mysql> change master to MASTER_HOST='10.0.0.105',MASTER_PORT=3306;
Query OK, 0 rows affected (0.01 sec)
?
mysql> start slave;
Query OK, 0 rows affected (0.01 sec)
?
mysql> show slave status\G;
*************************** 1. row ***************************
???????????? Slave_IO_State: Waiting for master to send event
??????????????? Master_Host: 10.0.0.105
??????????????? Master_User: rep
??????????????? Master_Port: 3306
????????????? Connect_Retry: 60
??????????? Master_Log_File: mysql-bin.000008
??????? Read_Master_Log_Pos: 3785
???????????? Relay_Log_File: mysqld-relay-bin.000009
????????????? Relay_Log_Pos: 3922
????? Relay_Master_Log_File: mysql-bin.000008
?????????? Slave_IO_Running: Yes
????????? Slave_SQL_Running: Yes
?
############End#######################
這里有些人說要執行 reset master,感覺沒必要。畢竟一般的mysql主庫機器性能相對slave比較好,這樣等主庫維護后,還是要用主庫的
1.5從庫一切完畢,下面就該在web server 上做了
這里我將hosts 先備份下 方便比較
[root@APACHE etc]# cp hosts hosts.xp
[root@APACHE etc]# diff hosts hosts.xp
7c7
< 10.0.0.105? ??master.mysql
---
> 10.0.0.57? ???master.mysql
這里只是修改下master.mysql 地址
本著實用的原則,應該與開發人員協商好,分發hosts 文件對運維來說很簡單。對開發也方便,不用記IP地址。一舉兩得
此刻再次查看web
到這里已經能正常對外提供服務
1.6下面我們就再設置另一個slave 讓他跟新的主庫同步
mysql> change master to MASTER_HOST='10.0.0.105',MASTER_PORT=3306,MASTER_USER='slave',MASTER_PASSWORD='123';
ERROR 1198 (HY000): This operation cannot be performed with a running slave; run STOP SLAVE first
mysql> stop slave;
Query OK, 0 rows affected (0.00 sec)
?
mysql> change master to MASTER_HOST='10.0.0.105',MASTER_PORT=3306,MASTER_USER='slave',MASTER_PASSWORD='123';
Query OK, 0 rows affected (0.05 sec)
?
mysql> start slave ;
Query OK, 0 rows affected (0.00 sec)
?
mysql> show slave status\G;
*************************** 1. row ***************************
???????????? Slave_IO_State: Waiting for master to send event
??????????????? Master_Host: 10.0.0.105
??????????????? Master_User: slave
??????????????? Master_Port: 3306
????????????? Connect_Retry: 60
??????????? Master_Log_File: mysql-bin.000008
??????? Read_Master_Log_Pos: 3932
???????????? Relay_Log_File: mysqld-relay-bin.000009
????????????? Relay_Log_Pos: 4069
????? Relay_Master_Log_File: mysql-bin.000008
? ?????????Slave_IO_Running: Yes
????????? Slave_SQL_Running: Yes
在設置Slave 2 的時候 ,可以不需要指定bin log 文件的位置和位置點,畢竟slave 1 和slave 2 都跟MASTER 同步過,所以可以不用再次指定
1.7下面測試我們的成果
主庫創建庫操作
mysql> system ifconfig eth0
eth0????? Link encap:Ethernet? HWaddr 00:0C:29:89:45:B9?
????????? inet addr:10.0.0.105? Bcast:255.255.255.255? Mask:255.255.255.0
????????? inet6 addr: fe80::20c:29ff:fe89:45b9/64 Scope:Link
????????? UP BROADCAST RUNNING MULTICAST? MTU:1500? Metric:1
????????? RX packets:51542 errors:0 dropped:0 overruns:0 frame:0
????????? TX packets:36032 errors:0 dropped:0 overruns:0 carrier:0
????????? collisions:0 txqueuelen:1000
????????? RX bytes:17240206 (16.4 MiB)? TX bytes:10300211 (9.8 MiB)
????????? Interrupt:59 Base address:0x2000
?
mysql> create database testzhucong;
Query OK, 1 row affected (0.00 sec)
從庫驗證結果
mysql> system ifconfig eth0
eth0????? Link encap:Ethernet? HWaddr 00:0C:29:B9:80:C2?
????????? inet addr:10.0.0.103? Bcast:255.255.255.255? Mask:255.255.255.0
????????? inet6 addr: fe80::20c:29ff:feb9:80c2/64 Scope:Link
????????? UP BROADCAST RUNNING MULTICAST? MTU:1500? Metric:1
????????? RX packets:42409 errors:0 dropped:0 overruns:0 frame:0
????????? TX packets:28298 errors:0 dropped:0 overruns:0 carrier:0
????????? collisions:0 txqueuelen:1000
????????? RX bytes:44970006 (42.8 MiB)? TX bytes:2551998 (2.4 MiB)
????????? Interrupt:59 Base address:0x2000
?
mysql> show databases ;
+--------------------+
| Database?????????? |
+--------------------+
| information_schema |
| ceshi????????????? |
| hostceshi????????? |
| mysql????????????? |
| test?????????????? |
| testzhucong?? ?????|
+--------------------+
6 rows in set (0.08 sec)
轉載于:https://blog.51cto.com/8time/1047388
總結
以上是生活随笔為你收集整理的不一样的思路,hosts文件在Mysql主从中的应用的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 关于runjs的一些想法
- 下一篇: windows服务器apache+PHP