mysql雙機熱備方案


1. 環境規划:

node1(mysql1)

192.168.10.94

node2(mysql2)

192.168.10.95

vip

192.168.10.222

數據庫

mysql-5.6.26

2.mysql安裝

2.1卸載查看到的包

1 #查看是否有已安裝的mysql,如果有卸載
2 rpm -qa|grep -mysql
3 
4 # 通常系統自帶mysql-libs,將其卸載
5 yum remove mysql-libs

2.2安裝mysql

1 tar -xvf MySQL-5.6.35-1.el6.x86_64.rpm-bundle.tar
2 
3 yum localinstall MySQL-client-5.6.35-1.el6.x86_64.rpm MySQL-server-5.6.35-1.el6.x86_64.rpm MySQL-devel-5.6.35-1.el6.x86_64.rpm 

2.3初始化數據庫

進行初始化腳本之前需要先啟動mysql服務和復制mysql的隨機密碼。在初始化總第一步將使用隨機密碼。

1 # 查看隨機初始密碼
2 cat /root/.mysql_secret
3 service mysql start
4 # 初始化mysql
5 /usr/bin/mysql_secure_installation

初始化:

 1 NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MySQL
 2 SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!
 3 
 4 In order to log into MySQL to secure it, we'll need the current
 5 password for the root user.  If you've just installed MySQL, and
 6 you haven't set the root password yet, the password will be blank,
 7 so you should just press enter here.
 8 
 9 Enter current password for root (enter for none):
10 OK, successfully used password, moving on...
11 
12 Setting the root password ensures that nobody can log into the MySQL
13 root user without the proper authorisation.
14 
15 You already have a root password set, so you can safely answer 'n'.
16 
17 Change the root password? [Y/n] Y
18 New password:
19 Re-enter new password:
20 Password updated successfully!
21 Reloading privilege tables..
22 ... Success!
23 
24 By default, a MySQL installation has an anonymous user, allowing anyone
25 to log into MySQL without having to have a user account created for
26 them.  This is intended only for testing, and to make the installation
27 go a bit smoother.  You should remove them before moving into a
28 production environment.
29 
30 Remove anonymous users? [Y/n] Y
31 ... Success!
32 
33 Normally, root should only be allowed to connect from 'localhost'.  This
34 ensures that someone cannot guess at the root password from the network.
35 
36 Disallow root login remotely? [Y/n] Y
37 ... Success!
38 
39 By default, MySQL comes with a database named 'test' that anyone can
40 access.  This is also intended only for testing, and should be removed
41 before moving into a production environment.
42 
43 Remove test database and access to it? [Y/n] Y
44 - Dropping test database...
45 ... Success!
46 - Removing privileges on test database...
47 ... Success!
48 
49 Reloading the privilege tables will ensure that all changes made so far
50 will take effect immediately.
51 
52 Reload privilege tables now? [Y/n] Y
53 ... Success!
54 
55 All done!  If you've completed all of the above steps, your MySQL
56 installation should now be secure.
57 
58 Thanks for using MySQL!
59 
60 
61 Cleaning up...

3.3修改mysql配置

yum安裝找不到/etc/my.cnf,先將配置文件拷貝到/etc下

1 cp /usr/share/mysql/my-default.cnf /etc/my.cnf
2 vim /etc/my.cnf

分別修改node1和node2上的/etc/my.cnf

[mysqld]
#生產環境要把落盤目錄放到掛載盤中
#修改此目錄需要把源目錄(/var/lib/mysql/)下的文件拷貝到此,並授權為mysql:mysql
#chown -R mysql:mysql mysql/
datadir=/data/mysql 
socket=/var/lib/mysql/mysql.sock
user=mysql
#主要兩個配置文件區別
server-id=1
log-bin=mysqlbin-log
symbolic-links=0
default-storage-engine=INNODB
character-set-server=utf8
collation-server=utf8_general_ci

