如果对您有帮助请点赞,谢谢!一个赞至少让我开心一天。

CentOS8配置MySQL双主互备

  • 1安装操作系统centos8及配置
    • 启用SSH
  • 2安装MySQL及配置
    • 2.1下载镜像
    • 2.2安装镜像
    • 2.3升级系统上的MySQL软件包
    • 2.4安装MySQL服务
    • 2.5初始化数据库
    • 2.6启动服务
    • 2.7修改初始密码
    • 2.8重启MySQL服务
    • 2.9设置MySQL开机自启动
  • 3配置双主互备
    • 3.1 配置主从文件
      • 在172.29.60.145主机上操作
      • 在172.29.60.146主机上操作
    • 3.2创建互备专用用户(repl)
      • 在172.29.60.145主机上操作
      • 在172.29.60.146主机上操作
    • 3.3修改日志文件标记
      • 在172.29.60.145主机上操作
      • 在172.29.60.146主机上操作
      • 在172.29.60.145主机上操作
      • 在172.29.60.146主机上操作
    • 3.4校验是否设置成功
      • 在172.29.60.145主机上操作
      • 操作:172.29.60.146
  • 4异常情况
    • 4.1log file不同步

1安装操作系统centos8及配置

启用SSH

  1. 首先,要确保CentOS8安装了openssh-server,在终端中输入yum list installed | grep openssh-server
  2. 如果又没任何输出显示表示没有安装openssh-server,输入 yum install openssh-server
  3. 找到了/etc/ssh/目录下的sshd服务配置文件sshd_config,将#Port 22去掉注释
  4. 开启 sshd 服务,输入 sudo service sshd start
  5. 检查 sshd 服务是否已经开启,输入ps -e | grep sshd

2安装MySQL及配置

2.1下载镜像

wget https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm

2.2安装镜像

rpm -ivh mysql80-community-release-el7-3.noarch.rpm

2.3升级系统上的MySQL软件包

通过以下命令升级MySQL及其相关组件:
方案一.指定更新MySQL服务器(推荐使用):

yum update mysql-server
方案二.通过更新系统上的所有内容来更新MySQL(谨慎使用):
注意,这个方法需要谨慎使用,一般是在空白服务器的时候可以使用,因为有可能会把你系统中的其他软件都给升级了
yum update

2.4安装MySQL服务

yum install mysql-server

2.5初始化数据库

mysqld --initialize

2.6启动服务

service mysqld start

Job for mysqld.service failed because the control process exited with error code.
See “systemctl status mysqld.service” and “journalctl -xe” for details.

如果出现异常,执行以下语句:

chown mysql:mysql -R /var/lib/mysql
service mysqld start
systemctl status mysqld
● mysqld.service - MySQL 8.0 database serverLoaded: loaded (/usr/lib/systemd/system/mysqld.service; disabled; vendor preset: disabled)Active: active (running) since Thu 2019-09-26 17:42:23 CST; 6s agoProcess: 30399 ExecStopPost=/usr/libexec/mysql-wait-stop (code=exited, status=0/SUCCESS)Process: 30550 ExecStartPost=/usr/libexec/mysql-check-upgrade (code=exited, status=0/SUCCESS)Process: 30464 ExecStartPre=/usr/libexec/mysql-prepare-db-dir mysqld.service (code=exited, status=0/SUCCESS)Process: 30439 ExecStartPre=/usr/libexec/mysql-check-socket (code=exited, status=0/SUCCESS)Main PID: 30501 (mysqld)Status: "SERVER_OPERATING"Tasks: 38 (limit: 101224)Memory: 379.8MCGroup: /system.slice/mysqld.service└─30501 /usr/libexec/mysqld --basedir=/usr9月 26 17:42:22 hx-mysql-01 systemd[1]: Starting MySQL 8.0 database server...
9月 26 17:42:23 hx-mysql-01 systemd[1]: Started MySQL 8.0 database server.

2.7修改初始密码

获取密码

grep "password" /var/log/mysql/mysqld.log

2019-09-26T09:41:56.912605Z 5 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: ju7Ic#aQwyIJ

登陆

mysql -u root -p
ALTER USER 'root'@'%' IDENTIFIED BY '-你的密码-';
flush privileges;
exit;

2.8重启MySQL服务

service mysqld restart;

2.9设置MySQL开机自启动

systemctl enable mysqld


3配置双主互备

  • 主机1:172.29.60.145
  • 主机2:172.29.60.146

