主服务器(mysql-master):IP地址:123.125.1.1,mysql已安装,没有用户数据。
从服务器(mysql-slave):IP地址:123.125.1.2,mysql已安装,没有用户数据。
主从服务器均可正常提供服务。
主从复制配置如下:
在主服务器上操作:
1)、确保/etc/my.cnf中有如下参数,没有的话需手工添加,并重启mysql服务。
[mysqld]
log-bin=mysql-bin 启动二进制文件
server-id=1 服务器ID
2)、登录主服务器mysql,在mysql中添加一个repl的账号,并授权给从服务器。
[root@localhost ~]# mysql -S /tmp/mysql3306.sock 登录mysql
mysql> grant replication slave on *.* to 'repl'@'123.125.1.2' identified by 'repl'; 创建repl用户,并授权给123.125.1.2使用。
3)、查询主数据库状态,并记下FILE及Position的值,这个在后面配置从服务器的时候要用到。
mysql> show master status;
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+---------------+----------+--------------+------------------+
| binlog.000008 | 106 | |
在从服务器上操作:
1)、确保/etc/my.cnf中有log-bin=mysql-bin和server-id=1参数,并把server-id=1修改为server-id=10(只要不等于主服务器的值)。修改之后如下所示:
[mysqld]
log-bin=mysql-bin 启动二进制文件
server-id=10 服务器ID
2)、重启从mysql服务。
[root@localhost ~]# /usr/local/webserver/mysql/bin/mysqladmin shutdown -S /tmp/mysql3306.sock
[root@localhost ~]# /usr/local/webserver/mysql/bin/mysqld_safe --defaults-file=/data0/mysql/3306/my.cnf &
3)、登录从mysql,执行如下语句,此处配置对应主服务器上信息
[root@localhost ~]# /usr/local/webserver/mysql/bin/mysql -S /tmp/mysql3306.sock
mysql> change master to master_host='123.125.1.1',master_user='repl',master_password='repl',
master_log_file='binlog.000008',master_log_pos=106;
4)、启动slave同步。
mysql> start slave;
5)、检查主从同步,如果您看到Slave_IO_Running和Slave_SQL_Running均为Yes,则主从复制连接正常。
mysql> show slave status\G
Slave_IO_State: Waiting for master to send event
Master_Host: 123.125.1.2
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: binlog.000008
Read_Master_Log_Pos: 205
Relay_Log_File: relaylog.000015
Relay_Log_Pos: 347
Relay_Master_Log_File: binlog.000008
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
验证配置是否正常,mysql主从能否正常复制。
在主数据库上新建一个库,并且在库中写一个表和一些数据。
[root@localhost ~]# /usr/local/webserver/mysql/bin/mysql -S /tmp/mysql3306.sock
mysql> create database spytest;
mysql> use spytest;
mysql> create table spy(id int(5),name char(10));
mysql> insert into user values (101,'spy');
在从数据库中验证一下,是否正常复制到数据。
[root@localhost ~]# /usr/local/webserver/mysql/bin/mysql -S /tmp/mysql3306.sock
mysql> show databases;
mysql> select * from spytest.spy;
int_id | name |
+------+-------+
| 101 | spy |
成功了!FIRST BLOOD...