侯体宗的博客
  • 首页
  • Hyperf版
  • beego仿版
  • 人生(杂谈)
  • 技术
  • 关于我
  • 更多分类
    • 文件下载
    • 文字修仙
    • 中国象棋ai
    • 群聊
    • 九宫格抽奖
    • 拼图
    • 消消乐
    • 相册

centos7 mariadb主从复制配置搭建详解步骤

linux  /  管理员 发布于 7年前   185

花了小一天的时间,终于实现了centos7 mariadb主从复制配置搭建,下面记录一下过程

环境:

虚拟机:vm8; centos7 版本:7.2.1511; mariadb 版本:centos7.2内置的

主库服务器: 10.69.5.200,CentOS 7,MariaDB 10已安装,有数据。

从库服务器1: 10.69.5.201,CentOS 7,MariaDB 10已安装,无应用数据。

主服务器配置

以下操作在主服务器192.168.71.151的/etc/my.cnf上进行。

1.修改配置文件,命令:vim /etc/my.cnf,输入下列代码:

[root@localhost ~]# cat /etc/my.cnf[mysqld]datadir=/var/lib/mysqlsocket=/var/lib/mysql/mysql.sock`# 新添加的部分# 配置主从时需要添加以下信息 startinnodb_file_per_table=NOlog-bin=/var/lib/mysql/master-bin #log-bin没指定存储目录,则是默认datadir指向的目录binlog_format=mixedserver-id=200 #每个服务器都需要添加server_id配置,各个服务器的server_id需要保证唯一性,实践中通常设置为服务器IP地址的最后一位#配置主从时需要添加以下信息 end `# Disabling symbolic-links is recommended to prevent assorted security riskssymbolic-links=0# Settings user and group are ignored when systemd is used.# If you need to run mysqld under a different user or group,# customize your systemd unit file for mariadb according to the# instructions in http://fedoraproject.org/wiki/Systemd[mysqld_safe]log-error=/var/log/mariadb/mariadb.logpid-file=/var/run/mariadb/mariadb.pid## include all files from the config directory#!includedir /etc/my.cnf.d

最后,:wq!保存退出

2.重启mariadb服务,输入命令

[root@localhost ~]# systemctl restart mariadb.service

3.登录mariadb

[root@localhost ~]# mysql -u root -padmin 

注:-p后是密码,中间没有空格

4.创建帐号并赋予replication的权限

从库,从主库复制数据时需要使用这个帐号进行

MariaDB [(none)]> GRANT REPLICATION SLAVE ON *.* TO 'root'@'10.69.5.%' IDENTIFIED BY 'admin';Query OK, 0 rows affected (0.00 sec)

5.备份数据库数据,用于导入到从数据库中

加锁

实际工作中,备份的时候是不让往库中写数据的,所以数据库要加锁,只能读

MariaDB [(none)]> FLUSH TABLES WITH READ LOCK;Query OK, 0 rows affected (0.00 sec)

记录主库log文件及其当前位置

MariaDB [(none)]> SHOW MASTER STATUS;+------------------+----------+--------------+------------------+| File       | Position | Binlog_Do_DB | Binlog_Ignore_DB |+------------------+----------+--------------+------------------+| mysql-bin.000001 |   694 |       |         |+------------------+----------+--------------+------------------+

记住File和Position的部分,从服务器会用到

备份数据,输入命令:

[root@localhost ~]# mysqldump -uroot -p --all-databases > /root/db.sql

解锁 主库

数据备份完成后,就可以释放主库上的锁:

MariaDB [(none)]> UNLOCK TABLES;Query OK, 0 rows affected (0.00 sec)

从服务器配置

以下在从服务器上的操作

1.导入主库的数据

[root@localhost ~]# mysql -uroot -p < db.sql

2.从服务器/etc/my.cnf配置,设置relay-log

my.cnf文件中添加一行relay_log=relay-bin

如果不设置,默认是按主机名 + “-relay-bin”生成relay log。