在安装172.29.60.145172.29.60.146完成MySQL的安装,切勿直接复制虚拟主机。

3.1 配置主从文件

在172.29.60.145主机上操作

修改/etc/my.cnf

[client-server]#
# include all files from the config directory
#
!includedir /etc/my.cnf.d
binlog-ignore-db=mysql,test,infromation_schema,sys,performance_schema
binlog-do-db=test1
master-host=172.29.60.146
master-port=3306
master-user=root
master-pass=KONGKONG
master-retry-count=999
master-connect-retry=60
[mysqld]
log-bin=bin-log
server-id=145

在172.29.60.146主机上操作

修改/etc/my.cnf

[client-server]#
# include all files from the config directory
#
!includedir /etc/my.cnf.d
binlog-ignore-db=mysql,test,infromation_schema,sys,performance_schema
binlog-do-db=test1
master-host=172.29.60.145
master-port=3306
master-user=root
master-pass=KONGKONG
master-retry-count=999
master-connect-retry=60
[mysqld]
log-bin=bin-log
server-id=146

3.2创建互备专用用户(repl)

在172.29.60.145主机上操作

mysql -u root -p

mysql> CREATE USER 'repl'@'172.29.60.146' IDENTIFIED WITH mysql_native_password BY 'KONGKONG';
mysql> GRANT REPLICATION SLAVE ON *.* TO 'repl'@'172.29.60.146';
mysql> flush privileges;

在172.29.60.146主机上操作

mysql -u root -p

mysql> CREATE USER 'repl'@'172.29.60.145' IDENTIFIED WITH mysql_native_password BY 'KONGKONG';
mysql> GRANT REPLICATION SLAVE ON *.* TO 'repl'@'172.29.60.145';
mysql> flush privileges;

3.3修改日志文件标记

在172.29.60.145主机上操作

mysql> SHOW MASTER STATUS;
+----------------+----------+--------------+------------------+-------------------+
| File           | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+----------------+----------+--------------+------------------+-------------------+
| bin-log.000002 |     3864 |              |                  |                   |
+----------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

在172.29.60.146主机上操作

mysql> SHOW MASTER STATUS;
+----------------+----------+--------------+------------------+-------------------+
| File           | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+----------------+----------+--------------+------------------+-------------------+
| bin-log.000003 |     2443 |              |                  |                   |
+----------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

在172.29.60.145主机上操作

STOP SLAVE;
CHANGE MASTER TO MASTER_HOST='172.29.60.146',MASTER_USER='repl',MASTER_PASSWORD='KONGKONG',MASTER_LOG_FILE='bin-log.000003',MASTER_LOG_POS=2443;
START SLAVE;

在172.29.60.146主机上操作

STOP SLAVE;
CHANGE MASTER TO MASTER_HOST='172.29.60.145',MASTER_USER='repl',MASTER_PASSWORD='KONGKONG',MASTER_LOG_FILE='bin-log.000002',MASTER_LOG_POS=3864;
START SLAVE;

3.4校验是否设置成功

  • Slave_IO_Running:Yes
  • Slave_SQL_Running: Yes
    若以上两个参数均为Yes则表示同步成功。

在172.29.60.145主机上操作

