部署MySQL-主从异步复制

实验环境  三台虚拟机 一主两从

主:     192.168.200.111

从1:  192.168.200.112

从2:  192.168.200.113

[root@localhost ~]# iptables -F
[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# setenforce 0

建立三台服务器时间同步环境,在主服务器上安装配置NTP时间同步服务器

[root@localhost ~]# yum -y install ntp

[root@localhost ~]# vim /etc/ntp.conf

末行添加

server 127.127.1.0

fudge 127.127.1.0 startum 8

[root@localhost ~]# systemctl restart ntpd

在两台从服务器上同步时间

[root@localhost ~]# yum -y install ntpdate

[root@localhost ~]# ntpdate 192.168.200.111
15 Oct 12:02:12 ntpdate[10406]: adjust time server 192.168.200.111 offset 0.008413 sec

slave2服务器与slave1服务器相同

在所有的服务器上操作安装mariadb和mariadb-server

[root@localhost ~]# yum -y install mariadb mariadb-server

配置MysqL Master主服务器

[root@localhost MysqL]# vim /etc/my.cnf

[MysqLd]

server-id=1                      //三台不要相同
log-bin=MysqL-bin
log-slave-updates=true   //手动添加,开启从日志

[root@localhost ~]# systemctl restart mariadb

给从服务器授权

[root@localhost ~]# MysqL -uroot -p123456

MariaDB [(none)]> grant replication slave on *.* to 'root'@'192.168.200.%' identified by '123456';
Query OK, 0 rows affected (0.00 sec)

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

MariaDB [(none)]> show master status;
+------------------+----------+--------------+------------------+
| File | Position | binlog_Do_DB | binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| MysqL-bin.000010 | 474 |                 |                        |                     //数据后面要用到
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)

备份之前主服务器有数据需要先传给从服务器,没有就忽略。

主:MysqLdump -uroot -all-databases > /root/alldbacup.sql

scp传给从服务器

从:MysqL -uroot -p <  /root/alldbacup.sql

配置从服务器

[root@localhost ~]# vim /etc/my.cnf

[MysqLd]

server-id=2                                               //id号不能相同
relay-log=relay-log-bin                              //开启中继日志
relay-log-index=slave-relay-bin.index 

[root@localhost ~]# systemctl restart mariadb

[root@localhost ~]# MysqL -uroot -p123456

MariaDB [(none)]> stop slave;                       //把自己从的角色功能先停掉
Query OK, 0 rows affected, 1 warning (0.01 sec)

MariaDB [(none)]> change master to -> master_host='192.168.200.111',-> master_user='root',-> master_password='123456',-> master_log_file='MysqL-bin.000010',-> master_log_pos=474;

Query OK, 0 rows affected (0.01 sec)

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

MariaDB [(none)]> show slave status \G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.200.111
Master_User: root
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: MysqL-bin.000010
Read_Master_Log_Pos: 474
Relay_Log_File: relay-log-bin.000002
Relay_Log_Pos: 529
Relay_Master_Log_File: MysqL-bin.000010
Slave_IO_Running: Yes
Slave_sql_Running: Yes

........................................

通过查看slave状态,确保Slave_IO_Running: Yes      Slave_sql_Running: Yes

slave2配置相同

测试:

在主服务器创建数据库,在从服务器查看是否同步

主:

MariaDB [(none)]> create database db_test;
Query OK, 1 row affected (0.00 sec)

MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema   |
| db_test        |
| MysqL        |
| performance_schema |
| test |
+--------------------+
5 rows in set (0.00 sec)

从:

MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema   |
| db_test        |
| MysqL        |
| performance_schema |
| test |
+--------------------+
5 rows in set (0.00 sec)

相关文章

安装开始...1.打开“https://dev.mysql.com/downloadsepo/yu...
安装Helm3#官网下载慢#wgethttps://get.helm.sh/helm-v3.5.4...
通过Linux命令行启动用的指令:systemctlstartmariadb.servi...
InstallingMariaDBServer10.4TodeployMariaDBCommunityServe...
阅读目录一什么是存储引擎二mysql支持的存储引擎三使用存储引...
1、安装MariaDB安装命令yum-yinstallmariadbmariadb-server安...