以下所有操作都是在Red-hat 7.2上
实验环境:
mysql-master 172.25.77.5(主服务器)
mysql-slave 172.25.77.6(从服务器)
1.数据库的安装和配置
mysql-master和mysql-slave都安装mariadb
开启数据库并执行数据库初始化
yum install mariadb -y
2.配置主从数据库配置文件
3.在master和slave上进行授权等操作
3.1 在master上创建复制用户并授权
MariaDB [(none)]> create user slave; #创建用户
MariaDB [(none)]>grant all privileges on *.* to 'slave'@'172.25.77.6' identified by 'redhat'; #全部权限给,并且给定密码为redhat
3.2 在salve上配置mysql
MariaDB [(none)]>CHANGE MASTER TO MASTER_HOST='172.25.77.5',MASTER_USER='salve',MASTER_PASSWORD='redhat',MASTER_PORT=3306;
置了这些数据后,再开启slave的复制,mysql主从复制就可以正常进行了。
开启salve
MariaDB [(none)]> start slave;
4.检查状态并测试
检查master的状态:
MariaDB [(none)]> show master status\G
*************************** 1. row ***************************
File: mysql-bin.000001
Position: 245
Binlog_Do_DB:
Binlog_Ignore_DB:
1 row in set (0.00 sec)
第一行:是正在写入的日志,目前是master-bin.000001
检查slave的状态:
MariaDB [(none)]> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 172.25.77.5
Master_User: slave
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 551
Relay_Log_File: mariadb-relay-bin.000002
Relay_Log_Pos: 835
Relay_Master_Log_File: mysql-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: 551
Relay_Log_Space: 1131
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
ps:在这里主要检查的参数就是(只要显示yes就是配置成功)
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
在mysql里面测试一下,现在master上创建一个库,观察slave是否会复制