####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)
------
问题备注:
CHANGE MASTER TO MASTER_HOST='10.10.54.85',MASTER_PORT=3306,MASTER_USER='slave2',MASTER_PASSWORD='slave2',MASTER_LOG_FILE='master-bin.000009',MASTER_LOG_POS=120;
----
(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上都能同步。