centos7 maiadb主从复制搭建
admin
2023-05-26 12:01:29
0次

需求:

由于要使用saltstack,部署环境,先手动搭建了下主从环境,发现原来的word资料都找不到了,所以这次赶紧的记录到博客当中!!

环境:

两台设备,ip地址分别为:

node1:192.168.56.11

node2:192.168.56.12

系统:Centos7

软件包:使用系统自带的yum 来安装的mariadb

==========================================================================================

安装:

分别在两台设备上安装数据库mariadb,使用yum来安装的,可以更换成过内的yum源来操作

命令:yum install -y mariadb mariadb-server

配置:

先配置node1上的master配置

[root@salt-node1 ~]# vim /etc/my.cnf

[mysqld]
innodb_file_per_table=NO
log-bin=/var/lib/mysql/master-bin #这里如果不指定路径默认是在datadir下面生成
binlog_format=mixed
server-id = 11#这个主节点一定要是唯一的
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
# 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

重启node1上的mysql服务

systemctl restart mariadb.service

登录mysql:

mysql -uroot -p
我这里测试就没有设置密码

创建帐号并赋予replication的权限。从库,主从库复制数据时需要使用这个帐号进行

grant replication slave on *.* to 'root'@'192.168.56.%' identified by '123456';

加锁:

实际工作中,需要禁止数据库的写入,要给数据库上锁

FLUSH TABLES WITH READ LOCK;

记录主库的binlog日志文件和位置信息(这个信息,要在从库配置的时候用到)

MariaDB [(none)]> show master status;
+-------------------+----------+--------------+------------------+
| File              | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+-------------------+----------+--------------+------------------+
| master-bin.000001 |      398 |              |                  |
+-------------------+----------+--------------+------------------+
1 row in set (0.00 sec)

备份主库的数据:

mysqldump -uroot -p --all-databases > /tmp/db.sql

==========================================================================================

下面开始配置从库:

导入数据到从库当中

mysql -uroot -p 

修改配置文件my.cnf:

[root@salt-node2 ~]# cat /etc/my.cnf
[mysqld]
innodb_file_per_table=NO
#log-bin=mysql-bin
binlog_format=mixed
server-id = 12
relay-log = /var/lib/mysql/relay-bin
#log_slave_updates = 1
read_only = on
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
# 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

重启服务:

systemctl restart mariadb.service

登录mysql

mysql -uroot -p

设置主从服务配置

CHANGE MASTER TO MASTER_HOST='192.168.56.11',MASTER_USER='root', MASTER_PASSWORD='123456', MASTER_LOG_FILE='master-bin.000001', MASTER_LOG_POS= 398;

开启主从服务:

start slave;

查看从库状态:

MariaDB [(none)]> show slave status\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.56.11
                  Master_User: root
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: master-bin.000001
          Read_Master_Log_Pos: 485
               Relay_Log_File: relay-bin.000002
                Relay_Log_Pos: 617
        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: 485
              Relay_Log_Space: 905
              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: 11
1 row in set (0.00 sec)

ERROR: No query specified
             Slave_IO_Running: Yes #这里必须为yes,如果不是,需要排查故障
            Slave_SQL_Running: Yes #这里必须为yes,如果不是,需要排查故障

主从验证,

#主库创建数据库

MariaDB [(none)]> create database jiayou ;
Query OK, 1 row affected (0.00 sec)
#从库查看是否同步过来这个数据库
MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| gg                 |
| jiayou             |
| kk                 |
| mysql              |
| performance_schema |
| salt               |
| test               |
+--------------------+
8 rows in set (0.00 sec)

有时候会有一些故障问题,可以参考下面这个博客,自己懒的写了

http://blog.csdn.net/mingliangniwo/article/details/54606894?locationNum=8&fps=1

相关内容

热门资讯

空调连续工作多久需要停机休息 空调连续工作的时间取决于多个因素,包括空调的型号、功率、使用环境和维护情况等。一般来说,空调可以连续...
空调哪个符号是除湿的 空调除湿功能通常用一个水滴形状的符号表示。不同品牌和型号的空调遥控器可能会有所差异,但一般来说,除湿...
格力空调多长时间加一次氟利昂 1、一般情况下,空调对氟利昂的使用量是非常小的,所以在空调使用了四年左右的时候再考虑加也没问题。2、...
空调用多长时间需要加氟一次 一般情况下是五年。当出现以下这几种情况时,空调才需要加氟。1、空调的室内机和室外机各有两个接口,当接...
格调生活·C标思享荟:卡萨帝在... 9月12日的郑州龙湖金融岛,夜风轻拂,灯光璀璨。“C标时刻共传奇——卡萨帝思享荟暨20周年品牌盛典郑...
品牌价值破千亿、高端份额持续领... 9月12日,“C标时刻共传奇——卡萨帝思享荟暨20周年品牌盛典郑州站”在郑州龙湖金融岛举办。2026...
空调主板坏了修800还是换新的 空调主板坏了是修还是换新,需要考虑多个因素,以下是一些建议:1. 维修成本:如果维修费用较低,比如 ...
空调开多久需要休息多长时间 问题:空调开多久需要休息多长时间回答:1、在使用空调的过程中,一般空调开7~8个小时左右需要休息2~...
中央空调多长时间清洗一次 1、一般空调开机前清洗一次即可,空调开机中间时段再清洗一次,空调关机时清洗一次,这样比较合理。2、空...
中央空调氟利昂多久加一次 看空调制热和制冷程度,很多人的观点认为氟利昂必须一年放一次,其实这个观点是错误的,理解上面有点偏差,...