1. 实验环境
- 系统环境:CentOS7.3
- 两个节点:
- 主节点host3_13 IP地址:192.168.10.13
- 从节点host4_14 IP地址:192.168.10.14
- 同步时间:工具ntpdate
2. 搭建环境:
- 安装软件
两个节点做同样的工作,这里只放一个节点的图
- 同步时间
作为主从复制,时间同步是最基本的设置!最基本的满足条件!
3. 配置主节点
主节点做三件事:
(1)对主服务器节点设置一个server-id
(2)启用二进制日志
(3)创建一个拥有复制权限的用户账号
修改 Mariadb-server 配置文件 /etc/my.cnf.d/server.cnf :
创建一个拥有复制权限的用户账号 :
[root@host3 mysql]#mysql
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 3
ongoingServer version: 5.5.52-MariaDB MariaDB Server
Copyright (c) 2000, 2016, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
###### 创建一个拥有复制权限的用户账号 ######
MariaDB [(none)]> grant replication client,replication slave on *.* to 'repluser'@'19'192.168.10.%' identified by 'replpass';
Query OK, 0 rows affected (0.01 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 |
+-------------------+----------+--------------+------------------+
| master-log.000003 | 498 | | |
+-------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
MariaDB [(none)]> show binlog events in 'master-log.000003';
+-------------------+-----+-------------+-----------+-------------+---------------------------------------------------------------------------------------------------------+
| Log_name | Pos | Event_type | Server_id | End_log_pos | Info |
+-------------------+-----+-------------+-----------+-------------+---------------------------------------------------------------------------------------------------------+
| master-log.000003 | 4 | Format_desc | 1 | 245 | Server ver: 5.5.52-MariaDB, Binlog ver: 4 |
| master-log.000003 | 245 | Query | 1 | 423 | grant replication client,replication slave on *.* to 'repluser'@'192.168.10.%' identified by 'replpass' |
| master-log.000003 | 423 | Query | 1 | 498 | flush privileges |
+-------------------+-----+-------------+-----------+-------------+---------------------------------------------------------------------------------------------------------+
3 rows in set (0.00 sec)
MariaDB [(none)]>
4. 配置从节点
修改从服务器节点的配置文件:
使用主服务器授权的账号连接至主服务器:
- 特别注意:
把一个节点变成从节点的要点在于,我们可以在从节点上直接用MySQL命令''CHANGE MASTER TO''来指定自己的主服务器节点;
指定主服务器节点:
启动从服务器:
对比从服务器状态前后差别
-
启动之前状态
-
start slave启动从服务器节点
5. 测试
我们这是实现MySQL主从复制,所测试也简单,我们在主节点上跑几个SQL指令,在回过头查看从节点就可以了
主节点:
从节点:
整体对比图:
结语:
对于运维工程师来说有很多工作需要我们去做,这里有些个技能是我们必须具备的;其中数据备份、数据恢复和MySQL主从复制等等更是格外重要!还是那句话,我们运维就是什么都懂的神仙...