中文字幕av专区_日韩电影在线播放_精品国产精品久久一区免费式_av在线免费观看网站

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

mysql主主+keepalived高可用

發布時間:2020-07-15 05:40:08 來源:網絡 閱讀:598 作者:浪客之心 欄目:MySQL數據庫

mysql主主+keepalived高可用

1.兩臺主機

host:
db1  外網:11.0.0.51  內網:172.16.1.51
db2  外網:11.0.0.52  內網:172.16.1.52

2.兩臺主機分別下載mysql

cd /usr/chris/src
wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.24-linux-glibc2.12-x86_64.tar
useradd mysql -u 550 -s /sbin/nologin -M
tar xf mysql-5.7.24-linux-glibc2.12-x86_64.tar.gz
mv mysql-5.7.24-linux-glibc2.12-x86_64 /usr/local/mysql-5.7.24

3.db1數據庫操作

ln -s /usr/local/mysql-5.7.24 /usr/local/mysql

vim /etc/my.cnf 
#begin
[mysqld]
 sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
datadir=/data/mysql/data
basedir=/usr/local/mysql

socket=/tmp/mysql.sock
user=mysql
symbolic-links=0

#slow log
slow-query-log=ON
slow_query_log_file=/data/mysql/data/log/mysql_slow_query.log
long_query_time=3
log_queries_not_using_indexes = OFF
log-slave-updates=true

gtid-mode=on
enforce-gtid-consistency=true

ft_min_word_len = 2
net_buffer_length = 8K
max_allowed_packet = 64M
wait_timeout = 180000
interactive_timeout=180000
thread_cache_size = 128
thread_stack = 512K
table_open_cache = 512
join_buffer_size    = 16M
sort_buffer_size    = 16M
log-error = /data/mysql/log/error.log

log_slave_updates = 1
#character_name:value
character-set-server=utf8  
collation-server=utf8_general_ci

log_bin = OFF
log-bin = mysql-bin
#relay-log = mysql-bin
server-id = 20
#auto_increment_offset = 2
#auto_increment_increment = 2
max_connections = 2000
group_concat_max_len = 2000
max_allowed_packet = 1024M
innodb_buffer_pool_size = 512M

read-only=0
binlog-ignore-db=mysql
binlog-ignore-db=information_schema
binlog-ignore-db=performance_schema
binlog-ignore-db=amh

