MySQL数据库主从同步部署
MySQL数据库主从同步部署:
环境
Mater: CentOS6.4 mysql5.5.38 192.168.107.133
Slave: CentOS6.4 mysql5.5.38 192.168.107.135
1.导出主服务数据,将主备初始数据同步
master:
//从master上导出需要同步的数据库信息
mysqldump -u*** -p*** --database test > test.sql
//将master上的备份信息传输到slave上
scp /root/test.sql :/opt/
slave:
//进入slave的数据库
mysql -u*** -p***
//清空test数据库
drop database test
//导入master的test数据库信息
source /opt/test.sql
2.配置master和slave上的mysql数据库
master:
//修改master的my.cnf文件
vim /etc/my.cnf
//master配置如下,在[mysqld]下添加如下配置
#log-bin
server-id = 1
log_bin = master-bin
expire_logs_days = 10
max_binlog_size = 100M
binlog-do_db = test
binlog_ignore_db = mysql
//重启mysql数据库
service mysqld restart
slave:
//修改slave的my.cnf文件
vim /etc/my.cnf
//slave配置如下,在[mysqld]下添加如下配置
server-id = 2
//重启mysql数据库
service mysqld restart
简单说明一下参数配置,保证主备server-id唯一。在master上需要开启mysql的binlog,log_bin=master_bin,指定binlog文件的名称。
3.创建一个复制用户,具有replication slave 权限,能保证slave能把master的数据同步过去
master:
grant replication slave on *.* to ‘replication‘@‘192.168.107.135‘ identified by ‘replication‘;
4.获取master的binlog位置
master:
//进入mysql数据库
mysql -u*** -p***
//设置读锁
flush tables with read lock;
//获取mysql的binlog文件信息和偏移量
show master status;
+-------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+-------------------+----------+--------------+------------------+
| master-bin.000010 | 3713 | test | mysql |
+-------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
//解锁
unlock tables;
5.设置备端数据库
//进入mysql数据库
mysql -u*** -p***
//停止slave
stop slave;
//设置对应master的binlog信息
MariaDB [(none)]> change master to
-> master_host=‘192.168.107.133‘,
-> master_user=‘replication‘,
-> master_password=‘replication‘,
-> master_log_file=‘master-bin.000010‘,
-> master_log_pos=3713;
//启动slave
start slave;
6.查看备端状态
MariaDB [(none)]> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.107.133
Master_User: replication
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: master-bin.000010
Read_Master_Log_Pos: 3881
Relay_Log_File: mariadb-relay-bin.000002
Relay_Log_Pos: 698
Relay_Master_Log_File: master-bin.000010
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: 3881
Relay_Log_Space: 994
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
如果:Slave_IO_Running: Yes,Slave_SQL_Running: Yes则为配置成功,配置错误重复上面操作即可。如果解决不了可通过查看mysql日志分析处理。
7.测试。配置成功之后直接连到主从数据库,在master上改变表、字段、数据,slave会同步变化
相关推荐
emmm00 2020-11-17
暗夜之城 2020-11-11
疯狂老司机 2020-09-08
CoderToy 2020-11-16
bianruifeng 2020-11-16
云中舞步 2020-11-12
敏敏张 2020-11-11
好记忆也需烂 2020-11-11
Coder技术文摘 2020-09-29
huacuilaifa 2020-10-29
Gexrior 2020-10-22
lpfvip00 2020-10-07
云中舞步 2020-09-11
康慧欣 2020-09-10
世樹 2020-11-11
tufeiax 2020-09-03
王艺强 2020-11-17
ribavnu 2020-11-16