[root@localhost ~]# cat /etc/my.cnf[mysqld]datadir=/var/lib/mysqlsocket=/var/lib/mysql/mysql.sock# Disabling symbolic-links is recommended to prevent assorted security riskssymbolic-links=0`#配置主从时需要添加以下信息 startinnodb_file_per_table=NOserver-id=201 #一般与服务器ip的最后数字一致relay-log=/var/lib/mysql/relay-bin#配置主从时需要添加以下信息 end `# Settings user and group are ignored when systemd is used.# If you need to run mysqld under a different user or group,# customize your systemd unit file for mariadb according to the# instructions in http://fedoraproject.org/wiki/Systemd[mysqld_safe]log-error=/var/log/mariadb/mariadb.logpid-file=/var/run/mariadb/mariadb.pid## include all files from the config directory#!includedir /etc/my.cnf.d

3.重启服务

[root@localhost ~]# systemctl restart mariadb.service

4.登录mariadb

[root@localhost ~]# mysql -u root -padmin

5.设置主从复制

MariaDB [(none)]> CHANGE MASTER TO MASTER_HOST='10.69.5.200',MASTER_USER='root', MASTER_PASSWORD='admin', MASTER_LOG_FILE='master-bin.000001', MASTER_LOG_POS= 694;Query OK, 0 rows affected (0.02 sec)

这个命令完成以下几个任务:

a.设置当前服务器为主服务器(10.69.5.200)的从库

b.提供当前数据库(从库)从主库复制数据时所需的用户名和密码,即上面的GRANT REPLICATION SLAVE ON *.* TO 'root'@'10.69.5.%' IDENTIFIED BY 'admin';设置的

c.指定从库开始复制主库时需要使用的日志文件和文件位置,即上面主库执行SHOW MASTER STATUS;显示结果中的File和Position

6.开启主从复制

MariaDB [(none)]> START SLAVE;Query OK, 0 rows affected (0.00 sec)

7.查看从库状态

MariaDB [(none)]> show slave status\G*************************** 1. row ***************************        Slave_IO_State: Waiting for master to send event         Master_Host: 10.69.5.200         Master_User: root         Master_Port: 3306        Connect_Retry: 60       Master_Log_File: master-bin.000001     Read_Master_Log_Pos: 694        Relay_Log_File: relay-bin.000003        Relay_Log_Pos: 530    Relay_Master_Log_File: master-bin.000001       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: 694       Relay_Log_Space: 818       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: 0Master_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: 2001 row in set (0.00 sec)

注意:结果中Slave_IO_Running和Slave_SQL_Running必须为Yes,如果不是,需要根据提示的错误修改。

验证

主服务器:

MariaDB [(none)]> show databases;+--------------------+| Database      |+--------------------+| information_schema || mysql       || mytest       || performance_schema || test        |+--------------------+5 rows in set (0.04 sec)MariaDB [(none)]> use mytest;Reading table information for completion of table and column namesYou can turn off this feature to get a quicker startup with -ADatabase changedMariaDB [mytest]> select * from user;+----+------+| id | name |+----+------+| 1 | t  || 2 | t2  || 3 | t3  |+----+------+3 rows in set (0.00 sec)MariaDB [mytest]> insert into user(name) values('t4');Query OK, 1 row affected (0.01 sec)MariaDB [mytest]> select * from user;+----+------+| id | name |+----+------+| 1 | t  || 2 | t2  || 3 | t3  || 4 | t4  |+----+------+4 rows in set (0.00 sec)

查看从服务器数据是否变化:

MariaDB [(none)]> use mytest;Reading table information for completion of table and column namesYou can turn off this feature to get a quicker startup with -ADatabase changedMariaDB [mytest]> select * from user;+----+------+| id | name |+----+------+| 1 | t  || 2 | t2  |+----+------+2 rows in set (0.00 sec)MariaDB [mytest]> select * from user;+----+------+| id | name |+----+------+| 1 | t  || 2 | t2  || 4 | t4  |+----+------+3 rows in set (0.00 sec)