[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
###end

4.創建數據目錄,修改啟動

mkdir /data/mysql/{data,log} -p
cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld

#啟動腳本修改
vim /etc/init.d/mysqld
basedir=/usr/local/mysql
datadir=/data/mysql/data

#環境變量修改(加入mysql路徑)
vim ~/.bash_profile
MYSQL_HOME=/usr/local/mysql
export PATH=$JAVA_HOME/bin:$PATH:$MYSQL_HOME/bin

source ~/.bash_profile
#初始化啟動
chown -R mysql:mysql /data/mysql
/usr/local/mysql/bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/data/mysql/data

#如果看見錯誤提示error while loading shared libraries: libaio.so.1,代表缺少了庫文件,安裝一下就可以了
yum install  libaio-devel.x86_64 -y

/etc/init.d/mysqld start

mysql -uroot -p
輸入初始密碼:
mysql> set password for root@localhost = PASSWORD('123456');
mysql> flush privileges;
###這里db1數據庫就配置完成了。

5.db2配置大致相同,就是配置文件需要修改

vim /etc/my.cnf
[mysqld]
sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION

datadir=/data/mysql/data
basedir=/usr/local/mysql

socket=/tmp/mysql.sock
user=mysql
symbolic-links=0

#slow log
slow-query-log=ON
slow_query_log_file=/data/mysql/data/log/mysql_slow_query.log
long_query_time=3
log_queries_not_using_indexes = OFF

log-slave-updates = true
ft_min_word_len = 2
net_buffer_length = 8K
max_allowed_packet = 64M
wait_timeout = 180000
interactive_timeout=180000
thread_cache_size = 128
thread_stack = 512K
table_open_cache = 512
join_buffer_size    = 16M
sort_buffer_size    = 16M
log-error = /data/mysql/log/error.log

log_slave_updates = 1
#character_name:value
character-set-server=utf8  
collation-server=utf8_general_ci

log_bin = OFF
log-bin = mysql-bin
#relay-log = mysql-bin
server-id = 21
#auto_increment_offset = 2
#auto_increment_increment = 2
max_connections = 2000
group_concat_max_len = 2000
max_allowed_packet = 1024M
innodb_buffer_pool_size = 512M

gtid-mode=on
enforce-gtid-consistency=true

read-only=0
binlog-ignore-db=mysql
binlog-ignore-db=information_schema
binlog-ignore-db=performance_schema
binlog-ignore-db=amh

[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid

#啟動修改密碼:
/etc/init.d/mysqld start
mysql -uroot -p
password:

mysql> set password for root@localhost = PASSWORD('123456');
mysql> flush privileges;

6.數據庫主主設置(互為主從)

--------------------------db1操作:--------------------------
mysql -uroot -p123456

mysql> grant replication slave on *.* to 'rep'@'172.16.1.52' identified by '123456';
mysql> flush privileges;
mysql> show master status\G
*************************** 1. row ***************************
                         File: mysql-bin.000015
                 Position: 234
         Binlog_Do_DB: 
 Binlog_Ignore_DB: mysql,information_schema,performance_schema,amh
Executed_Gtid_Set: 79a90ed3-fa86-11e7-ac66-000c29542a93:1-3,
9238586c-fa86-11e7-acab-000c29f6ed58:1-5
1 row in set (0.00 sec)

###【這一步需要自己查看主庫最新mysql-bin和pos,下面有查看】
mysql> change master to
         > master_host='172.16.1.52',
         > master_user='rep',
         > master_password='123456',
         > master_log_file='mysql-bin.000024',   #這個地方是主庫的最新mysql-bin
         > master_log_pos='234';                        #主庫的最新pos

mysql> start slave;
mysql> show slave status\G

###生產環境如果要修改數據庫架構不建議這么做,適合新部署,開啟gtid功能
###mysql> stop slave;
###mysql> change master to MASTER_AUTO_POSITION=1;

--------------------------db2操作:----------------------------
mysql> grant replication slave on *.* to 'rep'@'172.16.1.51' identified by '123456';
mysql> flush privileges;
mysql> show master status\G
*************************** 1. row ***************************
                         File: mysql-bin.000024
                 Position: 234
         Binlog_Do_DB: 
 Binlog_Ignore_DB: mysql,information_schema,performance_schema,amh
Executed_Gtid_Set: 79a90ed3-fa86-11e7-ac66-000c29542a93:1-3,
9238586c-fa86-11e7-acab-000c29f6ed58:1-5
1 row in set (0.00 sec)

###【這一步需要自己查看主庫最新mysql-bin和pos,上面有查看】
mysql> change master to
         > master_host='172.16.1.51',
         > master_user='rep',
         > master_password='123456',
         > master_log_file='mysql-bin.000015',
         > master_log_pos='234';

mysql> start slave;
mysql> show slave status\G

###生產環境不建議這么做,開啟gtid功能
###mysql> stop slave;
###mysql> change master to MASTER_AUTO_POSITION=1;

7.keepalived安裝配置

###db1和db2分別安裝
yum install keepalived -y

###db1的keepalived.conf配置
mkdir /etc/keepalived/scripts -p

vim /etc/keepalived/keepalived.conf
global_defs {
router_id db
}

vrrp_instance VI_1 {
        state MASTER
        interface eth2
        virtual_router_id 61
        priority 150
        advert_int 1
        nopreempt
        authentication {
                auth_type PASS
                auth_pass 1111
        }
        virtual_ipaddress {
                172.16.1.3
        }
}

virtual_server 172.16.1.3 3306 {
        delay_loop 2
        lb_algo wrr
        lb_kind DR 
        nat_mask 255.255.255.0
        persistence_timeout 50
        protocol TCP

        real_server 172.16.1.51 3306 {
                weight 3
                notify_down /etc/keepalived/scripts/mysql_check.sh
        TCP_CHECK {
                        connect_timeout 3
                        nb_get_retry 3
                        delay_before_retry 3
                        connect_port 3306
                }
        }
}
###此處還要配置一個觸發腳本,用來檢測數據庫是否存活(我的很簡單粗暴,你們自己可以寫連接數據庫判斷)
vim /etc/keepalived/scripts/mysql_check.sh
#!/bin/bash

Db1_Num=`netstat -luntp|grep 3306|wc -l`

if [ $Db1_Num -eq 0 ];then
        /etc/init.d/keepalived stop
        echo "Mysql vip is moved."
else
        echo "Mysql is running."
        exit 1
fi

###腳本需要加執行權限
chmod +x /etc/keepalived/scripts/mysql_check.sh

###db2的keepalived.conf配置
    global_defs {
        router_id db
}

vrrp_instance VI_1 {
        state BACKUP 
        interface eth2
        virtual_router_id 61
        priority 100
        advert_int 1
        nopreempt
        authentication {
                auth_type PASS
                auth_pass 1111
        }
        virtual_ipaddress {
                172.16.1.3
        }
}

virtual_server 172.16.1.3 3306 {
        delay_loop 2
        lb_algo wrr
        lb_kind DR 
        nat_mask 255.255.255.0
        persistence_timeout 50
        protocol TCP

        real_server 172.16.1.52 3306 {
                weight 3
                notify_down /etc/keepalived/scripts/mysql_check.sh
        TCP_CHECK {
                        connect_timeout 3
                        nb_get_retry 3
                        delay_before_retry 3
                        connect_port 3306
                }
        }
}

###觸發腳本同上(在環境配置一樣的情況下,類似端口都是3306,自己注意,還有腳本的執行權限)

8.老衲不測試了

###自己停掉數據庫查看vip是否飄移
向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

左贡县| 图片| 成都市| 博白县| 巴南区| 老河口市| 繁昌县| 东乌珠穆沁旗| 唐海县| 开平市| 邵阳市| 拜泉县| 稻城县| 临汾市| 凭祥市| 柏乡县| 宝丰县| 万州区| 赤峰市| 泰宁县| 浦县| 秭归县| 惠东县| 德阳市| 西昌市| 九寨沟县| 东莞市| 许昌市| 东宁县| 吕梁市| 丁青县| 和平县| 古田县| 城市| 扎囊县| 周口市| 长子县| 金塔县| 都江堰市| 周宁县| 简阳市|