MySQL主从复制
一、现有架构存在的问题
随着互联网发展和兴起,当大量请求涌入的时候,单节点的应用服务器(tomcat)和单节点的数据库服务器(mysql),会因为处理速度过慢,或者出现极端情况导致宕机.
单节点故障
:外界不可抗因素,导致数据库服务器或者是应用服务器宕机,最终导致整个网站的瘫痪
单节点压力
:当大量请求涌入时,会耗尽服务器资源进而导致网站的瘫痪
集群(Cluster)
: 集多个服务器节点共同完成一项工作
节点(node)
:集群中的一个服务器
二、单节点的数据库服务器需要解决的问题
第一个问题 负载均衡
第二个问题 数据同步
本次搭建主从复制为三台服务器
centosA:主节点(master)
centosB:从节点(slave)
centosC:从节点(slave)
在三台服务器配置主机名映射
[root@centosA mysql]# vim /etc/hostname
localhost.localdomain
centosA
[root@centosB mysql]# vim /etc/hostname
localhost.localdomain
centosB
[root@centosC mysql]# vim /etc/hostname
localhost.localdomain
centosC
[root@centosX mysql]# vim /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.232.194 centosA
192.168.232.195 centosB
192.168.232.196 centosC
三、搭建主从复制
1、关闭网络防火墙
[root@centosB ~]# systemctl stop firewalld
[root@centosB ~]# systemctl disable firewalld
2、开启MySQL远程访问
a、登录MySQL
[root@centosB ~]# mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 23
Server version: 5.6.42-log MySQL Community Server (GPL)
Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
b、开启远程访问
mysql> use mysql;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> update user set host='%';
Query OK, 0 rows affected (0.00 sec)
Rows matched: 1 Changed: 0 Warnings: 0
mysql>
c、刷新权限
mysql> flush privileges;
Query OK, 0 rows affected (0.11 sec)
mysql>
3、配置主节点(master)
a、配置主节点配置文件
/etc/my.cnf三台服务器都需要配置,server-id需要唯一,三台服务器不同
[root@centosA mysql]# vim /etc/my.cnf
# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.6/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
user=mysql
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
server-id=1 #唯一标识,在主从复制里唯一
log-bin=mysql-bin #日志文件的名字
binlog-do-db=zpark #主从复制的数据库
binlog-do-db=baizhi #主从复制的数据库
binlog-ignore-db=mysql#不加入总从复制
binlog-ignore-db=test#不加入主从复制
expire_logs_days=10
auto_increment_increment=2
auto_increment_offset=1
# Recommended in standard MySQL setup
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
b、重启MySQL
[root@centosA mysql]# systemctl restart mysqld
c、登录到mysql客户端
[root@centosA mysql]# mysql -u root -p
检测配置是否生效
mysql> SHOW VARIABLES like 'server_id';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| server_id | 1 |
+---------------+-------+
1 row in set (0.00 sec)
mysql>
4、配置从节点(slave)
虚拟机需要先停掉备库实例,删除备库的auto.cnf文件,启动备库实例,此时备库就会产生一个新的auto.cnf文件(产生新的UUID)。重启mysql
[root@centosA mysql]# find / -name "auto.cnf"
[root@centosA mysql]# rm -rf /var/lib/mysql/auto.cnf
a、查看主机状态
mysql>show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 | 4314 | zpark,baizhi | mysql,test | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
b、在所用的从机中加入如下配置
mysql>change master to
master_host='192.168.65.101',
master_user='root',
master_password='123456',
master_log_file='mysql-bin.000001(master日志文件名)',
master_log_pos=4314(日志偏移量);
只需两台从机执行该命令
c、开启从机同步
mysql>start slave;
只需两台从机执行该命令
d、查看从机的同步状态
mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.232.194
Master_User: root
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 4314
Relay_Log_File: mysqld-relay-bin.000002
Relay_Log_Pos: 905
Relay_Master_Log_File: mysql-bin.000001
`Slave_IO_Running: Yes
Slave_SQL_Running: Yes`
Replicate_Do_DB: zpark,baizhi
Replicate_Ignore_DB: mysql,test
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: 4314
Relay_Log_Space: 1079
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: 9ba63592-69d2-11ea-9a3e-000c297bf0a0
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)
mysql>
只需两台从机执行该命令
Slave_IO_Running: Yes Slave_SQL_Running: Yes
都为yes则开启成功
如果需要停掉从机使用 stop slave;
向主节点添加数据测试是否成功,如果主节点数据发生改变,从机也改变,配置成功