|
|
MySQL主從複製(Master-Slave)
转载自: http://heylinux.com/archives/1004.html Mysql作为目前世界上使用最广泛的免费数据库,相信所有从事系统运维的工程师都一定接触过。但在实际的生产环境中,由单台Mysql作为独立的数据库是完全不能满足实际需求的,无论是在安全性,高可用性以及高并发等各个方面。 因此,一般来说都是通过 主从复制(Master-Slave)的方式来同步数据,再通过读写分离(MySQL-Proxy)来提升数据库的并发负载能力 这样的方案来进行部署与实施的。 下面是我在实际工作过程中所整理的笔记,在此分享出来,以供大家参考。 一、MySQL的安装与配置 具体的安装过程,建议参考我的这一篇文章:http://heylinux.com/archives/993.html 值得一提的是,我的安装过程都是源码包编译安装的,并且所有的配置与数据等都统一规划到了/opt/mysql目录中,因此在一台服务器上安装完成以后,可以将整个mysql目录打包,然后传到其它服务器上解包,便可立即使用。 二、MySQL主从复制 场景描述: 主数据库服务器:192.168.10.130,MySQL已经安装,并且无应用数据。 从数据库服务器:192.168.10.131,MySQL已经安装,并且无应用数据。 2.1 主服务器上进行的操作 启动mysql服务 /opt/mysql/init.d/mysql start
通过命令行登录管理MySQL服务器 /opt/mysql/bin/mysql -uroot -p'new-password'
授权给从数据库服务器192.168.10.131 mysql> GRANT REPLICATION SLAVE ON *.* to 'rep1'@'192.168.10.131' identified by ‘password’;
查询主数据库状态 Mysql> show master status;
记录下 FILE 及 Position 的值,在后面进行从服务器操作的时候需要用到。+------------------+----------+--------------+------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | +------------------+----------+--------------+------------------+ | mysql-bin.000005 | 261 | | | +------------------+----------+--------------+------------------+ 2.2 配置从服务器 修改从服务器的配置文件/opt/mysql/etc/my.cnf 将 server-id = 1修改为 server-id = 10,并确保这个ID没有被别的MySQL服务所使用。 启动mysql服务 /opt/mysql/init.d/mysql start
通过命令行登录管理MySQL服务器 /opt/mysql/bin/mysql -uroot -p'new-password'
执行同步SQL语句 mysql> change master to
master_host=’192.168.10.130’, master_user=’rep1’, master_password=’password’, master_log_file=’mysql-bin.000005’, master_log_pos=261; 正确执行后启动Slave同步进程 mysql> start slave;
主从同步检查 mysql> show slave status\G
============================================== **************** 1. row ******************* Slave_IO_State: Master_Host: 192.168.10.130 Master_User: rep1 Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000005 Read_Master_Log_Pos: 415 Relay_Log_File: localhost-relay-bin.000008 Relay_Log_Pos: 561 Relay_Master_Log_File: mysql-bin.000005 Slave_IO_Running: YES Slave_SQL_Running: YES Replicate_Do_DB: ……………省略若干…………… Master_Server_Id: 1 1 row in set (0.01 sec) ============================================== 其中Slave_IO_Running 与 Slave_SQL_Running 的值都必须为YES,才表明状态正常。 如果主服务器已经存在应用数据,则在进行主从复制时,需要做以下处理: (1)主数据库进行锁表操作,不让数据再进行写入动作 mysql> FLUSH TABLES WITH READ LOCK;
(2)查看主数据库状态 mysql> show master status;
(3)记录下 FILE 及 Position 的值。 将主服务器的数据文件(整个/opt/mysql/data目录)复制到从服务器,建议通过tar归档压缩后再传到从服务器解压。 (4)取消主数据库锁定 mysql> UNLOCK TABLES;
2.3 验证主从复制效果 主服务器上的操作 在主服务器上创建数据库first_db mysql> create database first_db;
Query Ok, 1 row affected (0.01 sec) 在主服务器上创建表first_tb mysql> create table first_tb(id int(3),name char(10));
Query Ok, 1 row affected (0.00 sec) 在主服务器上的表first_tb中插入记录 mysql> insert into first_tb values (001,’myself’);
Query Ok, 1 row affected (0.00 sec) 在从服务器上查看 mysql> show databases;
数据库first_db已经自动生成============================= +--------------------+ | Database | +--------------------+ | information_schema | | first_db | | mysql | | performance_schema | | test | +--------------------+ 5 rows in set (0.01 sec) ============================= mysql> use first_db
记录也已经存在Database chaged mysql> show tables; ============================= +--------------------+ | Tables_in_first_db | +--------------------+ | first_tb | +--------------------+ 1 row in set (0.02 sec) ============================= 数据库表first_tb也已经自动创建 mysql> select * from first_tb; ============================= +------+------+ | id | name | +------+------+ | 1 | myself | +------+------+ 1 rows in set (0.00 sec) ============================= 由此,整个MySQL主从复制的过程就完成了,接下来,我们进行MySQL读写分离的安装与配置。 |
|
104休閒信箱 2.3.0 © 104mm.com 2001 - 2021. | 您尚未登錄 |