您的位置:首页 > 数据库 > MySQL

MySQL-(Master-Slave)配置

2014-11-10 22:25 381 查看
1、两台机器,安装好版本一致的MySQL

  192.168.29.128 (master)  MySQL-5.6.21

  192.168.29.129 (slave)   MySQL-5.6.21

2、配置master

  配置binary log和server ID

[root@Luxh-01 ~]# vi /etc/my.cnf


  配置如下:

[mysqld]
log-bin=mysql-bin
server-id=1


  重启MySQL

3、配置slave

[mysqld]
server-id=2


4、在master上创建用于复制的账号

mysql> create user 'repl'@'%' identified by '123456';
Query OK, 0 rows affected (0.13 sec)

mysql> grant replication slave on *.* to 'repl'@'%' ;
Query OK, 0 rows affected (0.02 sec)


5、在master上查看 binary log文件名和 position

mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000003 |      120 |              |                  |                   |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)


6、配置slave

mysql> change master to master_host='192.168.29.128',master_user='repl',master_password='123456',master_port=3306,master_log_file='mysql-bin.000003',master_log_pos=120,master_connect_retry=10;
Query OK, 0 rows affected, 2 warnings (0.30 sec)


  使用上面创建的用于复制的账号,master_log_file使用master上查询出来的文件名,master_log_pos也必须使用master上查询出来的position

7、在slave上启动复制功能

mysql> start slave;
Query OK, 0 rows affected (0.01 sec)


8、查看slave的状态

mysql> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.29.128
Master_User: repl
Master_Port: 3306
Connect_Retry: 10
Master_Log_File: mysql-bin.000003
Read_Master_Log_Pos: 120
Relay_Log_File: Luxh-02-relay-bin.000002
Relay_Log_Pos: 283
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: 120
Relay_Log_Space: 458
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
Master_UUID: 9567210c-684f-11e4-b062-000c29398b4c
Master_Info_File: /var/lib/mysql/master.info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set:
Executed_Gtid_Set:
Auto_Position: 0
1 row in set (0.00 sec)


9、配置完毕。测试一下效果。

  1)在master上执行以下语句

mysql> create database web_db;
Query OK, 1 row affected (0.04 sec)

mysql> use web_db;
Database changed
mysql> create table t_user(id int primary key auto_increment,username varchar(32));
Query OK, 0 rows affected (0.13 sec)

mysql> insert into t_user(username) values('lihuai');
Query OK, 1 row affected (0.05 sec)

mysql> select * from t_user;
+----+----------+
| id | username |
+----+----------+
|  1 | lihuai   |
+----+----------+
1 row in set (0.00 sec)


  2)在slave上查看,看是否已成功复制

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
| web_db             |
+--------------------+
5 rows in set (0.05 sec)

mysql> use web_db;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> show tables;
+------------------+
| Tables_in_web_db |
+------------------+
| t_user           |
+------------------+
1 row in set (0.00 sec)

mysql> select * from t_user;
+----+----------+
| id | username |
+----+----------+
|  1 | lihuai   |
+----+----------+
1 row in set (0.00 sec)


10、以上配置是针对全新安装的MySQL服务器做主从复制,还可以针对已有数据的数据库、或者已有的主从结构上再添加slave

  具体配置,可以参考官网文档,非常详细。

  http://dev.mysql.com/doc/refman/5.6/en/replication-howto.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: