主节点的设定
[root@centos7 /]# cat /etc/my.cnf.d/server.cnf
#
# These groups are read by MariaDB server.
# Use it for options that only the server (but not clients) should see
#
# See the examples of server my.cnf files in /usr/share/mysql/
#
# this is read by the standalone daemon and embedded servers
[server]
# this is only for the mysqld standalone daemon
[mysqld]
server-id = 1 #设定serverID的值
log-bin = mysql.log #启用二进制日志并以文件
skip_name_resolve = NO #关闭名称解析功能
# this is only for embedded server
[embedded]
# This group is only read by MariaDB-5.5 servers.
# If you use the same .cnf file for MariaDB of different versions,
# use this group for options that older servers don't understand
[mysqld-5.5]
# These two groups are only read by MariaDB servers, not by MySQL.
# If you use the same .cnf file for MySQL and MariaDB,
# you can put MariaDB-only options here
[mariadb]
[mariadb-5.5]
启动主节点
[root@centos7 /]# systemctl restart mariadb
备节点的设置
[root@localhost ~]# cat /etc/my.cnf.d/server.cnf
#
# These groups are read by MariaDB server.
# Use it for options that only the server (but not clients) should see
#
# See the examples of server my.cnf files in /usr/share/mysql/
#
# this is read by the standalone daemon and embedded servers
[server]
server-id=2 #定义服务器ID
relay-log=chenxi.log #启用中继日志
read_only=ON #关闭名称解析功能
# this is only for the mysqld standalone daemon
[mysqld]
# this is only for embedded server
[embedded]
# This group is only read by MariaDB-5.5 servers.
# If you use the same .cnf file for MariaDB of different versions,
# use this group for options that older servers don't understand
[mysqld-5.5]
# These two groups are only read by MariaDB servers, not by MySQL.
# If you use the same .cnf file for MySQL and MariaDB,
# you can put MariaDB-only options here
[mariadb]
[mariadb-5.5]
启用备节点
[root@localhost ~]# systemctl restart mariadb
启用主节点后连入
[root@centos7 /]# mysql
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 2
Server 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)]> show master status; 查看二进制日志
+--------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+--------------+----------+--------------+------------------+
| mysql.000002 | 245 | | |
+--------------+----------+--------------+------------------+
1 row in set (0.00 sec)
MariaDB [(none)]> GRANT REPLICATION CLIENT,REPLICATION SLAVE ON *.* TO 'repluser'@'172.16.%.%' IDENTIFIED BY 'replpass'; 创建用户
MariaDB [(none)]> flush privileges; 刷新授权表
Query OK, 0 rows affected (0.01 sec)
MariaDB [(none)]> show master status; 查看二进制日志
+--------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+--------------+----------+--------------+------------------+
| mysql.000002 | 496 | | |
+--------------+----------+--------------+------------------+
1 row in set (0.00 sec)
MariaDB [(none)]> show binlog events in 'mysql.000002'; 查看
+--------------+-----+-------------+-----------+-------------+-------------------------------------------------------------------------------------------------------+
| Log_name | Pos | Event_type | Server_id | End_log_pos | Info |
+--------------+-----+-------------+-----------+-------------+-------------------------------------------------------------------------------------------------------+
| mysql.000002 | 4 | Format_desc | 1 | 245 | Server ver: 5.5.52-MariaDB, Binlog ver: 4 |
| mysql.000002 | 245 | Query | 1 | 421 | GRANT REPLICATION CLIENT,REPLICATION SLAVE ON *.* TO 'repluser'@'172.16.0.%' IDENTIFIED BY 'replpass' |
| mysql.000002 | 421 | Query | 1 | 496 | flush privileges |
+--------------+-----+-------------+-----------+-------------+-------------------------------------------------------------------------------------------------------+
3 rows in set (0.00 sec)
主节点准备好了
备节点准备
[root@localhost ~]# mysql 连接服务器
\Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 2
Server version: 5.5.56-MariaDB MariaDB Server
Copyright (c) 2000, 2017, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> change master to master_host='172.16.251.203' ,master_user='repluser',master_password='replpass',master_log_file='mysql.000002',master_log_pos=421;
Query OK, 0 rows affected (0.09 sec)
定义完成后数据文件目录会成几个文件
[root@localhost ~]# ls /var/lib/mysql/
aria_log.00000001 chenxi.000001 ibdata1 ib_logfile1 mysql performance_schema test
aria_log_control chenxi.index ib_logfile0 master.info 被节点 mysql.sock relay-log.info备节点
MariaDB [(none)]> show slave status\G 查看从服务器的状态
*************************** 1. row ***************************
Slave_IO_State:
Master_Host: 172.16.251.203
Master_User: repluser
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql.000002
Read_Master_Log_Pos: 421
Relay_Log_File: chenxi.000001
Relay_Log_Pos: 4
Relay_Master_Log_File: mysql.000002
Slave_IO_Running: No
Slave_SQL_Running: No
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: 421
Relay_Log_Space: 245
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: NULL
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: 0
1 row in set (0.00 sec)
MariaDB [(none)]> start slave; 打开IO线程与sql线程sql负责重放事件IO线程负责把从主节点读取事件
Query OK, 0 rows affected (0.09 sec)
MariaDB [(none)]> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 172.16.251.203
Master_User: repluser
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql.000003
Read_Master_Log_Pos: 421
Relay_Log_File: chenxi.000005
Relay_Log_Pos: 701
Relay_Master_Log_File: mysql.000003
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: 421
Relay_Log_Space: 1341
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: 1
1 row in set (0.00 sec)
备节点配置成功
主节点创建库测试
MariaDB [(none)]> create database mysdb;
Query OK, 1 row affected (0.04 sec)
MariaDB [(none)]> show master status; 检查日志的信息
+--------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+--------------+----------+--------------+------------------+
| mysql.000003 | 506 | | |
+--------------+----------+--------------+------------------+
1 row in set (0.00 sec)
从节点查看信息
MariaDB [(none)]> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 172.16.251.203
Master_User: repluser
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql.000003
Read_Master_Log_Pos: 506 变化
Relay_Log_File: chenxi.000005
Relay_Log_Pos: 786
Relay_Master_Log_File: mysql.000003
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: 506 从节点的变化
Relay_Log_Space: 1426
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: 1
1 row in set (0.00 sec)
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysdb | 从节点复制成功
| mysql |
| performance_schema |
| test |
+--------------------+
5 rows in set (0.08 sec)
网友评论