音效素材网提供各类素材,打造精品素材网站!

站内导航 站长工具 投稿中心 手机访问

音效素材

centos7 mariadb主从复制配置搭建详解步骤
日期:2017-02-08 08:59:58   来源:脚本之家

花了小一天的时间,终于实现了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/mysql
socket=/var/lib/mysql/mysql.sock

`# 新添加的部分
# 配置主从时需要添加以下信息 start
innodb_file_per_table=NO
log-bin=/var/lib/mysql/master-bin #log-bin没指定存储目录,则是默认datadir指向的目录
binlog_format=mixed
server-id=200 
#每个服务器都需要添加server_id配置,各个服务器的server_id需要保证唯一性,实践中通常设置为服务器IP地址的最后一位
#配置主从时需要添加以下信息 end 
`
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-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.log
pid-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/mysql
socket=/var/lib/mysql/mysql.sock
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0

`#配置主从时需要添加以下信息 start
innodb_file_per_table=NO
server-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.log
pid-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: 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: 200
1 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 names
You can turn off this feature to get a quicker startup with -A

Database changed
MariaDB [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 names
You can turn off this feature to get a quicker startup with -A

Database changed
MariaDB [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: No
Slave_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.log
160915 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 -padmin

MariaDB [(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: 0
1 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: 4
160915 13:17:56 [ERROR] Slave I/O: error connecting to master 'root@10.69.5.200: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 ~]# telnet
telnet>

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 ==> 测试登录!

结束!

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

    您感兴趣的教程

    在docker中安装mysql详解

    本篇文章主要介绍了在docker中安装mysql详解,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编...

    详解 安装 docker mysql

    win10中文输入法仅在桌面显示怎么办?

    win10中文输入法仅在桌面显示怎么办?

    win10系统使用搜狗,QQ输入法只有在显示桌面的时候才出来,在使用其他程序输入框里面却只能输入字母数字,win10中...

    win10 中文输入法

    一分钟掌握linux系统目录结构

    这篇文章主要介绍了linux系统目录结构,通过结构图和多张表格了解linux系统目录结构,感兴趣的小伙伴们可以参考一...

    结构 目录 系统 linux

    PHP程序员玩转Linux系列 Linux和Windows安装

    这篇文章主要为大家详细介绍了PHP程序员玩转Linux系列文章,Linux和Windows安装nginx教程,具有一定的参考价值,感兴趣...

    玩转 程序员 安装 系列 PHP

    win10怎么安装杜比音效Doby V4.1 win10安装杜

    第四代杜比®家庭影院®技术包含了一整套协同工作的技术,让PC 发出清晰的环绕声同时第四代杜比家庭影院技术...

    win10杜比音效

    纯CSS实现iOS风格打开关闭选择框功能

    这篇文章主要介绍了纯CSS实现iOS风格打开关闭选择框,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作...

    css ios c

    Win7如何给C盘扩容 Win7系统电脑C盘扩容的办法

    Win7如何给C盘扩容 Win7系统电脑C盘扩容的

    Win7给电脑C盘扩容的办法大家知道吗?当系统分区C盘空间不足时,就需要给它扩容了,如果不管,C盘没有足够的空间...

    Win7 C盘 扩容

    百度推广竞品词的投放策略

    SEM是基于关键词搜索的营销活动。作为推广人员,我们所做的工作,就是打理成千上万的关键词,关注它们的质量度...

    百度推广 竞品词

    Visual Studio Code(vscode) git的使用教程

    这篇文章主要介绍了详解Visual Studio Code(vscode) git的使用,小编觉得挺不错的,现在分享给大家,也给大家做个参考。...

    教程 Studio Visual Code git

    七牛云储存创始人分享七牛的创立故事与

    这篇文章主要介绍了七牛云储存创始人分享七牛的创立故事与对Go语言的应用,七牛选用Go语言这门新兴的编程语言进行...

    七牛 Go语言

    Win10预览版Mobile 10547即将发布 9月19日上午

    微软副总裁Gabriel Aul的Twitter透露了 Win10 Mobile预览版10536即将发布,他表示该版本已进入内部慢速版阶段,发布时间目...

    Win10 预览版

    HTML标签meta总结,HTML5 head meta 属性整理

    移动前端开发中添加一些webkit专属的HTML5头部标签,帮助浏览器更好解析HTML代码,更好地将移动web前端页面表现出来...

    移动端html5模拟长按事件的实现方法

    这篇文章主要介绍了移动端html5模拟长按事件的实现方法的相关资料,小编觉得挺不错的,现在分享给大家,也给大家...

    移动端 html5 长按

    HTML常用meta大全(推荐)

    这篇文章主要介绍了HTML常用meta大全(推荐),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参...

    cdr怎么把图片转换成位图? cdr图片转换为位图的教程

    cdr怎么把图片转换成位图? cdr图片转换为

    cdr怎么把图片转换成位图?cdr中插入的图片想要转换成位图,该怎么转换呢?下面我们就来看看cdr图片转换为位图的...

    cdr 图片 位图

    win10系统怎么录屏?win10系统自带录屏详细教程

    win10系统怎么录屏?win10系统自带录屏详细

    当我们是使用win10系统的时候,想要录制电脑上的画面,这时候有人会想到下个第三方软件,其实可以用电脑上的自带...

    win10 系统自带录屏 详细教程

    + 更多教程 +
    WIN服务器linux服务器FTP服务器DNS服务器其他