分类: Mysql/postgreSQL
2011-11-12 22:59:16
1、系统安装环境:
用的是radhat 5.5操作系统 mysql5.1.54版本
master 计算机名:node1 IP地址:master_host
slave 计算机名:node2 IP地址:slave_host
2、mysql安装
root登陆,执行如下步骤:
shell> groupadd mysql
shell> useradd -g mysql mysql
shell> gunzip < mysql-VERSION.tar.gz | tar -xvf -
shell> cd mysql-VERSION
shell> ./configure --prefix=/usr/local/mysql
shell> make
shell> make install
shell> cp support-files/my-medium.cnf /etc/my.cnf
shell> cd /usr/local/mysql
shell> bin/mysql_install_db --user=mysql
shell> chown -R root .
shell> chown -R mysql var
shell> chgrp -R mysql .
bin/mysql_install_db --user=mysql
3、配置mysql复制
1). 确保主服务器开启log-bin,如“log-bin=mysql-bin”,主从的log-bin值最好一样便于主从切换,主从服务器的server_id取不同的数字。 (在/etc/my.cnf配置)
2). 在主服务器上,设置一个复制账户,并授予REPLICATION SLAVE权限:
mysql > GRANT REPLICATION rep ON *.* TO 'rep'@'slave_host' IDENTIFIED BY 'repl_pass';
3). 在主服务器上,设置读锁定有效(若不锁定,偏移量会变化,主从服务器数据难一致):
mysql > FLUSH TABLES WITH READ LOCK;
然后得到主服务器上当前的二进制日志名和偏移量值:
mysql> show master status\G;
*************************** 1. row ***************************
File: mysql-bin.000008
Position: 756
Binlog_Do_DB: cacti
Binlog_Ignore_DB: mysql
1 row in set (0.00 sec)
4). 在从服务器上,做相应设置:
stop slave;
CHANGE MASTER TO
MASTER_HOST='master_host',
MASTER_PORT=master_port,
MASTER_USER='rep’,
MASTER_PASSWORD=' rep_pass ',
MASTER_LOG_FILE='mysql-bin.000008',
MASTER_LOG_POS=756;
5). 在从服务器上,启动slave线程:
mysql> START SLAVE;
这时slave上执行show processlist; 命令将显示类似如下进程:
10436 | system user 。。。|Waiting for master to send event | NULL
这表明slave已经连接上master,并开始接受并执行日志。
运行一段时间后在slave上执行show processlist\G; 命令将显示类似如下进程:
*************************** 2. row ***************************
Id: 2
User: system user
Host:
db: NULL
Command: Connect
Time: 0
State: Has read all relay log; waiting for the slave I/O thread to update it
Info: NULL
在master上执行show processlist\G; 命令将显示类似如下进程:
*************************** 8. row ***************************
Id: 608
User: replication
Host: 'slave_host':59716
db: NULL
Command: Binlog Dump
Time: 753
State: Has sent all binlog to slave; waiting for binlog to be updated
Info: NULL
6). 在主服务器上,重置读锁定:
mysql> UNLOCK TABLES;