MySQL主从安装配置
1、环境介绍
OS:CentOS6.7_x64
MySQL:5.1.73
2、MySQL安装
yum -y install mysql-server
注意事项:
主从设备的MySQL版本、硬件配置要一致,否则可能会导致一系列问题,如从库来不及消费主库的日志,会引起从库日志文件堆积等!
3、主从配置
主要操作步骤:
配置binlog和serverid;
建立同步帐号(master),并授权;
根据master的状态,配置slave同步;
启动slave的复制功能;
查看主从复制状态slave IO和SQL的running都为yes,即为配置成功;
测试。
3.1 修改主、从服务器的mysql配置文件my.ini添加如下配置
[mysqld]
log-bin=mysql-bin //[不是必须]启用二进制日志
server-id=112 //[必须]服务器唯一ID,默认是1,一般取IP最后一段
3.2 重启两台服务器的mysql
/etc/init.d/mysql restart
3.3 在主服务器上建立帐户并授权slave
[linuxidc@lb01 ~]$ mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.1.73-log Source distribution
Copyright (c) 2000, 2013, 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 'mysync'@'%' identified by 'q123456';
Query OK, 0 rows affected (0.00 sec)
mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)
注意:
同步帐号一般不用root;
在实际工作中,只能授权单个IP,不能是通配符的形式授权;
如果有多个ip,就每个ip单独执行一遍授权语句;
3.4 在主服务器上查看mysql状态
mysql> show master status;
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000001 | 476 | | |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
注:执行完此步骤后不要再操作主服务器MYSQL,防止主服务器状态值变化
3.5 配置从服务器
mysql> change master to master_host='10.86.87.254',master_user='mysync',master_password='q123456', master_log_file='mysql-bin.000001',master_log_pos=476;
Query OK, 0 rows affected (0.02 sec)
mysql> start slave; //启动复制功能
Query OK, 0 rows affected (0.00 sec)
3.6 查看主从服务器复制功能状态
mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 10.86.87.254 //主服务器地址
Master_User: mysync //授权帐户名,尽量避免使用root
Master_Port: 3306 //数据库端口,部分版本没有此行
Connect_Retry: 60
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 476 //同步读取二进制日志的位置,大于等于Exec_Master_Log_Pos
Relay_Log_File: mysqld-relay-bin.000002
Relay_Log_Pos: 251
Relay_Master_Log_File: mysql-bin.000001
Slave_IO_Running: Yes //此状态必须YES
Slave_SQL_Running: Yes //此状态必须YES
...
注:Slave_IO及Slave_SQL进程必须正常运行,即YES状态,否则都是错误的状态(如:其中一个NO均属错误)。
主从服务器配置完成。
4、测试
4.1 主服务器Mysql
建立数据库,并在这个库中建表插入一条数据
mysql> create database test_db;
Query OK, 1 row affected (0.00 sec)
mysql> use test_db;
Database changed
mysql> create table test_tb(id int(3),name char(10));
Query OK, 0 rows affected (0.00 sec)
mysql> insert into test_tb values(001,'bobu');
Query OK, 1 row affected (0.00 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema|
| test_db |
| mysql |
| test |
+--------------------+
4 rows in set (0.00 sec)
从服务器Mysql查询
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema|
| test_db | //I'M here,大家看到了吧
| mysql |
| test |
+--------------------+
4 rows in set (0.00 sec)
mysql> use test_db
Database changed
mysql> select * from test_tb; //查看主服务器上新增的具体数据
+--------+------+
| id | name|
+--------+------+
| 1 | bobu|
+--------+------+
1 row in set (0.00 sec)