MySQL主从

  • A+
所属分类:Linux

使用MySQL时随着时间的增长,用户量以及数据量的逐渐增加,访问量更是剧增,最终将会使MySQL达到某个瓶颈,那么MySQL的性能将会大大降低。那么如何跨过这个瓶颈,提高MySQL的并发量呢?方法有很多,分布式数据库、读写分离、高可用负载均衡、增加缓存服务器等等。其中实现高可用负载均衡的方法有很多,这里我们采用Keepalived+Mysql组合实现MySQL高可用负载均衡这一技术。

主机名 IP 安装服务
Mysql-Master 192.168.2.34 Mysql, Keepalived
Myysql-Slave 192.168.2.35 Mysql, Keepalived
VIP 192.168.2.30

一.Mysql

1.安装

2.修改配置文件

Mysql-master

[client]
port        = 3307
socket      = /data/mysql/mysql.sock
[mysqld]
port        = 3307
socket      = /data/mysql/mysql.sock
datadir = /data/mysql
character-set-server=utf8
collation-server=utf8_general_ci
skip-external-locking
key_buffer_size = 128M
max_allowed_packet = 32M
table_open_cache = 512
net_buffer_length = 8K
sort_buffer_size = 2M
read_buffer_size = 16M
read_rnd_buffer_size = 160M
myisam_sort_buffer_size = 32M
thread_cache_size = 64
query_cache_size= 64M
tmp_table_size = 64M
max_connections = 10240
max_user_connections = 10240
max_connect_errors = 10240
wait_timeout = 30
interactive_timeout = 30
long_query_time = 1
log-bin=mysql-bin
binlog_format=mixed
binlog-ignore-db = mysql,information_schema #忽略写入binlog日志的库
server-id   = 1
expire_logs_days = 10
early-plugin-load = ""
open_files_limit = 3000
log_slave_updates = 1
slow_query_log=1
slow_query_log_file=/var/log/mysql-slow.log
long_query_time=5
auto-increment-increment = 2 #字段变化增量值
auto-increment-offset = 1 #初始字段ID为1
#slave-skip-errors = all #忽略所有复制产生的错误

default_storage_engine = InnoDB
innodb_file_per_table = 1
innodb_data_home_dir = /data/mysql
innodb_data_file_path = ibdata1:10M:autoextend
innodb_log_group_home_dir = /data/mysql
innodb_buffer_pool_size = 512M
innodb_log_file_size = 128M
innodb_log_buffer_size = 8M
innodb_flush_log_at_trx_commit = 1
innodb_lock_wait_timeout = 50
sync_binlog = 1