[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid    #需要授權

[client]
default-character-set=utf8 

node2:只需要修改server-id

[mysqld]
#修改此目錄需要把源目錄(/var/lib/mysql/)下的文件拷貝到此,並授權為mysql:mysql
datadir=/data/mysql 
socket=/var/lib/mysql/mysql.sock
user=mysql
server-id=2
log-bin=mysqlbin-log
symbolic-links=0
default-storage-engine=INNODB
character-set-server=utf8
collation-server=utf8_general_ci

[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid    #需要授權

[client]
default-character-set=utf8

node1和node2重啟mysql

# /etc/init.d/mysql restart

3.4設置主主復制

node1設置:

 1 mysql>grant replication slave on *.* to slave@192.168.10.95 identified by '123456';
 2 mysql> flush privileges;
 3 
 4 mysql> change master to master_host='192.168.10.95', master_user='slave', master_password= ‘123456’;
 5 
 6 #啟動並查看狀態
 7 mysql> start slave;
 8 Query OK, 0 rows affected (0.01 sec)
 9 
10 mysql> show slave status\G;

顯示結果:

*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.10.95
Master_User: slave
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql2-bin.000003
Read_Master_Log_Pos: 408
Relay_Log_File: mysql1-relay-bin.000004
Relay_Log_Pos: 572
Relay_Master_Log_File: mysql2-bin.000003
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: 408
Relay_Log_Space: 910
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: 2
Master_UUID: 1343e450-b350-11e5-9ecb-005056b721a6
Master_Info_File: /var/lib/mysql/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

node2登錄mysql

 1 mysql>grant replication slave on *.* to slave@192.168.10.94 identified by '123456';
 2 mysql> flush privileges;
 3 
 4 mysql> change master to master_host='192.168.10.94', master_user='slave', master_password= ‘123456’;
 5 
 6 #啟動並查看狀態
 7 mysql> start slave;
 8 Query OK, 0 rows affected (0.01 sec)
 9 
10 mysql> show slave status\G;

顯示結果:

*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.10.94
Master_User: slave
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql1-bin.000004
Read_Master_Log_Pos: 408
Relay_Log_File: mysql2-relay-bin.000005
Relay_Log_Pos: 572
Relay_Master_Log_File: mysql1-bin.000004 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: 408
Relay_Log_Space: 910
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: 2b56309a-b350-11e5-9ecb-005056b77a1b
Master_Info_File: /var/lib/mysql/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

出現“Slave_IO_Running: Yes”和“Slave_SQL_Running: Yes”說明成功

如MySQL在主從復制的時候經常遇到錯誤而導致Slave復制中斷,這個時候就需要人工干涉,來跳過這個錯誤,才能使Slave端的復制,得以繼續進行;

跳過錯誤的方法:

mysql> STOP SLAVE;
mysql > SET GLOBAL  SQL_SLAVE_SKIP_COUNTER=1;
#跳過一個事務,可根據情況設置跳過多個錯誤。
mysql > SHOW GLOBAL VARIABLES LIKE 'SQL_SLAVE_SKIP_COUNTER';  +------------------------+-------+
| Variable_name          | Value |
+------------------------+-------+
| sql_slave_skip_counter | 1     |
+------------------------+-------+
mysql > START SLAVE;

3.5測試主主復制

在node1進入mysql:

1 mysql> create database world;
2 mysql> use world;
3 mysql> create table t1 ( id int );
4 mysql> insert into t1 values (1),(2),(3);

在node2可以看到已經建立的word數據庫和數據庫中t1表

mysql> use world;
mysql> select * from t1;
+------+
| id  |
+------+
|  1|
|  2 |
|  3 |
+------+
3 rows in set (0.00 sec)
再插入一行數據
mysql> insert into t1 values (4);

在node1可以看到新插入的一條數據

3.keepalived安裝

3.1通過yum安裝keepalived

yum install keepalived-1.2.13-5.el6_6.x86_64.rpm

3.2修改keepalived配置

在每個節點上備份keepalived配置文件,並設置。

1 cd /etc/keepalived
2 mv keepalived.conf keepalived.conf.bak
3 vim keepalived.conf

Node1設置配置文件:

! Configuration File for keepalived
 
global_defs {
   notification_email {
     root@lkgBlogs.com
   }
   notification_email_from keepalived@localhost  
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id LVS_DEVEL
}
 
vrrp_instance HA_1 {
    state BACKUP                #master和slave都配置為BACKUP
    interface eth0              #指定HA檢測的網絡接口
    virtual_router_id 80        #虛擬路由標識,主備相同
    priority 100                #定義優先級,slave設置90
    advert_int 1                #設定master和slave之間同步檢查的時間間隔
    nopreempt  #不搶占模式。只在優先級高的機器上設置即可
    authentication {
        auth_type PASS
        auth_pass 1111
    }
 
    virtual_ipaddress {                 #設置虛擬IP,可以設置多個,每行一個
        192.168.10.222       #MySQL對外服務的IP,即VIP
    }
}
 
virtual_server 192.168.10.222 3306 {
    delay_loop 2                    #每隔2秒查詢real server狀態
    lb_algo wrr                     #lvs 算法
    lb_kinf DR                      #LVS模式(Direct Route)
    persistence_timeout 50
    protocol TCP
 
    real_server 192.168.10.94 3306 {    #監聽本機的IP
        weight 1
        notify_down /etc/keepalived /mysql.sh
        TCP_CHECK {
        connect_timeout 10         #10秒無響應超時
        bingto 192.168.10.222
        nb_get_retry 3
        delay_before_retry 3
        connect_port 3306
        }
    }
 
}

node2服務器只修改priority為90、nopreempt不設置、real_server設置本地IP。

注意:

虛擬路由標識(virtual_router_id),主備相同。如果集群中有多個主從或者主主mysql,則需要將兩個集群的virtual_router_id設置成不同的值。

3.3添加mysql腳本

node1和node2上創建/etc/keepalived/mysql.sh: # vim /etc/keepalived/mysql.sh 

1 #!/bin/bash
2 pkill keepalived

node1和node2上均執行

1 chmod +x mysql.sh
2 # node1和node2均啟動keepalived
3 /etc/init.d/keepalived start

3.5測試

1.通過mysql客戶端登錄通過VIP11.11.168.222登錄MySQL,查看MySQL連接狀態

1 mysql> show variables like 'hostname%';

當殺死一個mysql后,自動轉移到另一個mysql上。

2.ip addr

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM