Jquery中文网 www.jquerycn.cn
Jquery中文网 >  服务器技术  >  数据库服务器  >  正文 从源码开始搭建MySQL主从服务器的简单配置

从源码开始搭建MySQL主从服务器的简单配置

发布时间:2014-07-20   编辑:www.jquerycn.cn
简单把从源码开始搭建MySQL主从服务器的步骤记下来。

简单把从源码开始搭建MySQL主从复制服务器的步骤记下来。
如有谬误还请指正啊。
 
测试环境:
Redhat 5u3
MySQL 5.1.33
主数据库服务器IP:192.168.254.2
从数据库服务器IP:192.168.254.3
 
一、分别在主从数据库上编译MySQL-server并安装
我使用的是mysql-5.1.33.tg.gz
下面是我的编译参数。
 

复制代码 代码如下:
#  ./configure --prefix=/usr/local/mysql-5.1.33 --without-comment --without-debug --with-ssl --with-charset=gbk --with-extra-charsets=gb2312,utf8 --enable-thread-safe-client --enable-local-infile --enable-assembler --with-big-tables --with-plugins=innobase
# make
# make install

# ln -s /usr/local/mysql-5.1.33 /usr/local/mysql
保持编译默认配置,启动数据库,为了方便起见可以把mysql的目录写到PATH变量中.
# export PATH=$PATH:/usr/local/mysql/bin
关于更详细的安装过程请参照:
[目录]Linux|UNIX下LAMP环境的搭建及常见问题
 
三、编译和安装MySQL
 
四、关于MySQL的正常启动
 
五、安装MySQL中可能遇到的问题
 
这里就不来再赘述了。

二、修改主数据库的配置文件
更权威的信息请访问:http://dev.mysql.com/doc/refman/5.1/en/replication-howto.html
[主服务器]
eth1:192.168.254.2
参照http://dev.mysql.com/doc/refman/5.1/en/replication-howto-masterbaseconfig.html,binlog_format=mixed参数不是必须的,可以注
释掉。
# vim /etc/my.cnf
 

复制代码 代码如下:
[mysqld]
# Replication Master Server (default)
# binary logging is required for replication
log-bin=mysql-bin
# binary logging format - mixed recommended
binlog_format=mixed
# required unique id between 1 and 2^32 - 1
# defaults to 1 if master-host is not set
# but will not function as a master if omitted
server-id       = 1

三、启动主数据库并,生成主从复制用的账户

# service mysqld start
在主数据库服务器上,进入mysql下,配置专用的二进制日志传输用户.
也可参照http://dev.mysql.com/doc/refman/5.1/en/replication-howto-repuser.html只使用replication slave权限即可。
mysql> grant replication slave,reload,super on *.* to slave@192.168.254.3 identified by 'slavepass';
mysql> flush privileges;
mysql> exit;
请在从数据库服务器上测试这个账户:
 

复制代码 代码如下:
# mysql -u slave -h 192.168.254.2 -pslavepass
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.1.33-log Source distribution
Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
mysql>

四、给主数据库建立一个完整的快照
 
上面是测试成功的情况。可以通过show privileges;来查看一下这个账户都有什么权限。
在shell下使用mysqldump 来备份一次主数据库:
# mysqldump --all-database --lock-all-tables > dbbackup_`date +%F'-'%T`.dump

请特别注意在此期间最好没有任何写入操作。可以在mysql下锁定数据库的写:
mysql> FLUSH TABLES WITH READ LOCK;
并记下二进制日志的偏移量:
注意如果在my.cnf中包含了主服务器的信息,这一步就不是必须。
mysql> show master status;
+------------------+----------+--------------+------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000004 |      332 |              |                  |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
 
在第六步中,在成功把这个快照导入到从数据库后便可以打开数据库的锁:
mysql> UNLOCK TABLES;
 
然后直接复制到从服务器上:
我在这里使用scp命令。
# scp dbbackup*.dump root@192.168.254.3:/root/
输入从服务器的密码。
 
五、修改从服务器的my.cnf
 

复制代码 代码如下:
[从服务器]
eth1:192.168.254.3
# vim /etc/my.cnf
[mysqld]
# required unique id between 2 and 2^32 - 1
# (and different from the master)
# defaults to 2 if master-host is set
# but will not function as a slave if omitted
server-id       = 2
#
# 下面都是主服务器的信息,实际我们也可以在服务器启动后,实时地通过mysql命令来修改它们,都不是必须的。
# The replication master for this slave - required
master-host     =   192.168.254.2
#
# The username the slave will use for authentication when connecting
# to the master - required
master-user     =   slave
#
# The password the slave will authenticate with when connecting to
# the master - required
master-password =   slavepass
#
# The port the master is listening on.
# optional - defaults to 3306
#master-port     =  3306
#
# binary logging - not required for slaves, but recommended
#log-bin=mysql-bin
 

 
六、启动从服务器,并导入传输过来的,主数据库快照
# service mysqld start
# mysql < dbbackup*.sql
 
现在在另一个窗口中打开从数据库的锁:
mysql> UNLOCK TABLES;
同步数据:
如果在第五步中编译my.cnf的时候没有包含主服务器的信息,我们也可以在mysql命令下实时修改。
 

复制代码 代码如下:
mysql> CHANGE MASTER TO MASTER_HOST='ServerIP/FQDN', MASTER_USER='ReplClient', MASTER_PASSWORD='ClientPassword',
MASTER_LOG_FILE='mysql-bin.000001', MASTER_LOG_POS=98;

例如我是直接执行:
 

复制代码 代码如下:
mysql> CHANGE MASTER TO MASTER_HOST='192.168.254.2', MASTER_USER='slave',
MASTER_PASSWORD='slavepass',MASTER_LOG_FILE='mysql-bin.000004',MASTER_LOG_POS=332;
Query OK, 0 rows affected (0.02 sec)

启动同步
mysql> start slave;
 
七、其它的一些主从数据库的管理操作
查看状态
主:
mysql> show master status;
从:
mysql> show slave status;
 
启动和停止同步
启动:
mysql> start slave;
停止:
mysql> stop slave;
 
其它信息我会随时补充的。>>> 更多内容,请访问:mysql主从复制、mysql主从同步系列教程

您可能感兴趣的文章:
从源码开始搭建MySQL主从服务器的简单配置
php服务器配置(php+mysql+iis)步骤
mysql主从复制配置与原理分析
windows下mysql主从复制配置
简单配置mysql的主从复制
MySQL实现实时备份的配置过程详解
(图)RHE5服务器管理-搭建DNS服务器
【php学习】wamp基础环境的搭建
linux下apache mysql php开发环境纯源代码编译搭建
apache 怎么安装

[关闭]