主服务器:172.16.15.20
从服务器:172.16.15.30
1、两节点分别安装mysql
- # pvcreate /dev/sda5
- # vgcreate myvg /dev/sda5
- # lvcreate -L 10G -n mydata myvg
- # mkdir -p /data/mydata
- # mke2fs -j /dev/myvg/mydata
- # mount /dev/myvg/mydata /data/mydata/
- # tar xf mysql-5.5.24-linux2.6-i686.tar.gz -C /usr/local/
- # cd /usr/local/
- # ln -s mysql-5.5.24-linux2.6-i686/ mysql
- # cd mysql
- # useradd -r mysql
- # chown -R mysql.mysql .
- # scripts/mysql_install_db --datadir=/data/mydata/ --user=mysql
- # chown -R root .
- # cp support-files/my-large.cnf /etc/my.cnf
- # vim /etc/my.cnf
- thread_concurrency = 2
- datadir = /data/mydata
- # cp support-files/mysql.server /etc/rc.d/init.d/mysqld
- # chmod +x /etc/rc.d/init.d/mysqld
- # service mysqld start
2、配置主服务器,添加用户
- mysql>GRANT REPLICATION SLAVE,REPLICATION CLIENT ON *.* TO repluser@'172.16.15.%' IDENTIFIED BY 'redhat';
- mysql>flush privileges;
- (注:一定要查看下使用的日志和日志位置,后面要用到)
- mysql>show master status;
- +------------------+----------+--------------+------------------+
- | File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
- +------------------+----------+--------------+------------------+
- | mysql-bin.000001 | 335 | | |
- +------------------+----------+--------------+------------------+
- 1 row in set (0.00 sec
3、配置从服务器
- # vim /etc/my.cnf
- relay-log=mysql-relay //添加中继日志
- server-id = 2 //修改ID和主的不一样
- # service mysqld restart 重启生效
- 添加以下命令
- mysql> CHANGE MASTER TO MASTER_HOST = '172.16.15.20' , //指定主服务器
- -> MASTER_USER = 'repluser' , //指定用户
- -> MASTER_PASSWORD = 'redhat' , //密码
- -> MASTER_LOG_FILE = 'mysql-bin.000001' , //指定日志
- -> MASTER_LOG_POS = 355 ; //指定日志位置
- 可以通过下面命令查看刚配置信息:
- mysql> show slave status\G
- 启动从服务线程
- mysql> start slave;
- 禁止从服务器写入,重启服务就失效了,应该将read_only = on 写进配置文件中:
- SET GLOBAL read_only = on;
- 配置文件[mysqld]中添加如下,让从服务器的mysql服务在启动时候不要自动启动从服务线程,手动启动
- skip-slave-start=1
4、测试
- 主服务器上建立数据
- mysql> create database tb1;
- mysql> use tb1;
- mysql> create table test (name varchar(10));
- mysql> insert into test values ('jack'),('tom');
查看下:
再在从服务器上看下,可以看到已经有复制过来的信息了:
5、下面模拟从服务的数据丢失:
删除从服务器上/data/mydata/下的所有数据
在主服务器上用之前建立的LVM通过快照在主服务其上建立物理备份:
- 首先锁表,之后一定不要退出,新开个终端执行物理备份
- mysql> flush tables with read lock;
- Query OK, 0 rows affected (0.55 sec)
- # lvcreate -L 50M -s -p r -n mydata-snap /dev/myvg/mydata
- # mount /dev/myvg/mydata-snap /mnt
- # cd /mnt/
- # tar jcf mysql_bak.tar.bz2 *
- # scp mysql_bak.tar.bz2 172.16.15.30:/root
- 在解锁之前先看下日志位置
- mysql> show master status;
- +------------------+----------+--------------+------------------+
- | File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
- +------------------+----------+--------------+------------------+
- | mysql-bin.000001 | 978 | | |
- +------------------+----------+--------------+------------------+
- 1 row in set (0.00 sec)
- 确认日志位置后解锁
- mysql> unlock tables;
- Query OK, 0 rows affected (0.00 sec)
- 之后插入一条数据,看一会从服务器会产生否
- mysql> insert into tb1.test values ('redhat');
- mysql> select * from tb1.test;
- +--------+
- | name |
- +--------+
- | jack |
- | tom |
- | redhat |
- +--------+
从服务器恢复:
# tar xf mysql_bak.tar.bz2 -C /data/mydata/
启动服务发现有报错
# ps -ef | grep mysql 发现僵死进程,杀掉重启还报错,看日志发现权限问题
# ll /data/mydata/ -d
drwxr-xr-x 7 root mysql 4096 Aug 6 14:25 /data/mydata/
# chown -R mysql .
启动OK
重新配置从服务器,指定解锁前的日志位置
- mysql> CHANGE MASTER TO MASTER_HOST = '172.16.15.20' ,
- -> MASTER_USER = 'repluser' ,
- -> MASTER_PASSWORD = 'redhat' ,
- -> MASTER_LOG_FILE = 'mysql-bin.000001' ,
- -> MASTER_LOG_POS = 978 ;
- mysql> slave start;
再查看,发现已经有数据了,并且redhat也有,之后主再插入数据,依旧复制: