基于AWS EC2,搭建MySQL5.7 一主多从架构

10 篇文章 0 订阅

1、项目背景

    由于AWS的RDS是收费的,所以在我的项目中,利用EC2搭建MySQL5.7一主多从方案。这里我的项目中,目前采用一主两从方案,为了高可用性,这里用了2个可用区域,可用区A,可用区B。

    准备的EC2机器如下:

                master  172.31.76.147 可用区A

                slave1  172.31.76.148  可用区A

                slave2 172.31.73.136   可用区B

2、rpm安装msql yum源 (所有机器执行)

在所有机器执行下面命令:

$> wget 'https://dev.mysql.com/get/mysql57-community-release-el7-11.noarch.rpm'
$> sudo rpm -Uvh mysql57-community-release-el7-11.noarch.rpm
$> yum repolist all | grep mysql
$> yum install -y mysql-community-server

 3、启动MySQL(所有机器执行)

在所有机器执行下面命令:

$> systemctl enable mysqld
$> systemctl start  mysqld
$> systemctl status mysqld

4、修改MySQL localhost临时密码(所有机器执行)

查看root临时密码:

$> grep "password" /var/log/mysqld.log

登录root账户:

$> mysql -uroot -p

修改root密码:

mysql> set global validate_password_policy=0;
mysql> alter  user 'root'@'localhost' identified by 'abc!@#$';

修改root账户能够远程访问:

 mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' WITH GRANT OPTION;

5、在写库master机器上创建同步用户

mysql> CREATE USER 'slave'@'%' IDENTIFIED BY '123456';

给同步账户slave授权,这里以同步smarthome数据库为例:

mysql> GRANT SELECT,UPDATE,INSERT,DELETE ON smarthome to 'slave'@'%';
mysql> GRANT ALL PRIVILEGES ON bosma-smart.* TO 'slave'@'%';
mysql> GRANT SELECT ON mysql.user TO 'slave'@'%';
mysql> GRANT SELECT ON mysql.db TO 'slave'@'%';
mysql> GRANT SELECT ON mysql.tables_priv TO 'slave'@'%';
mysql> GRANT SHOW DATABASES ON *.* TO 'slave'@'%';
mysql> flush privileges; 

6、永久关闭selinux(所有机器执行)

所有机器上执行:

$> vim /etc/selinux/config

将SELINUX=enforcing改为SELINUX=disabled,保存后退出

# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
#     enforcing - SELinux security policy is enforced.
#     permissive - SELinux prints warnings instead of enforcing.
#     disabled - No SELinux policy is loaded.
SELINUX=enforcing
# SELINUXTYPE= can take one of three two values:
#     targeted - Targeted processes are protected,
#     minimum - Modification of targeted policy. Only selected processes are protected. 
#     mls - Multi Level Security protection.
SELINUXTYPE=targeted

重启
此时获取当前selinux防火墙的安全策略仍为Enforcing,配置文件并未生效。

$> getenforce
Enforcing
重启机器
$> reboot
验证

$> /usr/sbin/sestatus
SELinux status:                 disabled
$> getenforce
Disabled

7、创建软链接(所有机器执行)

生产中,为了以后能够扩展磁盘空间,这里单独为数据库存储挂载了一个磁盘空间 /db。MySQL默认安装到/var/lib/mysql目录下。

这里利用软链接方式,解决MySQL目录迁移问题。

$> mv /var/lib/mysql /db/
$> cd /var/lib/
$> ln -s /db/mysql mysql
$> chown -R mysql:mysql /db

8、重启MySQL,检查上述目录迁移是否成功。(所有机器执行)

$>  systemctl restart mysqld
$>  systemctl status mysqld

9、配置写库master机器,修改/etc/my.cnf配置文件,具体配置如下:(master机器执行)

# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.7/en/server-configuration-defaults.html

[mysqld]
#
# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M
#
# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin
#
# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
wait_timeout=1800
interactive_timeout=1800
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
#skip-grant-tables
log-error=/db/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid

server-id=1            #配置server-id,让主服务器有唯一ID号
log-bin=mysql-bin   #打开Mysql日志,日志格式为二进制
skip-name-resolve=1   #关闭名称解析,(非必须)
lower_case_table_names=1 #不区分大小写
binlog_format=row
expire_logs_days=7
max_connections=10000
gtid_mode=ON #开启gtid,必须主从全开
enforce_gtid_consistency=1
log_slave_updates = 1 ##从服务器的更新是否写入二进制日志
master-verify-checksum = 1
log-slave-updates=ON
character_set_server=utf8mb4
max_binlog_size=100M
innodb_flush_log_at_trx_commit=1
sync_binlog=1
######semi sync replication settings########
plugin_dir=/usr/lib64/mysql/plugin
plugin_load = "rpl_semi_sync_master=semisync_master.so;rpl_semi_sync_slave=semisync_slave.so"
loose_rpl_semi_sync_master_enabled = 1
loose_rpl_semi_sync_slave_enabled = 1
loose_rpl_semi_sync_master_timeout = 5000
rpl_semi_sync_master_wait_point = AFTER_SYNC
rpl_semi_sync_master_wait_for_slave_count = 1
[client]
default_character-set=utf8

10、重启master机器的MySQL服务(在master机器执行)

mysql> systemctl restart mysql
mysql> systemctl status mysql

11、配置从库slave机器,修改配置/etc/my.cnf,具体配置如下:(所有slave机器执行)

[mysqld]
#
# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M
#
# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin
#
# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
server-id=2
gtid_mode=on
enforce_gtid_consistency=on
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
lower_case_table_names=1 #不区分大小写
log-error=/db/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
# slave上开启只读,避免应用误写导致主从数据不一致
read_only = on
#######replication settings########
log-bin=mysql-bin
log-slave-updates=1
log-slave-updates=1
binlog_format=row      #强烈建议,其他格式可能造成数据不一致
relay_log_recovery = 1
binlog_gtid_simple_recovery = 1
slave_skip_errors = ddl_exist_errors
#innodb_flush_log_at_trx_commit=1
#sync_binlog=1
#relay log
skip_slave_start=1
######semi sync replication settings########
plugin_dir=/usr/lib64/mysql/plugin
plugin_load = "rpl_semi_sync_master=semisync_master.so;rpl_semi_sync_slave=semisync_slave.so"
loose_rpl_semi_sync_master_enabled = 1
loose_rpl_semi_sync_slave_enabled = 1
loose_rpl_semi_sync_master_timeout = 5000
rpl_semi_sync_master_wait_point = AFTER_SYNC
rpl_semi_sync_master_wait_for_slave_count = 1

#####多线程并发处理######
slave-parallel-type=LOGICAL_CLOCK
slave-parallel-workers=16
master_info_repository=TABLE
relay_log_info_repository=TABLE

##忽略数据库
#replicate-wild-ignore-table=mysql.%
#replicate-wild-ignore-table=test.%
#replicate-wild-ignore-table=information_schema.%

10、重启slave机器的MySQL服务(在所有slave机器执行)

mysql> systemctl restart mysql
mysql> systemctl status mysql

11、查看master机器上MySQL master状态 (在master机器执行)

$> mysql -uroot -p
mysql> show master status;
+------------------+----------+--------------+------------------+-----------------------------------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set                             |
+------------------+----------+--------------+------------------+-----------------------------------------------+
| mysql-bin.000003 | 79163465 |              |                  | 40cdbb7e-44b8-11e6-92f8-00163e000af0:1-134905 |
+------------------+----------+--------------+------------------+-----------------------------------------------+
1 row in set (0.04 sec)

12、配置从库连接上写库。(在所有slave机器执行)

上面主库的文件:mysql-bin.000003,偏移位置:79163465。

mysql> change master to master_host='172.31.76.147',master_user='slave',master_password='123456',
 master_log_file='mysql-bin.000003',master_log_pos=79163465;
mysql> start slave;
mysql> SHOW SLAVE STATUS\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 172.31.76.147
                  Master_User: slave
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000003
          Read_Master_Log_Pos: 307
               Relay_Log_File: mysql-relay-bin.000002
                Relay_Log_Pos: 320
        Relay_Master_Log_File: mysql-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: 79163465
              Relay_Log_Space: 79163465
              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: 6c0f317c-8bcd-11e8-be49-028c605d1146
             Master_Info_File: /db/mysql/master.info
                    SQL_Delay: 0
          SQL_Remaining_Delay: NULL
      Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
           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
         Replicate_Rewrite_DB:
                 Channel_Name:
           Master_TLS_Version:
1 row in set (0.00 sec)

注意Slave_IO_Running和Slave_SQL_Running状态都为,如下所示,才表示主从配置成功:
Slave_IO_Running: Yes,Slave_SQL_Running: Yes

 

13、总结

       以上MySQL一主多从同步搭建成功。实际要应用主从同步架构,有两种方案,一是利用MySQL proxy工具做读写分离,二是在应用程序上做读写分离。起初本人在项目中利用MySQL proxy工具实现读写分离,运行一段时间,经常出现连接池连接问题。后来利用Spring做读写分离,在应用程序层面上做读写分离,就解决了这个问题。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值