mysql主从复制读写分离与高可用配置

  

一、说明

  

前面我们说了mysql的安装配置(并提供一键安装脚本),mysql语句使用以及备份恢复mysql数据,本次要介绍的是mysql的主从复制,读写分离,及高可用尼古拉斯。
环境如下:
主:CentOS7_x64 mysql5.721 172.16.3.175 db1
slave1: CentOS7_x64 mysql5.7.21 172.16.3.235 db2
slave2: CentOS7_x64 mysql5.7.21 172.16.3.235 db4
proxysql/尼古拉斯:CentOS7_x64 mysql5.7.21 172.16.3.235 proxysql

  

<>强架构图:

  

 mysql主从复制读写分离与高可用配置

  

<强>说明:
配置测试时为了方便关闭了防火墙头,selinux安全策略,
现实中请开放防火墙策略,myslqdb的安装已经有脚本一键安装并配置好;这里就不在重复配置;只对对应的角色贴出对应的配置或安装与之相关的软件;

  

二、主从复制配置

  

一台主数据库,N从节点;从节点开启两个线程,通过Slave_IO_Running线程和主节点上有权限的账号从主数据库节点复制binlog日志到本地,能过Slave_SQL_Running线程在本地执行binlog日志,达到主从节点内容同步;

  

<强>主配置:
egrep - v”(^ $ | ^ #)”/usr/local/mysql/etc/my.cnf

  
 <代码> (mysqld)
  datadir=/data1/mysqldb
  套接字=/tmp/mysql.sock
  key_buffer_size=16米
  max_allowed_packet=16米
  thread_stack=192 k
  thread_cache_size=8
  query_cache_limit=1米
  query_cache_size=64
  query_cache_type=1
  符号链接=0
  innodb_file_per_table=对
  skip_name_resolve=对
  
  服务器id=1
  log_bin=/data1/mysqldb/mysql-bin.log
  (mysqld_safe)
  日志错误=/usr/地方/mysql/logs/error.log
  pid文件=/data1/mysqldb/mysql.pid
  ! includedir/usr/local/mysql/etc/my.cnf.d  
  

<>强创建从节点同步账号:

  
 <代码> mysql祝辞格兰特复制客户端,复制的奴隶,MASTER_USER=皉epluser MASTER_PASSWORD=皉eplpass MASTER_PORT=3306, MASTER_LOG_FILE=癿ysql-bin.000001 MASTER_LOG_POS=622; MASTER_CONNECT_RETRY=10, MASTER_HEARTBEAT_PERIOD=2;
  mysql祝辞开始的奴隶;#启动从节点()
  
  #查看从节点状态
  mysql祝辞显示奴隶状态;
  *************************** 1。行* * * * * * * * * * * * * * * * * * * * * * * * * * *
  Slave_IO_State:等待主人发送事件
  Master_Host: 172.16.3.175 #主节点
  Master_User: repluser #同步账号
  Master_Port: 3306
  Connect_Retry: 60
  Master_Log_File: mysql-bin.000001
  Read_Master_Log_Pos: 622
  Relay_Log_File: relay-log.000001
  Relay_Log_Pos: 582
  Relay_Master_Log_File: mysql-bin.000001
  Slave_IO_Running:是的#同步线程正常
  Slave_SQL_Running:是的#本地写线程正常
  Replicate_Do_DB: #同步过滤为空(可以只同步某个或某些库)
  Replicate_Ignore_DB: #不同步的库
  Replicate_Do_Table:
  Replicate_Ignore_Table:
  Replicate_Wild_Do_Table:
  Replicate_Wild_Ignore_Table:
  Last_Errno: 0 #最后同步的错误0表示正常同步
  Last_Error:
  Skip_Counter: 0
  Exec_Master_Log_Pos: 622
  Relay_Log_Space: 615
  Until_Condition:没有
  Until_Log_File:
  Until_Log_Pos: 0
  Master_SSL_Allowed:不
  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:不
  Last_IO_Errno: 0
  Last_IO_Error:
  Last_SQL_Errno: 0
  Last_SQL_Error:
  Replicate_Ignore_Server_Ids:
  Master_Server_Id: 1
  Master_UUID: 57017 c43 - 36 - e3 - 11 - e8 ac76 fc7——080027393
  Master_Info_File:/data1/mysqldb/master.info
  SQL_Delay: 0
  SQL_Remaining_Delay:零
  Slave_SQL_Running_State:奴隶读过所有继电器日志;等待更多的更新
  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行集(0.00秒)
  错误:
  没有指定查询
   
  

测试主从同步
在主导入测试数据,修改数据并查看奴隶中的数据是否一致。

  
 <代码> [root@db1 ~] # mysql & lt;Testdb.sql
  登录数据库
  root@db1 ~ # mysql -uroot - p
  mysql>显示数据库;
  +--------------------+
  | |数据库
  +--------------------+
  研究|
  +--------------------+
  5行集(0.00秒)
  # # #研究测试数据库导入成功
  mysql>使用研究;
  数据库修改
  mysql>显示表;
  + - - - - - - - - - - - - - - - - - - +
  | Tables_in_study |
  + - - - - - - - - - - - - - - - - - - +
  类| |
  课程| |
  | |一部分
  |积分|
  |学生|
  | tb31 |
  | tb32 |
  老师| |
  test1 | |
  | test2 |
  | user_info |
  + - - - - - - - - - - - - - - - - - - +
  11行集(0.00秒)
  #删除test1 test2表
  

mysql主从复制读写分离与高可用配置