阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

MySQL主从安装配置

142次阅读
没有评论

共计 3162 个字符,预计需要花费 8 分钟才能阅读完成。

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)

本文永久更新链接地址:http://www.linuxidc.com/Linux/2017-02/140477.htm

正文完
星哥说事-微信公众号
post-qrcode
 0
星锅
版权声明:本站原创文章,由 星锅 于2022-01-22发表,共计3162字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中