可以看到,从服务器更新了数据

搭建过程中遇到的问题及解决方法

问题1:从服务器设置主从复制出现错误:

MariaDB [mytest]> start slave;ERROR 1201 (HY000): Could not initialize master info structure; more error messages can be found in the MariaDB error log

发现 

Slave_IO_Running: NoSlave_SQL_Running: No

进一步发现我输入的是:CHANGE MASTER TO MASTER_HOST='192.168.71.151',MASTER_USER='slave_user', MASTER_PASSWORD='bigs3cret', MASTER_LOG_FILE='mysql-bin.000001', MASTER_LOG_POS= 469;

重新输入:MariaDB [(none)]> CHANGE MASTER TO MASTER_HOST='10.69.5.200',MASTER_USER='root', MASTER_PASSWORD='admin', MASTER_LOG_FILE='mysql-bin.000001', MASTER_LOG_POS= 469;
报错:ERROR 1201 (HY000): Could not initialize master info structure; more error messages can be found in the MariaDB error log

于是看错误日志:/var/log/mariadb/mariadb.log

错误日志的位置在/etc/my.cnf中配置:log-error=/

[root@localhost ~]# cat /var/log/mariadb/mariadb.log160915 12:52:02 [ERROR] Failed to open the relay log './mariadb-relay-bin.000001' (relay_log_pos 4)160915 12:52:02 [ERROR] Could not find target log during relay log initialization

通过查找答案: 删除/var/lib/mysql/路径下the ‘master.info' ‘mysqld-relay-bin.*' ‘relay-log.info' ‘relay-log-index.*'

运行命令:rm -rf master.info,rm -rf *relay*

重启服务:[root@localhost mysql]# systemctl restart mariadb.service

进入mariadb:

[root@localhost mysql]# mysql -u root -padminMariaDB [(none)]> flush logs;Query OK, 0 rows affected (0.00 sec)MariaDB [(none)]> reset slave;Query OK, 0 rows affected (0.00 sec)

重新设置主从复制关系:

MariaDB [(none)]> CHANGE MASTER TO MASTER_HOST='10.69.5.200',MASTER_USER='root', MASTER_PASSWORD='admin', MASTER_LOG_FILE='master-bin.000001', MASTER_LOG_POS= 694;Query OK, 0 rows affected (0.02 sec)

这次成功了。

MariaDB [(none)]> start slave;Query OK, 0 rows affected (0.01 sec)

查看从库状态:

MariaDB [(none)]> show slave status\G*************************** 1. row ***************************        Slave_IO_State: Connecting to master         Master_Host: 10.69.5.200         Master_User: root         Master_Port: 3306        Connect_Retry: 60       Master_Log_File: master-bin.000001     Read_Master_Log_Pos: 694        Relay_Log_File: relay-bin.000001        Relay_Log_Pos: 4    Relay_Master_Log_File: master-bin.000001      Slave_IO_Running: Connecting      Slave_SQL_Running: Yes  ・・・  ・・・  ・・・ Replicate_Ignore_Server_Ids:        Master_Server_Id: 01 row in set (0.00 sec)

发现问题2.Slave_IO_Running: Connecting

问题2.Slave_IO_Running: Connecting

查看错误日志

[root@localhost ~]# cat /var/log/mariadb/mariadb.log・・・160915 13:17:56 [Note] Slave SQL thread initialized, starting replication in log 'master-bin.000001' at position 694, relay log '/var/lib/mysql/relay-bin.000001' position: 4160915 13:17:56 [ERROR] Slave I/O: error connecting to master '[email protected]:3306' - retry-time: 60 retries: 86400 message: Can't connect to MySQL server on '10.69.5.200' (113), Error_code: 2003

这时运行telnet命令

[root@localhost ~]# telnet 10.69.5.200 3306

