环境搭建
- 需要两台虚拟机
- 在一个网段
[root@localhost ~]# virsh list --all
Id Name State
----------------------------------------------------
8 master running
9 slave running
[root@localhost ~]#
我这里有两台虚拟机
一台master
一台slave
配置主服务器
编辑配置文件
[root@master ~]# vim /etc/my.cnf.d/server.cnf
或者
[root@master ~]# vim /etc/my.cnf
编辑/etc/my.cnf.d/server.cnf 配置文件
[mysqld]
server-id = 1 #服务器的ID 和要比从的小
log-bin = mysqlbinlog #日志文件名称
skip_name_resolve = ON #禁用DNS解析 ON打开
/etc/my.cnf和子配置相同,两者只需改一个
启动mysql服务
[root@master ~]# systemctl restart mariadb
[root@master ~]# systemctl enable mariadb
[root@master ~]#
配置从服务器
编辑子配置文件
/etc/my.cnf.d/server.cnf
[mysqld]
server-id = 2
skip_name_resolve = ON
重启服务并设置为开机启动
[root@slave ~]# systemctl restart mariadb
[root@slave ~]# systemctl enable mariadb
[root@slave ~]#
配置主从数据库
主节点
- 在主服务器上创建用户,用于从服务器进行登录
授予slave@'192.168.43.20' 用户所有权限MariaDB [(none)]> grant all on *.* to slave@'192.168.43.20' identified by '123';
- 刷新授权表
MariaDB [(none)]> flush privileges;
- 查看主服务器状态
MariaDB [(none)]> show master status; +--------------------+----------+--------------+------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | +--------------------+----------+--------------+------------------+ | mysqlbinlog.000001 | 456 | | | +--------------------+----------+--------------+------------------+ 1 row in set (0.00 sec) MariaDB [(none)]>
- 配置从节点
连接主服务器,这里的MASTER_LOG_FILE和MASTER_LOG_POS要与刚才在主服务端所查看状态显示的数值相对性MariaDB [(none)]> change master to MASTER_HOST='192.168.43.20', MASTER_USER='slave',MASTER_PASSWORD='123',MASTER_LOG_FILE='mysqlbinlog.000001',MASTER_LOG_POS=456; Query OK, 0 rows affected (0.01 sec) MariaDB [(none)]>
- 查看从服务器的状态
注意查看加#的选项MariaDB [(none)]> show slave status\G; *************************** 1. row *************************** Slave_IO_State: Master_Host: 192.168.43.20 Master_User: slave Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysqlbinlog.000001 Read_Master_Log_Pos: 456 Relay_Log_File: mariadb-relay-bin.000001 Relay_Log_Pos: 4 Relay_Master_Log_File: mysqlbinlog.000001 # 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: 456 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) ERROR: No query specified MariaDB [(none)]>
启动主从同步
MariaDB [(none)]> start slave;
Query OK, 0 rows affected (0.06 sec)
MariaDB [(none)]>
查看从服务器的状态
MariaDB [(none)]> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.43.10
Master_User: slave
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysqlbinlog.000004
Read_Master_Log_Pos: 245
Relay_Log_File: mariadb-relay-bin.000002
Relay_Log_Pos: 531
Relay_Master_Log_File: mysqlbinlog.000004
# 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: 245
Relay_Log_Space: 827
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)
ERROR: No query specified
MariaDB [(none)]>
当从服务器出现以下状态需要查看主服务器防火墙的状态
MariaDB [(none)]> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Connecting to master
Master_Host: 192.168.43.10
Master_User: slave
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysqlbinlog.000004
Read_Master_Log_Pos: 245
Relay_Log_File: mariadb-relay-bin.000002
Relay_Log_Pos: 531
Relay_Master_Log_File: mysqlbinlog.000004
Slave_IO_Running: Connecting
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: 245
Relay_Log_Space: 827
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: 2003
# Last_IO_Error: error connecting to master 'slave@192.168.43.10:3306' - retry-time: 60 retries: 86400 message: Can't connect to MySQL server on '192.168.43.10' (113)
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 1
1 row in set (0.00 sec)
ERROR: No query specified
MariaDB [(none)]>
注意查看#号所显示的错误
- 查看主服务器防火墙状态
[root@master ~]# systemctl status firewalld
● firewalld.service - firewalld - dynamic firewall daemon
Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled)
Active: active (running) since Thu 2019-04-25 11:53:11 GMT; 6min ago
Docs: man:firewalld(1)
Main PID: 12795 (firewalld)
CGroup: /system.slice/firewalld.service
└─12795 /usr/bin/python -Es /usr/sbin/firewalld --nofork --nopid
Apr 25 11:53:11 master systemd[1]: Starting firewalld - dynamic firewall daemon...
Apr 25 11:53:11 master systemd[1]: Started firewalld - dynamic firewall daemon.
[root@master ~]#
将防火墙关闭重新连接即可