资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

mysql主主+keepalived高可用-创新互联

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是否飘移

另外有需要云服务器可以了解下创新互联cdcxhl.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。

专注于为中小企业提供成都网站制作、网站设计服务,电脑端+手机端+微信端的三站合一,更高效的管理,为中小企业乐都免费做网站提供优质的服务。我们立足成都,凝聚了一批互联网行业人才,有力地推动了超过千家企业的稳健成长,帮助中小企业通过网站建设实现规模扩充和转变。
网页名称:mysql主主+keepalived高可用-创新互联
URL链接:http://cdkjz.cn/article/dijjic.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220