-bash: telnet: 未找到命令

安装telnet

[root@localhost ~]# yum -y install telnet-server.x86_64

安装成功后重启telnet服务

[root@localhost ~]# systemctl start telnet.socket[root@localhost ~]# systemctl enable telnet.socket[root@localhost ~]# telnet 10.69.5.200 3306

-bash: telnet: 未找到命令

还是不行

这回我reboot重启虚拟机,运行命令

注意:这回不是"yum -y install telnet-server.x86_64"了,这回没有telnet-server了

[root@localhost ~]# yum install telnet.x86_64

运行成功了

接着

[root@localhost ~]# systemctl enable telnet.socket[root@localhost ~]# systemctl start telnet.socket[root@localhost ~]# firewall-cmd --add-service=telnet --permanent success[root@localhost ~]# telnettelnet>

telnet终于安装成功了

从最新版本的centos7系统开始,默认的是 Mariadb而不是mysql!

使用系统自带的repos安装很简单:

yum install mariadb mariadb-server
  • systemctl start mariadb ==> 启动mariadb
  • systemctl enable mariadb ==> 开机自启动
  • mysql_secure_installation ==> 设置 root密码等相关
  • mysql -u root -p 123456 ==> 测试登录!

结束!

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。


  • 上一条:
    centOS7下mysql插入中文字符报错问题解决方法
    下一条:
    VMware虚拟机CentOS系统网络设置
  • 昵称:

    邮箱:

    0条评论 (评论内容有缓存机制,请悉知!)
    最新最热
    • 分类目录
    • 人生(杂谈)
    • 技术
    • linux
    • Java
    • php
    • 框架(架构)
    • 前端
    • ThinkPHP
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • swoole
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • 在Linux系统中使用Iptables实现流量转发功能流程步骤(0个评论)
    • vim学习笔记-入门级需要了解的一些快捷键(0个评论)
    • 在centos7系统中实现分区并格式化挂载一块硬盘到/data目录流程步骤(0个评论)
    • 在Linux系统种查看某一个进程所占用的内存命令(0个评论)
    • Linux中grep命令中的10种高级用法浅析(0个评论)
    • 近期文章
    • 在go语言中使用api.geonames.org接口实现根据国际邮政编码获取地址信息功能(1个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf分页文件功能(0个评论)
    • gmail发邮件报错:534 5.7.9 Application-specific password required...解决方案(0个评论)
    • 欧盟关于强迫劳动的规定的官方举报渠道及官方举报网站(0个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf文件功能(0个评论)
    • Laravel从Accel获得5700万美元A轮融资(0个评论)
    • 在go + gin中gorm实现指定搜索/区间搜索分页列表功能接口实例(0个评论)
    • 在go语言中实现IP/CIDR的ip和netmask互转及IP段形式互转及ip是否存在IP/CIDR(0个评论)
    • PHP 8.4 Alpha 1现已发布!(0个评论)
    • Laravel 11.15版本发布 - Eloquent Builder中添加的泛型(0个评论)
    • 近期评论
    • 122 在

      学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..
    • 123 在

      Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..
    • 原梓番博客 在

      在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..
    • 博主 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..
    • 1111 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
    • 2016-11
    • 2017-07
    • 2017-10
    • 2017-11
    • 2018-01
    • 2018-02
    • 2020-03
    • 2020-04
    • 2020-05
    • 2020-06
    • 2021-02
    • 2021-03
    • 2021-04
    • 2021-06
    • 2021-07
    • 2021-08
    • 2021-09
    • 2021-10
    • 2021-11
    • 2021-12
    • 2022-01
    • 2022-03
    • 2022-04
    • 2022-08
    • 2022-11
    • 2022-12
    • 2023-01
    • 2023-02
    • 2023-03
    • 2023-06
    • 2023-07
    • 2023-10
    • 2023-12
    • 2024-01
    • 2024-04
    Top

    Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号 PHP交流群

    侯体宗的博客