1.环境准备:
系统环境:CentOS Linux release 7.7.1908 (Core)
MySQL环境:
172.16.6.140:
Ver 5.0.96-community-log for unknown-linux-gnu on x86_64
172.16.6.142:
Ver 14.12 Distrib 5.0.77, for unknown-linux-gnu (x86_64) using EditLine wrapper
2.不同版本数据库数据兼容
在服务器172.16.6.142上操作以下
进入mysql数据库目录
cd /var/lib/mysql/
Ls查看文件
ibdata1 ib_logfile1 mysql mysql-bin.000002 mysql-bin.000004 mysql-bin.000006 mysql-bin.000008 test
ib_logfile0 localhost.localdomain.err ysql-bin.000001 mysql-bin.000003 mysql-bin.000005 mysql-bin.000007 mysql-bin.index
Service mysqld stop关掉MySQL服务 删除所有数据库目录文件
在服务器172.16.6.140上操作以下
Cd进入/var/lib/mysql/
停服务把所有数据文件打包拷贝给172.16.6.142
tar -zcf data.tar.gz *
scp data.tar.gz root@172.16.6.142:/var/lib/mysql/
登录172.16.6.142解包并起服务
tar -xf data.tar.gz
service mysqld start显示 Starting MySQL. SUCCESS! 表示成功
查看以下3306端口是否开启
netstat -untpl
3.这时说明数据库版本兼容接下来配置数据同步
先配置master服务器172.16.6.140
打开配置文件并修改
Vim /etc/my.cnf
server_id=140 #serverID不可重复
log_bin=master140 #日志名
binlog-ignore-db=mysql #二进制日志文件中忽略的数据库 (主从库数据不一致时可以备份,也可以用此项忽略)
binlog-do-db=XXXX #二进制日志文件中记录的数据库
刷新服务
Service mysqld restart
登录数据库授权用户
mysql -uroot -p密码
Grant replication slave on *.* to repluser@”%” identified by “123qqq...A”
查看binlog日志信息
Show master status\G;
*************************** 1. row ***************************
File: master140.000003 #日志名
Position: 98 #偏移量
Binlog_Do_DB:
Binlog_Ignore_DB: mysql
1 row in set (0.00 sec)
再配置slave服务器172.16.6.142
Vim /etc/my.cnf
Server_id=142 #serverID
修改完重启服务
Service mysqld restart
登录数据库指定master服务器信息
Mysql -uroot -p密码
change master to
master_host= "172.16.6.140" ,#主服务器ID
master_user= "repluser" , #主服务器授权的用户
master_password= "123qqq...A" ,#主服务器授权用户的密码
master_log_file= "master140.000003" ,#主服务器的日志文件
master_log_pos=98;#主服务器的偏移量
(以上是一条命令,逗号链接回车出现Query OK, 0 rows affected (0.00 sec)表示成功 )
开启slave进程
start slave;
Query OK, 0 rows affected (0.00 sec)
查看进程状态
show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 172.16.6.140
Master_User: repluser
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: master140.000003
Read_Master_Log_Pos: 98
Relay_Log_File: mysqld-relay-bin.000002
Relay_Log_Pos: 235
Relay_Master_Log_File: master140.000003
Slave_IO_Running: Yes#确保IO线程运行
Slave_SQL_Running: Yes#确保SQL线程运行
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: 98
Relay_Log_Space: 235
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
1 row in set (0.00 sec)
4.到master服务器建库测试
mysql -uroot -p 密码
create database xxoo character set utf8;
登录slave服务器数据库查看
show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| test |
| xxoo |
+--------------------+