当前位置: 移动技术网 > IT编程>数据库>Mysql > linux下mysql主从复制,实现数据库同步

linux下mysql主从复制,实现数据库同步

2019年01月17日  | 移动技术网IT编程  | 我要评论

运行环境:

查看linux版本命令:lsb_release -a

主服务器:centos release 6.5 mysql 5.6.10-log  ip:172.17.200.25
从服务器:centos release 6.5 mysql 5.6.10-log  ip:172.17.200.26
主服务器dashi数据库  

mysql默认配置文件,如不特殊指定默认为/etc/my.cnf

mysql配置文件查找顺序:/etc/my.cnf /etc/mysql/my.cnf ~/.my.cnf

我是默认设置 /etc/my.cnf

进入主服务 msyql -uroot -p 


 

一:主服务

1.1、创建一个复制用户dashi,具有replication slave 权限。

mysql>grant replication slave on *.* to 'dashi'@'172.17.200.26' identified by 'dashi';

mysql>flush privileges;

1.2、编辑my.cnf文件

vim /etc/my.cnf

增加 server-id=107

log-bin=bin.log 文件路径自己定(推荐绝对路径)

1.3 重启mysql

service mysqld restart

1.4、设置读锁

mysql>flush tables with read lock;

1.5、得到binlog日志文件名和偏移量(此处记住file名称和position值,后面slave服务器配置时需要用到)

mysql> show master status;
+------------+------------+--------------+--------------------------------------------------+-------------------+
| file | position | binlog_do_db | binlog_ignore_db | executed_gtid_set |
+------------+------------+--------------+--------------------------------------------------+-------------------+
| bin.000031 | 1011406487 | | information_schema,mysql,performance_schema,test | |
+------------+------------+--------------+--------------------------------------------------+-------------------+
1 row in set (0.00 sec)

1.6、备份要同步的数据库

mysqldump -uroot -p  test>test.sql

1.7解锁

mysql>unlock tables;


 

二:从服务器(172.17.200.26)

2.1、编辑my.cnf文件

vim /etc/my.cnf

增加 server-id=2

2.2 重启mysql

service mysqld restart

2.3、对从数据库进行相应设置

  此处要注意logfile的名称和position的值,其余host、user和password为主数据库设置的账号和密码

mysql> stop slave;
query ok, 0 rows affected (0.00 sec)

mysql> change master to 
   -> master_host='172.17.200.25',
   -> master_user='dashi',
   -> master_password='dashi',
   -> master_log_file='bin.log.000001',
   -> master_log_pos=713;

mysql> start slave;
query ok, 0 rows affected (0.00 sec)

mysql> show slave status\g; 

在这里主要是看:

  slave_io_running=yes
  slave_sql_running=yes


三、测试:
  上述项配置完以后可查看master和slave上线程的状态。在master上,你可以看到slave的i/o线程创建的连接:在master上输入show processlist\g;

mysql> show processlist \g;
*************************** 1. row ***************************
id: 10865
user: dashi
host: 172.17.200.25:37369
db: dashi
command: sleep
time: 7
state:
info: null
*************************** 2. row ***************************
id: 10866
user: dashi
host: 172.17.200.25:37370
db: dashi
command: sleep
time: 7
state:
info: null
*************************** 3. row ***************************
id: 10873
user: dashi
host: 172.17.200.26:37928
db: dashi
command: execute
time: 1
state: sending data
info: select count(1) from trade_click where link=?
*************************** 4. row ***************************
id: 10874
user: dashi
host: 172.17.200.26:37929
db: dashi
command: sleep
time: 9
state:
info: null
*************************** 5. row ***************************
id: 10882
user: dashi
host: 172.17.200.26:37962
db: dashi
command: sleep
time: 78
state:
info: null

 

error:
no query specified

3.1、在主数据库:192.168.0.107上添加新数据

3.2、在从库查找记录,是否存在

 

四:总结

      主服务器master记录数据库操作日志到binary log,从服务器开启i/o线程将二进制日志记录的操作同步到relay log(存在从服务器的缓存中),另外sql线程将relay log日志记录的操作在从服务器执行,从而达到主从复制。

 

 

 

 

 

 

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网