mysql> SHOW SLAVE STATUS;
+----------------------------------+---------------+-------------+-------------+---------------+-----------------+---------------------+------------------------------+---------------+-----------------------+------------------+-------------------+-----------------+---------------------+--------------------+------------------------+-------------------------+-----------------------------+------------+------------+--------------+---------------------+-----------------+-----------------+----------------+---------------+--------------------+--------------------+--------------------+-----------------+-------------------+----------------+-----------------------+-------------------------------+---------------+---------------+----------------+----------------+-----------------------------+------------------+--------------------------------------+-------------------------+-----------+---------------------+--------------------------------------------------------+--------------------+-------------+-------------------------+--------------------------+----------------+--------------------+--------------------+-------------------+---------------+----------------------+--------------+--------------------+------------------------+-----------------------+
| Slave_IO_State                   | Master_Host   | Master_User | Master_Port | Connect_Retry | Master_Log_File | Read_Master_Log_Pos | Relay_Log_File               | Relay_Log_Pos | Relay_Master_Log_File | 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 | Last_Error | Skip_Counter | Exec_Master_Log_Pos | Relay_Log_Space | Until_Condition | Until_Log_File | Until_Log_Pos | Master_SSL_Allowed | Master_SSL_CA_File | Master_SSL_CA_Path | Master_SSL_Cert | Master_SSL_Cipher | Master_SSL_Key | Seconds_Behind_Master | Master_SSL_Verify_Server_Cert | Last_IO_Errno | Last_IO_Error | Last_SQL_Errno | Last_SQL_Error | Replicate_Ignore_Server_Ids | Master_Server_Id | Master_UUID                          | Master_Info_File        | SQL_Delay | SQL_Remaining_Delay | Slave_SQL_Running_State                                | Master_Retry_Count | Master_Bind | Last_IO_Error_Timestamp | Last_SQL_Error_Timestamp | Master_SSL_Crl | Master_SSL_Crlpath | Retrieved_Gtid_Set | Executed_Gtid_Set | Auto_Position | Replicate_Rewrite_DB | Channel_Name | Master_TLS_Version | Master_public_key_path | Get_master_public_key |
+----------------------------------+---------------+-------------+-------------+---------------+-----------------+---------------------+------------------------------+---------------+-----------------------+------------------+-------------------+-----------------+---------------------+--------------------+------------------------+-------------------------+-----------------------------+------------+------------+--------------+---------------------+-----------------+-----------------+----------------+---------------+--------------------+--------------------+--------------------+-----------------+-------------------+----------------+-----------------------+-------------------------------+---------------+---------------+----------------+----------------+-----------------------------+------------------+--------------------------------------+-------------------------+-----------+---------------------+--------------------------------------------------------+--------------------+-------------+-------------------------+--------------------------+----------------+--------------------+--------------------+-------------------+---------------+----------------------+--------------+--------------------+------------------------+-----------------------+
| Waiting for master to send event | 172.29.60.146 | repl        |        3306 |            60 | bin-log.000004  |                 155 | hx-mysql-01-relay-bin.000002 |           320 | bin-log.000004        | Yes              | Yes               |                 |                     |                    |                        |                         |                             |          0 |            |            0 |                 155 |             534 | None            |                |             0 | No                 |                    |                    |                 |                   |                |                     0 | No                            |             0 |               |              0 |                |                             |              146 | 668ce869-f082-11e9-b456-ae0d77cbedee | mysql.slave_master_info |         0 |                NULL | Slave has read all relay log; waiting for more updates |              86400 |             |                         |                          |                |                    |                    |                   |             0 |                      |              |                    |                        |                     0 |
+----------------------------------+---------------+-------------+-------------+---------------+-----------------+---------------------+------------------------------+---------------+-----------------------+------------------+-------------------+-----------------+---------------------+--------------------+------------------------+-------------------------+-----------------------------+------------+------------+--------------+---------------------+-----------------+-----------------+----------------+---------------+--------------------+--------------------+--------------------+-----------------+-------------------+----------------+-----------------------+-------------------------------+---------------+---------------+----------------+----------------+-----------------------------+------------------+--------------------------------------+-------------------------+-----------+---------------------+--------------------------------------------------------+--------------------+-------------+-------------------------+--------------------------+----------------+--------------------+--------------------+-------------------+---------------+----------------------+--------------+--------------------+------------------------+-----------------------+
1 row in set (0.00 sec)

操作:172.29.60.146

