一、在两个主服务器上分别安装mysql。
useradd mysql
wget    http://122.225.32.131/icons/mysql-5.0.22.tar.gz
tar -zxvf mysql-5.0.22.tar.gz
cd      mysql-5.0.22
mkdir   /usr/local/mysql
./configure     \
--prefix=/usr/local/mysql       \
--without-debug \
--enable-thread-safe-client \
--enable-assembler \
--enable-profiling \
--with-mysqld-ldflags=-all-static \
--with-client-ldflags=-all-static \
--with-charset=latin1 \
--with-extra-charsets=all  \
--with-big-tables       \
--enable-largefile      \
make && make install
cp support-files/my-medium.cnf /etc/my.cnf
cd /usr/local/mysql/bin
./mysql_install_db --user=mysql
./mysqld_safe   --user=mysql    &
cp /root/mysql-5.0.22/support-files/mysql.server  /etc/rc.d/init.d/mysqld
chkconfig --add mysqld
chkconfig --level 345 mysqld on
sleep 3
mysql -uroot mysql -e "delete from user where user = '';"
mysql -uroot mysql -e "grant replication slave on *.* to 'repl'@'%'  identified by '123321';"
mysql -uroot mysql -e "grant shutdown on *.* to 'shutdown'@'%' identified by 'shutdown';"
//可以根据你的实际情况把%改为从服务器的IP,修改用户名和密码。
以上服务器是基本配置,如果要优化的情更具实际情况优化。
 
2、检查服务器配置文件,确认打开bing-log,并且配置serverid = 1
mysql
flush tables with read lock;
show master status;
+------------------+----------+--------------+------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000014 |   386037 |              |                  |
+------------------+----------+--------------+------------------+
记录以上数值,然后通过dump,或者tar -zcvf 备份 数据库或者var目录。
unlock tables;
exit
scp 备份的var目录到从服务器上。
两个主服务器都这样操作。
3、搭建从服务器。

 
useradd mysql
wget    http://122.225.32.131/icons/mysql-5.0.22.tar.gz
tar -zxvf mysql-5.0.22.tar.gz
cd      mysql-5.0.22
mkdir   /usr/local/mysql
./configure     \
--prefix=/usr/local/mysql       \
--without-debug \
--enable-thread-safe-client \
--enable-assembler \
--enable-profiling \
--with-mysqld-ldflags=-all-static \
--with-client-ldflags=-all-static \
--with-charset=latin1 \
--with-extra-charsets=all  \
--with-big-tables       \
--enable-largefile      \
make && make install
cp support-files/my-medium.cnf /etc/my.cnf
修改/etc/my.cnf
在结果加上以下代码
[mysqld_multi]
mysqld = /usr/local/mysql/bin/mysqld_safe
mysqladmin = /usr/local/mysql/bin/mysqladmin
user = shutdown
password =  shutdown
log = /usr/local/mysql/etc/mysqld_multi.log
[mysqld1]
socket          = /tmp/mysql.sock1
port            = 3306
pid-file        = /usr/local/mysql/var1/localhost.pid
datadir         = /usr/local/mysql/var1
user            = mysql
server-id       = 2
[mysqld2]
socket          = /tmp/mysql.sock2
port            = 3307
pid-file        = /usr/local/mysql/var2/localhost.pid
datadir         = /usr/local/mysql/var2
user            = mysql
server-id       = 2
然后
mkdir /usr/local/mysql/etc
cp support-files/mysqld_multi.server  /usr/sbin/mysqld_multi.server
chmod 755 /usr/sbin/mysqld_multi.server
export PATH=$PATH:/usr/sbin/:/usr/local/mysql/bin
如果想下次启动还生效,请修改/etc/profile
把刚才备份过来的var分别放到/usr/local/mysql/下面命令为var1和var2
var1和var2和/usr/local/mysql/etc的权限必须是mysql可以写。
然后启动mysql_multi.server
mysqld_multi.server start
通过netstat -tulnp 检查端口是否启动。
tcp        0      0 0.0.0.0:3306                0.0.0.0:*                   LISTEN      23623/mysqld
tcp        0      0 0.0.0.0:3307                0.0.0.0:*                   LISTEN      23733/mysqld
通过
mysql -S /tmp/mysql.sock1   //登录第一个mysql实例
mysql -S /tmp/mysql.sock2    //登录第二个mysql实例
mysqld_multi.servr stop 1 //关闭第一个mysql实例
mysqld_multi.servr stop 1-2 //关闭第一和第二个mysql实例
mysqld_multi.servr stop   //关闭所有mysql实例。start同样。
 
下面就是分别登录到实例1和实例2后配置mysql复制的从服务器就可以了。
mysql -S /tmp/mysql.sock1
change master to
master_host='第一个主服务器ip',
master_user=‘repl’,
master_password='123321',
master_log_file='mysql-bin.000014‘, \\刚才show master的数据
master_log_pos=386037;
start slave;
show slave status \G;
           Slave_IO_Running: Yes \\负责从主服务器读取binlog,如果为NO,应该是账号或者密码不对,不能连接到主服务器,或者是刚才的binlog号码或者pos号码有问题。请仔细检查
          Slave_SQL_Running: Yes \\负责执行IO传输过来的binlog。
检查以上两项是否为yes,如果是就OK了。
 
另一个实例如法炮制。