MySQL-Jira雙機熱備
主服務器:192.168.1.23
從服務器:192.168.1.243
一、主服務器Master配置
1. 創建同步賬號、賦權
在主服務器上為從服務器建立一個連接帳戶,此處用root,該帳戶必須授予REPLICATION SLAVE權限。因為從mysql版本3.2以後就可以通過REPLICATION對其進行雙機熱備的功能操作。
mysql> grant replication slave on *.* to ‘root‘@‘192.168.1.243‘ identified by ‘123456‘; mysql> flush privileges;
創建好同步連接帳戶後,我們可以通過在從服務器(Slave)上用replicat帳戶對主服務器(Master)數據庫進行訪問下,看下是否能連接成功。
在從服務器(Slave)上輸入如下指令:
[[email protected] ~]# mysql -h192.168.1.23 -uroot -p123456
2. 修改Mysql配置文件
找到mysql配置所有在目錄,一般在安裝好mysql服務後,都會將配置文件復制一一份出來放到/ect目錄下面,並且配置文件命名為:my.cnf。即配置文件準確目錄為/etc/my.cnf
項目裏的mysql配置文件在/opt/product/mysql目錄下。
[mysqld] server-id = 1 log-bin=mysql-bin binlog-do-db = jira binlog-ignore-db = mysql
3. 重啟mysql服務
/etc/init.d/myslq restart
4. 查看主服務器狀態
mysql> flush tables with read lock; mysql> show master status \G; *************************** 1. row *************************** File: mysql-bin.000002 Position: 1290 Binlog_Do_DB: jira Binlog_Ignore_DB: mysql Executed_Gtid_Set:1 row in set (0.00 sec) ERROR: No query specified
註意看裏面的參數,特別前面兩個File和Position,在從服務器(Slave)配置主從關系會有用到的。
註:這裏使用了鎖表,目的是為了產生環境中不讓進新的數據,好讓從服務器定位同步位置,初次同步完成後,記得解鎖。
mysql> unlock tables;
二、從服務器Slave配置
1.修改配置文件
因為這裏面是以主-從方式實現mysql雙機熱備的,所以在從服務器就不用在建立同步帳戶了,直接打開配置文件my.cnf進行修改即可,道理還是同修改主服務器上的一樣,只不過需要修改的參數不一樣而已。如下:
[mysqld] server-id = 2 log-bin=mysql-bin replicate-do-db = jira replicate-ignore-db = mysql,information_schema,performance_schema
2.重啟mysql服務
修改完配置文件後,保存後,重啟一下mysql服務,如果成功則沒問題。
3.用change mster 語句指定同步位置
這步是最關鍵的一步了,在進入mysql操作界面後,輸入如下指令:
//先停步slave服務線程,這個是很重要的,如果不這樣做會造成以下操作不成功。 mysql>stop slave; mysql>change master to master_host=‘192.168.1.23‘,master_user=‘root‘,master_password=‘123456‘,master_log_file=‘ mysql-bin.000002 ‘,master_log_pos=1290;
mysql>start slave;
註:master_log_file, master_log_pos由主服務器(Master)查出的狀態值中確定。也就是上面剛剛叫註意的。master_log_file對應File, master_log_pos對應Position。Mysql 5.x以上版本已經不支持在配置文件中指定主服務器相關選項。
遇到的問題,如果按上面步驟之後還出現如下情況:
4.查看從服務器(Slave)狀態
mysql> show slave status\G; *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.1.14 Master_User: root Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000002 Read_Master_Log_Pos: 1290 Relay_Log_File: ha-db-relay-bin.000002 Relay_Log_Pos: 1453 Relay_Master_Log_File: mysql-bin.000002 Slave_IO_Running: Yes Slave_SQL_Running: Yes Replicate_Do_DB: jira Replicate_Ignore_DB: mysql,information_schema,performance_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: 1290 Relay_Log_Space: 1626 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: 1 Master_UUID: 08337b95-6e04-11e7-a595-000c2994f6ec Master_Info_File: /opt/product/mysql/data/master.info SQL_Delay: 0 SQL_Remaining_Delay: NULL Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it Master_Retry_Count: 86400 Master_Bind: Last_IO_Error_Timestamp: Last_SQL_Error_Timestamp: Master_SSL_Crl: Master_SSL_Crlpath: Retrieved_Gtid_Set: Executed_Gtid_Set: Auto_Position: 0 1 row in set (0.00 sec) ERROR: No query specified
查看下面兩項值均為Yes,即表示設置從服務器成功。
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
三、測試同步
參考:http://blog.csdn.net/huaweitman/article/details/50853075
MySQL-Jira雙機熱備