从源码开始搭建MySQL主从服务器
时间:2010-03-21 来源:skybin090804
测试环境:
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 # make && make install
# ln -s /usr/local/mysql-5.1.33 /usr/local/mysql
保持编译默认配置,启动数据库,为了方便起见可以把mysql的目录写到PATH变量中.
# export PATH=$PATH:/usr/local/mysql/bin
二、修改主数据库的配置文件 _format=mixed参数不是必须的,可以注 释掉。
# vi /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下,配置专用的二进制日志传输用户.
mysql> grant replication slave,reload,super on *.* to [email protected] 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 [email protected]:/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; 本文来源: http://wangxiaoyu.blog.51cto.com/922065/216685
我使用的是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 # make && make install
# ln -s /usr/local/mysql-5.1.33 /usr/local/mysql
保持编译默认配置,启动数据库,为了方便起见可以把mysql的目录写到PATH变量中.
# export PATH=$PATH:/usr/local/mysql/bin
二、修改主数据库的配置文件 _format=mixed参数不是必须的,可以注 释掉。
# vi /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下,配置专用的二进制日志传输用户.
mysql> grant replication slave,reload,super on *.* to [email protected] 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 [email protected]:/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; 本文来源: http://wangxiaoyu.blog.51cto.com/922065/216685
相关阅读 更多 +