[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
[mysqldump]
quick
max_allowed_packet = 32M
[mysql]
no-auto-rehash

[myisamchk]
key_buffer_size = 128M
sort_buffer_size = 2M
read_buffer = 2M
write_buffer = 2M

[mysqlhotcopy]
interactive-timeout

Mysql-slave:

[client]
port        = 3307
socket      = /data/mysql/mysql.sock
[mysqld]
port        = 3307
socket      = /data/mysql/mysql.sock
datadir = /data/mysql
character-set-server=utf8
collation-server=utf8_general_ci
skip-external-locking
key_buffer_size = 128M
max_allowed_packet = 32M
table_open_cache = 512
net_buffer_length = 8K
sort_buffer_size = 2M
read_buffer_size = 16M
read_rnd_buffer_size = 160M
myisam_sort_buffer_size = 32M
thread_cache_size = 64
query_cache_size= 64M
tmp_table_size = 64M
max_connections = 10240
max_user_connections = 10240
max_connect_errors = 10240
wait_timeout = 30
interactive_timeout = 30
long_query_time = 1
log-bin=mysql-bin
binlog_format=mixed
binlog-ignore-db = mysql,information_schema #忽略写入binlog日志的库
server-id   = 2
expire_logs_days = 10
early-plugin-load = ""
open_files_limit = 3000
log_slave_updates = 1
slow_query_log=1
slow_query_log_file=/var/log/mysql-slow.log
long_query_time=5
auto-increment-increment = 2 #字段变化增量值
auto-increment-offset = 1 #初始字段ID为1
#slave-skip-errors = all #忽略所有复制产生的错误

default_storage_engine = InnoDB
innodb_file_per_table = 1
innodb_data_home_dir = /data/mysql
innodb_data_file_path = ibdata1:10M:autoextend
innodb_log_group_home_dir = /data/mysql
innodb_buffer_pool_size = 512M
innodb_log_file_size = 128M
innodb_log_buffer_size = 8M
innodb_flush_log_at_trx_commit = 1
innodb_lock_wait_timeout = 50
sync_binlog = 1

[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
[mysqldump]
quick
max_allowed_packet = 32M
[mysql]
no-auto-rehash

[myisamchk]
key_buffer_size = 128M
sort_buffer_size = 2M
read_buffer = 2M
write_buffer = 2M

[mysqlhotcopy]
interactive-timeout

3.重启服务并添加端口

systemctl restart mysqld;
firewall-cmd --zone=public --add-port=3307/tcp --permanent;firewall-cmd --reload;

4.创建同步帐号(Master和Slave)

mysql -u root -p
GRANT REPLICATION SLAVE ON *.* TO 'sync'@'192.168.2.%' IDENTIFIED BY 'sync@123';
flush privileges;

5.查看log bin和pos值并配置
Mysql-master:

mysql> show master status;

MySQL主从

Mysql-slave:

mysql> show master status;

MySQL主从

Mysql-master: (log bin pos写slave的)

mysql> change master to master_host='192.168.2.35', master_user='sync',master_password='sync@123',master_port=3307,master_log_file='mysql-bin.000003',master_log_pos=608;
mysql> start slave;

Mysql-slave: (log bin pos写master的)

mysql> change master to master_host='192.168.2.34', master_user='sync',master_password='sync@123',master_port=3307,master_log_file='mysql-bin.000003',master_log_pos=608;
mysql> start slave;

6.查看是否搭建成功

mysql> show slave status \G
Slave_IO_Running: Yes
Slave_SQL_Running: Yes

二.Keepalived

1.安装

yum install keepalived -y

2.修改配置文件

Mysql-master:

mv /etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf.bak;
tee -a /etc/keepalived/keepalived.conf <<-'EOF'
! Configuration File for keepalived
global_defs {
   router_id MYSQL_HA
}
vrrp_instance VI_1 {
    state BACKUP
    interface eth0
    virtual_router_id 51
    priority 100
    advert_int 1
    nopreempt
    authentication {
         auth_type PASS
         auth_pass 2020
     }
     virtual_ipaddress {
     192.168.2.30/24
     }
}
virtual_server 192.168.2.30 3307 {
   delay_loop 6
   lb_algo wrr
   lb_kind DR
   persistence_timeout 50
   protocol TCP
real_server 192.168.1.34 3307 {
   weight 3
   notify_down /etc/keepalived/mysql.sh
   TCP_CHECK {
        connect_timeout 10
        nb_get_retry 3
        delay_before_retry 3
        connect_port 3307
             }
       }
 }
EOF

Mysql-slave:

mv /etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf.bak;
tee -a /etc/keepalived/keepalived.conf <<-'EOF'
! Configuration File for keepalived
global_defs {
   router_id MYSQL_HA
}
vrrp_instance VI_1 {
    state BACKUP
    interface eth0
    virtual_router_id 51
    priority 90
    advert_int 1
    authentication {
         auth_type PASS
         auth_pass 2020
     }
     virtual_ipaddress {
     192.168.2.30/24
     }
}
virtual_server 192.168.2.30 3307 {
   delay_loop 6
   lb_algo wrr
   lb_kind DR
   persistence_timeout 50
   protocol TCP
real_server 192.168.1.35 3307 {
   weight 3
   notify_down /etc/keepalived/mysql.sh
   TCP_CHECK {
        connect_timeout 10
        nb_get_retry 3
        delay_before_retry 3
        connect_port 3307
             }
       }
 }
EOF

3.关掉keepalived服务

tee -a /etc/keepalived/mysql.sh <<-'EOF'
#!/bin/bash
pkill keepalived
EOF

chmod +x /etc/keepalived/mysql.sh;

4.添加防火墙规则

firewall-cmd --direct --permanent --add-rule ipv4 filter INPUT 0 --in-interface eth0 --destination 224.0.0.18 --protocol vrrp -j ACCEPT;
firewall-cmd --reload;

5.启动服务

systemctl start keepalived;
systemctl enable keepalived;

 

 

avatar

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: