mysql主从复制环境部署

来源:互联网 发布:网络层的4个功能 编辑:程序博客网 时间:2024/06/02 05:12

Linux环境:Centos6.8 64-bit

Mysql版本:5.1.7

 

一、在主库和从库安装mysql(安装mysql步骤

二、部署过程

1、修改主库/etc下的 my.conf 配置文件

vi /etc/my.conf[mysqld]log-bin=mysql-bin //[必须]启用二进制日志server-id=1 //[必须]服务器唯一ID,默认是1,一般取IP最后一段binlog-ignore-db=mysql #新增的配置,忽略mysql库的同步binlog-ignore-db=information_schema #新增的配置,忽略information_schema 的同步

2、修改从服务器slave:

vi /etc/my.cnf[mysqld]log-bin=mysql-bin //[不是必须]启用二进制日志server-id=2 //[必须]服务器唯一ID,默认是1,一般取IP最后一段

3、重启更改过配置的主库和从库mysql服务

 service mysqld restart

4、在主服务器上建立帐户并授权slave:

#赋予权限:以下2个选一个就可以了。根据你们自己的业务去定#1、赋予复制的权限mysql> GRANT REPLICATION SLAVE ON *.* to 'slave1'@'%' identified by '123456' with grant option;Query OK, 0 rows affected (0.02 sec)#2、赋予全部权限mysql> GRANT ALL PRIVILEGES ON *.* TO 'slave1'@'%' IDENTIFIED BY '123456' WITH GRANT OPTION;Query OK, 0 rows affected (0.02 sec)#刷新权限mysql> flush privileges;Query OK, 0 rows affected (0.00 sec)


5、登录主服务器的mysql,查询master的状态: 
mysql>show master status;
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
mysql-bin.000002 | 267 | | |
+------------------+----------+--------------+------------------+

1 row in set (0.00 sec)   

注:mysql-bin.000002    和    267 在配置从服务器的时候需要用到,执行完此步骤后不要再操作主服务器MYSQL,防止主服务器状态值变化

6、配置从服务器(Slave):

mysql>change master to master_host='192.168.202.130',master_user='slave1',master_password='123456',
master_log_file='mysql-bin.000002',master_log_pos=267; //注意不要断开,267数字前后无单引号。

7、启动从服务器的数据库复制功能,在从服务器执行如下语句:

mysql>start slave;  -- (相反,停止从服务器的主从复制功能 :stop  slave;)

8、检查从服务器复制功能状态:
mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.202.130
Master_User: slave1
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000002
Read_Master_Log_Pos: 267
Relay_Log_File: mysqld-relay-bin.000002
Relay_Log_Pos: 253
Relay_Master_Log_File: mysql-bin.000002
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: 267
Relay_Log_Space: 410
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
1 row in set (0.00 sec)
注:Slave_IO及Slave_SQL进程必须正常运行,即YES状态,否则都是错误的状态(如:其中一个NO均属错误)。

以上操作过程,主从服务器配置完成。

9、主从服务器测试:主服务器Mysql,建立数据库,并在这个库中建表插入一条数据

mysql> create database test;Query OK, 1 row affected (0.00 sec)mysql> use test;Database changedmysql> create table tb1(id int(3),age int(3));Query OK, 0 rows affected (0.00 sec)mysql> insert into tb1 values(001,3);Query OK, 1 row affected (0.00 sec)



从服务器查询数据是否已经同步

原创粉丝点击