码迷,mamicode.com
首页 > 数据库 > 详细

mysql主从服务器

时间:2016-06-08 19:00:00      阅读:293      评论:0      收藏:0      [点我收藏+]

标签:

在[mysqld]下添加

-------------主服务器---------------
《步骤1》
vi /etc/my.cnf (源码编译安装的mysql配置文件所在)/data0/mysql/3306/my.cnf

[mysqld]
server-id = 1
log-bin = mysql-bin
binlog-do-db = huoli
binlog-do-db = db2
binlog-ignore-db = test
binlog-ignore-db = mysql
binlog-ignore-db = information_schema

service mysql restart

《步骤2》
登陆mysql命令行,执行
GRANT REPLICATION SLAVE ON *.*  TO ‘帐号‘@‘从服务器IP‘ IDENTIFIED BY ‘密码‘;
REPLICATION SLAVE

GRANT REPLICATION SLAVE ON *.* TO ‘backup‘@‘192.168.1.171‘ IDENTIFIED BY ‘878899‘;
GRANT REPLICATION SLAVE ON *.* TO ‘backup‘@‘192.168.1.172‘ IDENTIFIED BY ‘878899‘;

测试账号是否可以连接使用


《步骤3》
-------------主服务器-------------------
锁定主机数据库表
msyql>FLUSH TABLES WITH READ LOCK;
Query OK, 0 rows affected (0.13 sec)
查看主机 Master 状态
mysql>SHOW MASTER STATUS; ————》这一步看到的信息是《步骤6》中的信息,注意改正确! 
备份主机数据库
cd /usr/local/mysql/var/ 源码编译安装的mysql的数据库位置 /data0/mysql/3306/data/
tar zcvf db_backup.tar.gz ibdata* ib_logfile* huoli/
mv db_backup.tar.gz /tmp


《步骤4》
-------------从服务器-------------
vi /etc/my.cnf 源码编译安装的mysql的配置文件 vi /data0/mysql/3306/my.cnf server-id (从机的ID号不能重复,要唯一)
[mysqld]
server-id = 2 
log-bin=mysql-bin
replicate-do-db = huoli
replicate-do-db = db2
replicate-ignore-db = test
replicate-ignore-db = mysql
replicate-ignore-db = information_schema

service mysql start 重启mysql /data0/mysql/3306/mysql restart

《步骤5》
---------从服务器----------
为了保持数据一致:
$ service mysql stop /data0/mysql/3306/mysql stop
$ cd /usr/local/mysql/var 源码编译安装mysql的数据库路劲 /data0/mysql/3306/data/
删除从机上的日志和数据
$ rm -rf ibdata* ib_logfile* mysql-bin.* master.info relay-log.info huoli/ [有类似数据库则删除]
复制主mysql的数据文件到当前目录,解压缩
$ scp 192.168.1.172:/tmp/db_backup.tar.gz ./
$ tar zxvf db_backup.tar.gz
修改权限
$ chown -R mysql.mysql ibdata* ib_logfile* huoli/


《步骤6》
-----------在从服务器进行-------------
先启动mysql
/data0/mysql/3306/mysql start
停止原来的备份
mysql>slave stop;
Query OK, 0 rows affected, 1 warning (0.00 sec)
设置备份点(根据主服务器的日志文件和记录点来设置)
mysql>CHANGE MASTER TO MASTER_HOST=‘192.168.1.170‘,MASTER_PORT=3306,MASTER_USER=‘backup‘,MASTER_PASSWORD=‘878899‘,MASTER_LOG_FILE=‘mysql-bin.000001‘,MASTER_LOG_POS=107;
Query OK, 0 rows affected (0.02 sec)
启动备份
mysql>slave start;
Query OK, 0 rows affected (0.02 sec)
 解锁主机数据库表
msyql>UNLOCK TABLES;
Query OK, 0 rows affected (0.00 sec)

 

mysql>show slave status\G

执行后信息显示如下:

*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 172.19.203.200 ————》这是主服务器IP
Master_User: backup
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 524
Relay_Log_File: localhost-relay-bin.000002
Relay_Log_Pos: 235
Relay_Master_Log_File: mysql-bin.000010
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB: db1,db2
Replicate_Ignore_DB: test,mysql,information_schema
Exec_Master_Log_Pos: 524
Master_SSL_Key:
Seconds_Behind_Master: 0

 

mysql主从服务器

标签:

原文地址:http://www.cnblogs.com/isuben/p/5570678.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!