本帖最后由 renduoa 于 2013-5-13 09:07 编辑
目的: 主从服务器设置的稳健性得以提升,如果主服务器发生故障,可以把本来作为备份的从服务器提升为新的主服务器。 在主从服务器上分开处理用户的请求,可获得更短的响应时间。 用从服务器做数据备份而不会占用主服务器的系统资源。
场景描述 主服务器:IP地址192.168.56.128,mysql已经安装,无用户数据。 从服务器:IP地址192.168.56.129,mysql已经安装。 主服务器的mysql服务已经正常启动
主从复制配置 (1)创建用户并授权 [backcolor=white !important][backcolor=rgb(224, 224, 224) !important][size=1em]1
[backcolor=white !important][size=1em]2
[backcolor=rgb(224, 224, 224) !important][size=1em]3
| [size=1em][backcolor=rgb(224, 224, 224) !important][size=1em]insert into mysql.user(host,user,password) values("localhost","rep1",password("mysql"));
[backcolor=white !important][size=1em]flush privileges;
[backcolor=rgb(224, 224, 224) !important][size=1em]grant replication slave on *.* torep1@192.168.56.129 identified by 'mysql';
|
(2)查询主数据库的状态 [backcolor=white !important][backcolor=rgb(224, 224, 224) !important][size=1em]1
| [size=1em][backcolor=rgb(224, 224, 224) !important][size=1em]mysql> show master status;
|
记下File以及Position的值,在后面进行从服务器操作的时候需要使用。 (1)修改从服务器的配置文件/etc/my.cnf 使得“server-id=2”,并确定这个id没有被别的mysql服务所使用。 (2)启动mysql服务 [backcolor=white !important]
| [backcolor=rgb(224, 224, 224) !important]mysqld_safe --user=mysql &
|
(3)mysql登录 [backcolor=white !important]
| [backcolor=rgb(224, 224, 224) !important]mysql -uroot -p
|
(4)执行同步的sql语句 [backcolor=white !important]
| [backcolor=rgb(224, 224, 224) !important]change master to master_host='192.168.56.128',master_user='rep1',master_password='mysql',master_log_file='mysql-bin.000004',master_log_pos=477;
|
正确执行后再执行 [backcolor=white !important]
| [backcolor=rgb(224, 224, 224) !important]mysql> start slave;
|
就启动了slave同步功能。 (5)主从同步检查 执行如下语句 [backcolor=white !important]
| [backcolor=rgb(224, 224, 224) !important]mysql>show slave status\G
|
来检查从服务器的同步情况,一个正常的输出结果应该如下面的形式:
Slave_IO进程以及slave_SQL进程都必须正常运行,在状态输出中表现为:“slave”;否则都是不正确的状态(如一个值是Yes,另一个是No则不行)。
如果主数据库服务器已经存在用户数据,那么在进行主从复制时,需要做以下处理。 (1)主数据库锁表操作,不让数据再进行写入动作。 [backcolor=white !important]
| [backcolor=rgb(224, 224, 224) !important]mysql>flush tables with read lock;
|
(2)查看主数据库的状态 [backcolor=white !important]
| [backcolor=rgb(224, 224, 224) !important]mysql>show master status;
|
记下File以及Position的值,以备从服务器使用。 (3)把主服务器的数据文件复制到从服务器,最好先用tar归档压缩处理一下 (4)取消主数据库锁定 [backcolor=white !important]
| [backcolor=rgb(224, 224, 224) !important]mysql>unlock tables;
|
从服务器的操作和前面的步骤一样,略过。 在主数据库服务器上创建库和表,然后插入记录,再登录到从服务器,查看是否也建立相一致的库和表以及记录。 (1)主服务器上的操作 [backcolor=white !important]
| [backcolor=rgb(224, 224, 224) !important]mysql>create database first_tb;
|
①在主数据库服务器创建库first_tb; [backcolor=white !important]
| [backcolor=rgb(224, 224, 224) !important]mysql>create table first_db(id int(3)),name char (10));
|
②在主数据库服务器创建表first_tb; [backcolor=white !important]
| [backcolor=rgb(224, 224, 224) !important]mysql>insert into first_tb values (001,'myself');
|
③在主数据服务器的表first_tb中插入记录; (2)从服务器上查看 [backcolor=white !important]
| [backcolor=rgb(224, 224, 224) !important]mysql>show databases;
|
①数据库first_db自动生成 ②表first_tb也自动生成,数据记也录存在。 实验二 导入数据库,在主库上查看现在的数据库
创建数据库buy并且导入数据
在从库上查看,数据库已经完全导入。
|