mysql> SHOW SLAVE STATUS;
+----------------------------------+---------------+-------------+-------------+---------------+-----------------+---------------------+------------------------------+---------------+-----------------------+------------------+-------------------+-----------------+---------------------+--------------------+------------------------+-------------------------+-----------------------------+------------+------------+--------------+---------------------+-----------------+-----------------+----------------+---------------+--------------------+--------------------+--------------------+-----------------+-------------------+----------------+-----------------------+-------------------------------+---------------+---------------+----------------+----------------+-----------------------------+------------------+--------------------------------------+-------------------------+-----------+---------------------+--------------------------------------------------------+--------------------+-------------+-------------------------+--------------------------+----------------+--------------------+--------------------+-------------------+---------------+----------------------+--------------+--------------------+------------------------+-----------------------+
| Slave_IO_State                   | Master_Host   | Master_User | Master_Port | Connect_Retry | Master_Log_File | Read_Master_Log_Pos | Relay_Log_File               | Relay_Log_Pos | Relay_Master_Log_File | 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 | Last_Error | Skip_Counter | Exec_Master_Log_Pos | Relay_Log_Space | Until_Condition | Until_Log_File | Until_Log_Pos | Master_SSL_Allowed | Master_SSL_CA_File | Master_SSL_CA_Path | Master_SSL_Cert | Master_SSL_Cipher | Master_SSL_Key | Seconds_Behind_Master | Master_SSL_Verify_Server_Cert | Last_IO_Errno | Last_IO_Error | Last_SQL_Errno | Last_SQL_Error | Replicate_Ignore_Server_Ids | Master_Server_Id | Master_UUID                          | Master_Info_File        | SQL_Delay | SQL_Remaining_Delay | Slave_SQL_Running_State                                | Master_Retry_Count | Master_Bind | Last_IO_Error_Timestamp | Last_SQL_Error_Timestamp | Master_SSL_Crl | Master_SSL_Crlpath | Retrieved_Gtid_Set | Executed_Gtid_Set | Auto_Position | Replicate_Rewrite_DB | Channel_Name | Master_TLS_Version | Master_public_key_path | Get_master_public_key |
+----------------------------------+---------------+-------------+-------------+---------------+-----------------+---------------------+------------------------------+---------------+-----------------------+------------------+-------------------+-----------------+---------------------+--------------------+------------------------+-------------------------+-----------------------------+------------+------------+--------------+---------------------+-----------------+-----------------+----------------+---------------+--------------------+--------------------+--------------------+-----------------+-------------------+----------------+-----------------------+-------------------------------+---------------+---------------+----------------+----------------+-----------------------------+------------------+--------------------------------------+-------------------------+-----------+---------------------+--------------------------------------------------------+--------------------+-------------+-------------------------+--------------------------+----------------+--------------------+--------------------+-------------------+---------------+----------------------+--------------+--------------------+------------------------+-----------------------+
| Waiting for master to send event | 172.29.60.145 | repl        |        3306 |            60 | bin-log.000003  |                 155 | hx-mysql-02-relay-bin.000002 |           320 | bin-log.000003        | Yes              | Yes               |                 |                     |                    |                        |                         |                             |          0 |            |            0 |                 155 |             534 | None            |                |             0 | No                 |                    |                    |                 |                   |                |                     0 | No                            |             0 |               |              0 |                |                             |              145 | 323720bf-f083-11e9-bc77-4e1da18bea09 | mysql.slave_master_info |         0 |                NULL | Slave has read all relay log; waiting for more updates |              86400 |             |                         |                          |                |                    |                    |                   |             0 |                      |              |                    |                        |                     0 |
+----------------------------------+---------------+-------------+-------------+---------------+-----------------+---------------------+------------------------------+---------------+-----------------------+------------------+-------------------+-----------------+---------------------+--------------------+------------------------+-------------------------+-----------------------------+------------+------------+--------------+---------------------+-----------------+-----------------+----------------+---------------+--------------------+--------------------+--------------------+-----------------+-------------------+----------------+-----------------------+-------------------------------+---------------+---------------+----------------+----------------+-----------------------------+------------------+--------------------------------------+-------------------------+-----------+---------------------+--------------------------------------------------------+--------------------+-------------+-------------------------+--------------------------+----------------+--------------------+--------------------+-------------------+---------------+----------------------+--------------+--------------------+------------------------+-----------------------+

4异常情况

4.1log file不同步

Got fatal error 1236 from master when reading data from binary log: ‘Could not find first log file name in binary log index file’

  • log file不同步,重新执行步骤三

This operation cannot be performed with a running slave io thread; run STOP SLAVE IO_THREAD FOR CHANNEL ‘’ first.

  • 执行:stop slave;

The slave I/O thread stops because master and slave have equal MySQL server ids

修改/etc/my.cnf[client-server]
!includedir /etc/my.cnf.d
binlog-ignore-db=mysql,test,infromation_schema,sys,performance_schema
binlog-do-db=test1
master-host=172.29.60.146
master-port=3306
master-user=root
master-pass=***
master-retry-count=999
master-connect-retry=60
[mysqld]
log-bin=bin-log
server-id=145

Could not execute Write_rows event on table test1.user; Duplicate entry ‘12’

由于master要删除一条记录,而slave上找不到故报错,这种情况主上都将其删除了,那么从机可以直接跳过。可用命令:

stop slave;
set global sql_slave_skip_counter=1;
start slave;

