主 192.168.8.141
从 192.168.8.142
mysql5数据库安装(采用二进制版本)
shell> groupadd mysql
shell> useradd -g mysql mysql
shell> cd /usr/local/mysql
shell> chown -R mysql .
shell> chgrp -R mysql .
shell> scripts/mysql_install_db --user=mysql
shell> chown -R root .
shell> chown -R mysql data
shell> bin/mysqld_safe --user=mysql &
同步配置(主)
# cp /etc/my.cnf /etc/my.cnf_bak
# cp /usr/local/mysql/support-files/my-medium.cnf /etc/my.cnf
# scripts/mysql_install_db --user=mysql
# touch /usr/local/mysql/data/hcy.pid
# chown -R mysql:mysql/usr/local/mysql/
# 以上主从数据库都要这么配置
# vi /etc/my.cnf
server-id = 1
log-bin=mysql-bin
增加一个同步帐号
# grant all on *.* to 'hcy'@'%' identified by '123456';
# flush privileges;
FLUSH TABLES WITH READ LOCK;
UNLOCK TABLES;(锁表)
reset master(清空二进制日志)
mysql> create database dbback;
mysql> use dbback;
mysql> create table db_back_test
-> (
-> name varchar(10) not null
-> );
Query OK, 0 rows affected (0.02 sec)
# mysqladmin –uroot shutdown
# cd /usr/local/mysql/data
# tar zcvf dbback.tgz dbback/
把dbback.tgz拷贝到从数据库上。
# scp dbback.tg /usr/local/mysql/data/
# 登录从数据库上
# mysqladmin –uroot shutdown
# vi /etc/my.cnf
server-id = 2
master-host = 192.168.0.1
master-user = backup
master-password = 123456
replicate-do-db = dbback #同步的数据库,如果有多个数据库,每个数据库一行
replicate-ignore-db = mysql #不同步的数据库,如果有多个数据库,每个数据库一行
master-connect-retry = 60 #主从,从连接主,如果连接失败会尝试连接 8H ,这里我定义为 60秒。
slave-net-timeout = 30
log-bin = mysql-bin
# cd /usr/local/mysql/data/
# tar zxvf dbback.tgz
# 这时,把主从数据库都启动。
# 在从数据库上,执行
# slave start;
# show slave status\G;
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
如都出现yes,表示成功。
mysqldump --opt --master-data=2 dbname > db.sql
在主上把库这样导出里面会带有偏移量,再在从上chang master 即可,然后从库会追主库。
[root@web2 home]# more phpw.sql
-- MySQL dump 10.9
--
-- Host: localhost Database: phpw
-- ------------------------------------------------------
-- Server version 5.1.32-log
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;
/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;
--
-- Position to start replication or point-in-time recovery from
--
-- CHANGE MASTER TO MASTER_LOG_FILE='mysql-bin.000002', MASTER_LOG_POS=1598475;
阅读(971) | 评论(0) | 转发(0) |