当前位置:Gxlcms > 数据库问题 > Mysql Master-slave 主从配置

Mysql Master-slave 主从配置

时间:2021-07-01 10:21:17 帮助过:2人阅读

MySQL主从复制

 

场景描述:
主数据库服务器:192.168.10.130,MySQL已经安装,并且无应用数据。
从数据库服务器:192.168.10.131,MySQL已经安装,并且无应用数据。

2.1 主服务器上进行的操作

 

启动mysql服务

  1. /opt/mysql/init.d/mysql start

通过命令行登录管理MySQL服务器

  1. /opt/mysql/bin/mysql -uroot -p‘new-password‘

授权给从数据库服务器192.168.10.131

  1. mysql> GRANT REPLICATION SLAVE ON *.* to ‘rep1‘@‘192.168.10.131‘ identified by ‘password‘;

查询主数据库状态

  1. Mysql> show master status;
  2. +------------------+----------+--------------+------------------+
  3. | File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
  4. +------------------+----------+--------------+------------------+
  5. | mysql-bin.000005 | 261 | | |
  6. +------------------+----------+--------------+------------------+

记录下 FILE 及 Position 的值,在后面进行从服务器操作的时候需要用到。

2.2 配置从服务器

 

修改从服务器的配置文件/opt/mysql/etc/my.cnf

将 server-id = 1修改为 server-id = 10,并确保这个ID没有被别的MySQL服务所使用。

启动mysql服务

  1. /opt/mysql/init.d/mysql start

通过命令行登录管理MySQL服务器

  1. /opt/mysql/bin/mysql -uroot -p‘new-password‘

执行同步SQL语句

  1. mysql> change master to
  2. master_host=’192.168.10.130’,
  3. master_user=’rep1’,
  4. master_password=’password’,
  5. master_log_file=’mysql-bin.000005’,
  6. master_log_pos=261;

 

正确执行后启动Slave同步进程

  1. mysql> start slave;

 

主从同步检查

  1. mysql> show slave status\G
  2. ==============================================
  3. **************** 1. row *******************
  4. Slave_IO_State:
  5. Master_Host: 192.168.10.130
  6. Master_User: rep1
  7. Master_Port: 3306
  8. Connect_Retry: 60
  9. Master_Log_File: mysql-bin.000005
  10. Read_Master_Log_Pos: 415
  11. Relay_Log_File: localhost-relay-bin.000008
  12. Relay_Log_Pos: 561
  13. Relay_Master_Log_File: mysql-bin.000005
  14. Slave_IO_Running: YES
  15. Slave_SQL_Running: YES
  16. Replicate_Do_DB:
  17. ……………省略若干……………
  18. Master_Server_Id: 1
  19. 1 row in set (0.01 sec)
  20. ==============================================

 

其中Slave_IO_Running 与 Slave_SQL_Running 的值都必须为YES,才表明状态正常。

如果主服务器已经存在应用数据,则在进行主从复制时,需要做以下处理:

(1)主数据库进行锁表操作,不让数据再进行写入动作

 

  1. mysql> FLUSH TABLES WITH READ LOCK;

 

(2)查看主数据库状态

  1. mysql> show master status;

 

(3)记录下 FILE 及 Position 的值。

将主服务器的数据文件(整个/opt/mysql/data目录)复制到从服务器,建议通过tar归档压缩后再传到从服务器解压。

(4)取消主数据库锁定

  1. mysql> UNLOCK TABLES;

2.3 验证主从复制效果

 

主服务器上的操作

在主服务器上创建数据库first_db

  1. mysql> create database first_db;
  2. Query Ok, 1 row affected (0.01 sec)

 

在主服务器上创建表first_tb

  1. mysql> create table first_tb(id int(3),name char(10));
  2. Query Ok, 1 row affected (0.00 sec)

 

在主服务器上的表first_tb中插入记录

  1. mysql> insert into first_tb values (001,’myself’);
  2. Query Ok, 1 row affected (0.00 sec)

 

在从服务器上查看

  1. mysql> show databases;
  2. =============================
  3. +--------------------+
  4. | Database |
  5. +--------------------+
  6. | information_schema |
  7. | first_db |
  8. | mysql |
  9. | performance_schema |
  10. | test |
  11. +--------------------+
  12. 5 rows in set (0.01 sec)
  13. =============================

数据库first_db已经自动生成

 

  1. mysql> use first_db
  2. Database chaged
  3. mysql> show tables;
  4. =============================
  5. +--------------------+
  6. | Tables_in_first_db |
  7. +--------------------+
  8. | first_tb |
  9. +--------------------+
  10. 1 row in set (0.02 sec)
  11. =============================

数据库表first_tb也已经自动创建

 

  1. mysql> select * from first_tb;
  2. =============================
  3. +------+------+
  4. | id | name |
  5. +------+------+
  6. | 1 | myself |
  7. +------+------+
  8. 1 rows in set (0.00 sec)
  9. =============================

记录也已经存在

由此,整个MySQL主从复制的过程就完成了,接下来,我们进行MySQL读写分离的安装与配置。

Mysql Master-slave 主从配置

标签:

人气教程排行