Mysql---主从复制
mysql复制 1.搭建一个master对应1个slave(只复制某个数据库) (1)规划网络和主从服务器 master:10.10.54.86 slave:10.10.54.85 (2)主机设置 log-bin=master-bin binlog_format=mixed server-id=1 [root@nan86 tmp]# /etc/init.d/mysqld restart ERROR! MySQL server PID file could not be found! Starting MySQL.. SUCCESS! (3)从机设置 log-bin=slave-bin binlog_format=mixed server-id=10 [root@nan85 ~]# /etc/init.d/mysqld restart ERROR! MySQL server PID file could not be found! Starting MySQL.. SUCCESS! (4)在master上面创建一个复制用户并授予权限 mysql> grant replication slave on *.* to ‘emp‘@‘10.10.54.85‘ identified by ‘123‘; Query OK, 0 rows affected (0.00 sec) mysql> flush privileges; Query OK, 0 rows affected (0.00 sec) (5)查看master上的二进制日志和position位置 mysql> show master status; +-------------------+----------+--------------+------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | +-------------------+----------+--------------+------------------+ | master-bin.000006 | 330 | | | +-------------------+----------+--------------+------------------+ 1 row in set (0.00 sec) (6)备份master上的数据,把备份master数据库还原到从库上 [root@nan86 tmp]# mysqldump -uroot -p123456 --master-data=2 --single-transaction --flush-logs --database employees>employees.sql [root@nan86 tmp]# mysql -uroot -p123456 -h10.10.54.85<employees.sql 备注:-p 后接的密码为从机授权的密码 (7)在slave上面change master操作 mysql> change master to master_host=‘10.10.54.86‘,master_user=‘emp‘, master_password=‘123‘, master_log_file=‘master-bin.000006‘,master_log_pos=330; Query OK, 0 rows affected (0.01 sec) (8)在slave上启动slave mysql> start slave; Query OK, 0 rows affected (0.00 sec) (9)查看slave的状态 mysql> show slave status\G; *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 10.10.54.86 Master_User: emp Master_Port: 3306 Connect_Retry: 60 Master_Log_File: master-bin.000007 Read_Master_Log_Pos: 107 Relay_Log_File: nan85-relay-bin.000004 Relay_Log_Pos: 254 Relay_Master_Log_File: master-bin.000007 Slave_IO_Running: Yes Slave_SQL_Running: Yes (10)测试 主上: mysql> create database b; Query OK, 1 row affected (0.02 sec) 从上:显示有数据库b mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | a | | b | | employees | | mysql | | performance_schema | | test | +--------------------+ 测试在master上创建删除添加数据,在slave上都能同步。
本文出自 “笔记” 博客,请务必保留此出处http://sunflower2.blog.51cto.com/8837503/1651704
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。