1、环境说明
MasterIP:192.168.12.150
SlaveIP: 192.168.12.151
2 、yum安装mysql
- yum install mysql-server -y
3、配置Master的Mysql,在mysqld模块下添加如下两行;
- [root@bogon ~]# vi /etc/my.cnf
- [mysqld]
- server-id = 1
- log-bin=mysql-bin 日志路径及文件名
4、在Master数据库上设置同步用户,并给用户授权;
- [root@bogon ~]# mysql -u root
- Welcome to the MySQL monitor. Commands end with ; or \g.
- Your MySQL connection id is 6
- Server version: 5.0.95-log Source distribution
- Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.
- Oracle is a registered trademark of Oracle Corporation and/or its
- affiliates. Other names may be trademarks of their respective
- owners.
- Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
- mysql> grant replication slave on *.* to 'rep'@'192.168.12.%' identified by '123456';
- Query OK, 0 rows affected (0.00 sec)
5、主库锁表,停止数据更新。(锁表的作用是,防止备份数据库的时候,数据库的写入,备份完数据库之后,查看主库的状态)
- mysql> flush tables with read lock;
6、查看主库的状态;
- mysql> show master status;
- +------------------+----------+--------------+------------------+
- | File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
- +------------------+----------+--------------+------------------+
- | mysql-bin.000003 | 234 | | |
- +------------------+----------+--------------+------------------+
- 1 row in set (0.00 sec)
- mysql>
将上面的日志名mysql-bin.000003和偏移量234记录下来,从库将已它作为同步点,它之前的数据我们通过mysqldump、mysql命令导出,导入到从数据库,它之后的数据我们将进行同步。
7、数据库解锁
- mysql> unlock tables;
8、修改Slave数据库的配置文件,只需要添加server-id = 2 就ok,并重启mysqld
- [root@localhost ~]# vi /etc/my.cnf
- [mysqld]
- server-id = 2
- datadir=/var/lib/mysq
- [root@localhost ~]# service mysqld restart Stopping mysqld: [ OK ]
9、在从库中开始手动同步数据库。
- mysql> change master to master_host='192.168.12.150',master_user='rep',master_password='123456',master_log_file='mysql-bin.000003' ,master_log_pos=234;
- Query OK, 0 rows affected (0.01 sec)
- mysql> start slave; 启动从数据库
- Query OK, 0 rows affected (0.00 sec)
- mysql> show slave status \G; 查看状态
- *************************** 1. row ***************************
- Slave_IO_State: Connecting to master
- Master_Host: 192.168.12.150
- Master_User: rep
- Master_Port: 3306
- Connect_Retry: 60
- Master_Log_File: mysql-bin.000003
- Read_Master_Log_Pos: 234
- Relay_Log_File: mysqld-relay-bin.000001
- Relay_Log_Pos: 98
- Relay_Master_Log_File: mysql-bin.000003
- 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: 234
- Relay_Log_Space: 98
- 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: NULL
- 1 row in set (0.00 sec)
- ERROR:
- No query specified
- mysql>
标红的两个线程显示状态为yes,说明同步成功。
10 、测试主从数据库是否同步成功
主库上创建数据库test,并查看是否创建成功检查从库是否自动创建数据test
- mysql> create database test;
- Query OK, 1 row affected (0.00 sec)
- mysql> show databases;
- +--------------------+
- | Database |
- +--------------------+
- | information_schema |
- | mysql |
- | test |
- +--------------------+
- 3 rows in set (0.00 sec)
检查从库是否自动创建数据test
- mysql> show databases;
- +--------------------+
- | Database |
- +--------------------+
- | information_schema |
- | mysql |
- | test |
- +--------------------+
- 3 rows in set (0.00 sec)
- mysql> show slave status \G;
- *************************** 1. row ***************************
- Slave_IO_State: Waiting for master to send event
- Master_Host: 192.168.12.150
- Master_User: rep
- Master_Port: 3306
- Connect_Retry: 60
- Master_Log_File: mysql-bin.000004
- Read_Master_Log_Pos: 777
- Relay_Log_File: mysqld-relay-bin.004413
- Relay_Log_Pos: 235
- Relay_Master_Log_File: mysql-bin.000004
- 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: 777
- 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)
- ERROR:
- No query specified
- mysql>
主库上删除数据库test,,并查看主库的状态
- mysql> drop database test;
- Query OK, 0 rows affected (0.00 sec)
- mysql> show databases;
- +--------------------+
- | Database |
- +--------------------+
- | information_schema |
- | mysql |
- +--------------------+
- 2 rows in set (0.00 sec)
检查从库是否自动删除数据库test,
- mysql> show databases;
- +--------------------+
- | Database |
- +--------------------+
- | information_schema |
- | mysql |
- +--------------------+
- 2 rows in set (0.00 sec)
- mysql> show slave status \G;
- *************************** 1. row ***************************
- Slave_IO_State: Waiting for master to send event
- Master_Host: 192.168.12.150
- Master_User: rep
- Master_Port: 3306
- Connect_Retry: 60
- Master_Log_File: mysql-bin.000004
- Read_Master_Log_Pos: 858
- Relay_Log_File: mysqld-relay-bin.011065
- Relay_Log_Pos: 235
- Relay_Master_Log_File: mysql-bin.000004
- 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: 858
- 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)
- ERROR:
- No query specified
自此达到主从同步成功。
总结:主从同步最最要的的得到主库的查看主库的状态;得到File和Position的值mysql-bin.000003 ,234 ,从库同步的时候会用到。
- mysql> show master status;
- +------------------+----------+--------------+------------------+
- | File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
- +------------------+----------+--------------+------------------+
- | mysql-bin.000003 | 234 | | |
- +------------------+----------+--------------+------------------+
- 1 row in set (0.00 sec)
- mysql>
因为我们同步的时候是以它作为同步点的,它之后的数据我们将自动同步,它之前的数据我们将通过mysqldump的命令导入的,所以在备份主库的时候我们一定要锁表,(flush tables with read lock; ) 这样数据库就不能写了,File和Position的值mysql-bin.000003 ,234 ,也就不会变化 这样我们就保证了数据同步的完整性。 备份完数据库之后我们就可以通过 unlock tables;
命令解锁了。然后我们通过在主库上授权的用户在从库上开始第一次手动同步,yihou