CentOS8配置MySQL双主互备(安装centos8及MySQL)相关推荐

  1. mysql主备数据库配置_MySQL双主互备配置

    #主数据库配置 1.修改my.conf(windows下是my.ini)文件: 在[mysqld]部分插入如下两行: #开启二进制日志 log-bin=mysql-bin #设置server-id s ...

  2. 企业级-Mysql双主互备高可用负载均衡架构(基于GTID主从复制模式)(原创)

    前言: 原理与思想 这里选用GTID主从复制模式Mysql主从复制模式,是为了更加确保主从复制的正确性.健康性与易配性.这里做的是两服务器A,B各有Mysql实例3310,两个实例间互为主从 主从复制 ...

  3. linux 双mysql_MySQL双主互备+Keepalived高可用架构实现案例

    一.环境介绍 1.1.规划 序号 类别 版本 主机名 IP 端口 备注 1 OS CentOS release 6.9 (Final) (minimal) my1 172.16.210.180 830 ...

  4. mysql 5.6 双向互备_mysql双主互备

    5.则配置同步信息. change master to master_host='192.168.1.102',master_port=3306,master_user='replication',m ...

  5. ubuntu mysql双主热备配置_MySql双主热备配置

    用四台服务器配置MySql双主热备配置 主机1服务器ip:192.168.31.208 主机2服务器ip:192.168.31.133 从机1服务器ip:192.168.31.121 从机2服务器ip ...

  6. 高可用Mysql架构_Mysql主从复制、Mysql双主热备、Mysql双主双从、Mysql读写分离(Mycat中间件)、Mysql分库分表架构(Mycat中间件)的演变...

    [Mysql主从复制] 解决的问题 数据分布:比如一共150台机器,分别往电信.网通.移动各放50台,这样无论在哪个网络访问都很快.其次按照地域,比如国内国外,北方南方,这样地域性访问解决了. 负载均 ...

  7. keepalived mysql双主架构图_基于keepalived Mysql双主热备配置

    基于keepalived双主热备: 一.环境: OS:CentOS 6.5 X64 DB Version:Percona Mysql 5.7.15-9-log 路径:/app/mysql57 数据文件 ...

  8. mysql 双主 脑裂_MySQL 高可用性keepalived+mysql双主

    防伪码:明日复明日,明日何其多. 生产环境中一台mysql主机存在单点故障,所以我们要确保mysql的高可用性,即两台MySQL 服务器如果其中有一台 MySQL 服务器挂掉后,另外一台能立马接替其进 ...

  9. Nginx+keepalived双主配置(双机双主热备)

    简介 这种方案,使用两个VIP地址,前端使用2台机器,互为主备,同时有两台机器工作,当其中一台机器出现故障,两台机器的请求转移到一台机器负担,非常适合于生产架构环境. 一.网络拓扑 四台虚拟机如下所示 ...

最新文章

  1. slim php dd model,PHP全局使用Laravel辅助函数dump和dd
  2. QCustomPlot实现实时动态曲线(包含手动设置XY轴显示的方法)
  3. Eclipse SVN插件检出Src下面的包变成了文件夹解决
  4. javascript 15位和18位身份证的正则表达式及其验证
  5. 【设计模式系列】行为型之策略模式
  6. java jint_Android使用JNI实现Java与C之间传递数据
  7. AOSP 安卓源码-ubuntu开发环境搭建
  8. C++【递归】阿克曼函数
  9. 挠场的科学丨三、特斯拉所来不及知道的「挠场」
  10. word计算机板书,Word板书设计怎么写
  11. 三星董事长去世享年78岁,临终前最为遗憾的一件事
  12. 高中数学学习技巧,这几步你都做到了吗?
  13. node 常用 npm 命令
  14. THE TWENTY-EIGHTH DAY
  15. JavaWeb - 小米商城:商品添加到购物车
  16. 单元测试断言库:chai.js中文文档
  17. gunicorn flask的请求流程
  18. OA项目之项目简介会议发布
  19. java基础做简单的医院管理系统,附带课程设计说明书
  20. google map 离线数据

热门文章

  1. 电容电感阻抗模型分析和电源解耦电容选取经验
  2. 利用百度api实现ocr识别发票
  3. 寒武纪 4.5笔试题
  4. 【财富空间】小心!传统企业即将卷土重来!
  5. csm_eigen安装
  6. 美团收购摩拜,拉开出行领域的新一轮大战
  7. AI之机器翻译及相关技术
  8. 计算机模拟图像和数字,图像与数字图像基础
  9. VS Code上使用keil插件
  10. Python 笨鸟先飞小程序