mysql雙機熱備方案

来源:https://www.cnblogs.com/lkgBlogs/archive/2017/12/25/8109523.html
-Advertisement-
Play Games

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卸載查看到的包 2.2安裝mysql 2.3初始化資料庫 進行初始化腳本 ...


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 {
     [email protected]
   }
   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

 


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • CleanMyMac 3是Mac系統中一個功能強大的應用程式清理軟體,它能夠優化Mac系統,合理釋放系統資源的開銷。它有一系列的工具,包括系統清理,在Mac上刪除應用程式的卸載程式,不留下任何東西,碎紙機刪除文件沒有痕跡,一套優化調整方案等等。 聽起來不錯?那麼,它也很好看。 CleanMyMac關 ...
  • 使用Linux的文件API,經常看見一個東西,叫做文件描述符. 什麼是文件描述符? (1)文件描述符其實實質是一個數字,這個數字在一個進程中表示一個特定的含義,當我們open打開一個文件時,操作系統在記憶體中構建了一些數據結構來表示這個動態文件,然後返回給應用程式一個數字作為文件描述符,這個數字就和我 ...
  • 原創2017-12-25創新教育研究中心TeachPlus C語言面試題 指針篇(一) 指針的使用,一直是c語言面試題中必考的部分, 因為指針本身使用的複雜性與普適性,所以考點非常多,而且也可以與其他知識相互結合, 因此我們將會使用五篇專題的篇幅來介紹指針。分析下麵的程式,指出程式中的錯誤: # i ...
  • 1.磁碟分區——fdisk 當一個新的硬碟需要加到系統中,我們需要做一下幾個步驟 (1) 對硬碟進行分區,以建立可用的partition(分隔槽) (2) 對分隔槽進行format(格式化),以建立可用的filesystem(文件系統) (3) 可以對文件系統進行檢驗 (4) 掛載到相應的目錄上才能 ...
  • Oracle索引詳解(一) 索引介紹   索引對於Oracle學習來說,非常重要,在數據量巨大的狀況下,使用恰到好處的索引,將會使得數據查詢時間大大減少,於2017/12/25暫時對Oracle中的索引進行一個大致的瞭解。 索引的創建語法 索引的特點 索引的不足 比較適合建立索引 ...
  • -- 查詢學生的最高分和最低分,並統計所以學生的最高分和最低分;SELECT sno,MAX(grade),MIN(grade) FROM sc GROUP BY sno WITH ROLLUP; with rollup:該查詢的最後結果出現在最後一行,並且統計了最後一行做了作為總算; (這是在我自 ...
  • 本篇文章是我在MVP直通車分享的關於SQL Server 2017的新功能,現在ppt分享如下,可以點擊這裡下載。 ...
  • 一、 實現功能 有時候在linux伺服器端, 會在mysql命令行中, 創建資料庫, 今天講一下怎麼在創建資料庫時, 把charset設置為utf8,collate設置為utf8_general_ci, 只要使用下麵的命令即可, CREATE DATABASE qinziheng DEFAULT C ...
一周排行
    -Advertisement-
    Play Games
  • 示例項目結構 在 Visual Studio 中創建一個 WinForms 應用程式後,項目結構如下所示: MyWinFormsApp/ │ ├───Properties/ │ └───Settings.settings │ ├───bin/ │ ├───Debug/ │ └───Release/ ...
  • [STAThread] 特性用於需要與 COM 組件交互的應用程式,尤其是依賴單線程模型(如 Windows Forms 應用程式)的組件。在 STA 模式下,線程擁有自己的消息迴圈,這對於處理用戶界面和某些 COM 組件是必要的。 [STAThread] static void Main(stri ...
  • 在WinForm中使用全局異常捕獲處理 在WinForm應用程式中,全局異常捕獲是確保程式穩定性的關鍵。通過在Program類的Main方法中設置全局異常處理,可以有效地捕獲並處理未預見的異常,從而避免程式崩潰。 註冊全局異常事件 [STAThread] static void Main() { / ...
  • 前言 給大家推薦一款開源的 Winform 控制項庫,可以幫助我們開發更加美觀、漂亮的 WinForm 界面。 項目介紹 SunnyUI.NET 是一個基於 .NET Framework 4.0+、.NET 6、.NET 7 和 .NET 8 的 WinForm 開源控制項庫,同時也提供了工具類庫、擴展 ...
  • 說明 該文章是屬於OverallAuth2.0系列文章,每周更新一篇該系列文章(從0到1完成系統開發)。 該系統文章,我會儘量說的非常詳細,做到不管新手、老手都能看懂。 說明:OverallAuth2.0 是一個簡單、易懂、功能強大的許可權+可視化流程管理系統。 有興趣的朋友,請關註我吧(*^▽^*) ...
  • 一、下載安裝 1.下載git 必須先下載並安裝git,再TortoiseGit下載安裝 git安裝參考教程:https://blog.csdn.net/mukes/article/details/115693833 2.TortoiseGit下載與安裝 TortoiseGit,Git客戶端,32/6 ...
  • 前言 在項目開發過程中,理解數據結構和演算法如同掌握蓋房子的秘訣。演算法不僅能幫助我們編寫高效、優質的代碼,還能解決項目中遇到的各種難題。 給大家推薦一個支持C#的開源免費、新手友好的數據結構與演算法入門教程:Hello演算法。 項目介紹 《Hello Algo》是一本開源免費、新手友好的數據結構與演算法入門 ...
  • 1.生成單個Proto.bat內容 @rem Copyright 2016, Google Inc. @rem All rights reserved. @rem @rem Redistribution and use in source and binary forms, with or with ...
  • 一:背景 1. 講故事 前段時間有位朋友找到我,說他的窗體程式在客戶這邊出現了卡死,讓我幫忙看下怎麼回事?dump也生成了,既然有dump了那就上 windbg 分析吧。 二:WinDbg 分析 1. 為什麼會卡死 窗體程式的卡死,入口門檻很低,後續往下分析就不一定了,不管怎麼說先用 !clrsta ...
  • 前言 人工智慧時代,人臉識別技術已成為安全驗證、身份識別和用戶交互的關鍵工具。 給大家推薦一款.NET 開源提供了強大的人臉識別 API,工具不僅易於集成,還具備高效處理能力。 本文將介紹一款如何利用這些API,為我們的項目添加智能識別的亮點。 項目介紹 GitHub 上擁有 1.2k 星標的 C# ...