1. Environment
RedHat AS4.0
MySQL 5.1.42 :MySQL-server-community-5.1.42-0.rhel4.i386.rpm
MySQL-client-community-5.1.42-0.rhel4.i386.rpm
Tssavl01(as master server):16.173.241.51
Tssavl03(as slave server) :16.173.241.53
2. Installation
We use command ‘rpm –vih fullfilename’ to install all packages
3. Configuration
We change file /etc/my.cnf in two servers to configure, if this file not exist, go to directory /usr/share/mysql/ and copy a relevant file to that path base on hardware configuration, here we copied my-huge.cnf to /etc/my.cnf。
Master server configures:
Change in my.cnf:
log-bin=mysql-bin
server-id = 1
binlog-do-db = rep
note:
1. log-bin means open binlog, it’s premise of doing replication
2. generally server-id of master server set to 1
3. binlog-do-db=rep means database rep need backup
Slave server configures:
Change in my.cnf:
server-id = 2
master-host = 16.173.241.51
master-user = rep
master-password = rep
master-port = 3306
replicate-do-db = rep
log-bin=mysql-bin
note:
1. master-user and master-password means set an account to connect database in master server
2. replicate-do-db means we go to synchronize database rep
3. log-bin means open binlog, it’s premise of doing replication
Restart mysql server and make change effective
4. Distribution of privilege in master server
Login mysql in master server and run commands below:
grant all privileges on rep.* to [email protected] identified by ‘rep’;
This command gives account grep/grep in host:16.173.241.53 all privileges(include replication slave and file) to configure database rep.
Then run command ‘flush privileges’ to update permissions
5. Validation
Create database rep and schema test in master server
mysql> create database rep;
Query OK, 1 row affected (0.00 sec)
mysql> use rep
Database changed
mysql> create table test (a int(10),b int(10));
Query OK, 0 rows affected (0.01 sec)
Back to slave server and check:
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| rep |
| test |
+--------------------+
4 rows in set (0.00 sec)
mysql> use rep
Database changed
mysql> show tables;
+---------------+
| Tables_in_rep |
+---------------+
| test |
+---------------+
1 row in set (0.00 sec)
Note here the success of two server synchronization.
6. Frequently used commands
Master server上:
show master status
mysql> show master status;
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000001 | 288 | rep,rep | |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
Here the no. of position can’t be 0, if it is 0, we should check configuration of /etc/my.cnf, make sure server-id is right and log-bin opened
show processlist
mysql> show processlist;
+----+------+---------------------------+------+-------------+------+----------------------------------------------------------------+------------------+
| Id | User | Host | db | Command | Time | State | Info |
+----+------+---------------------------+------+-------------+------+----------------------------------------------------------------+------------------+
| 27 | rep | tssavl03.chn.hp.com:55699 | NULL | Binlog Dump | 935 | Has sent all binlog to slave; waiting for binlog to be updated | NULL |
| 28 | root | localhost | NULL | Query | 0 | NULL | show processlist |
+----+------+---------------------------+------+-------------+------+----------------------------------------------------------------+------------------+
2 rows in set (0.00 sec)
If replication succeed, there will be two processes above
Slave server上:
show processlist
mysql> show processlist;
+----+-------------+-----------+------+---------+------+-----------------------------------------------------------------------+------------------+
| Id | User | Host | db | Command | Time | State | Info |
+----+-------------+-----------+------+---------+------+-----------------------------------------------------------------------+------------------+
| 1 | system user | | NULL | Connect | 5167 | Waiting for master to send event | NULL |
| 2 | system user | | NULL | Connect | 2634 | Has read all relay log; waiting for the slave I/O thread to update it | NULL |
| 4 | root | localhost | NULL | Query | 0 | NULL | show processlist |
+----+-------------+-----------+------+---------+------+-----------------------------------------------------------------------+------------------+
3 rows in set (0.00 sec)
If replication succeed, there will be two processes above
For common errors, we can check error log hostname.err locate in /var/lib/mysql, here the log file in slave server named Tssavl03.err
fmHC;mso-bidi-font-family:宋体; color:#4F81BD;mso-themecolor:accent1'>Query OK, 1 row affected (0.00 sec)
mysql> use rep
Database changed
mysql> create table test (a int(10),b int(10));
Query OK, 0 rows affected (0.01 sec)
返回slave server查看:
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| rep |
| test |
+--------------------+
4 rows in set (0.00 sec)
mysql> use rep
Database changed
mysql> show tables;
+---------------+
| Tables_in_rep |
+---------------+
| test |
+---------------+
1 row in set (0.00 sec)
这里就说明两台server同步成功。
六、常用查看命令
Master server上:
show master status
mysql> show master status;
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000001 | 288 | rep,rep | |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
这里的position不能为0,如果为0则表示有问题,可检查/etc/my.cnf中的配置,server-id是否正确和是否打开了log-bin
show processlist
mysql> show processlist;
+----+------+---------------------------+------+-------------+------+----------------------------------------------------------------+------------------+
| Id | User | Host | db | Command | Time | State | Info |
+----+------+---------------------------+------+-------------+------+----------------------------------------------------------------+------------------+
| 27 | rep | tssavl03.chn.hp.com:55699 | NULL | Binlog Dump | 935 | Has sent all binlog to slave; waiting for binlog to be updated | NULL |
| 28 | root | localhost | NULL | Query | 0 | NULL | show processlist |
+----+------+---------------------------+------+-------------+------+----------------------------------------------------------------+------------------+
2 rows in set (0.00 sec)
若replication成功了,则这里会有如上两个进程
Slave server上:
show processlist
mysql> show processlist;
+----+-------------+-----------+------+---------+------+-----------------------------------------------------------------------+------------------+
| Id | User | Host | db | Command | Time | State | Info |
+----+-------------+-----------+------+---------+------+-----------------------------------------------------------------------+------------------+
| 1 | system user | | NULL | Connect | 5167 | Waiting for master to send event | NULL |
| 2 | system user | | NULL | Connect | 2634 | Has read all relay log; waiting for the slave I/O thread to update it | NULL |
| 4 | root | localhost | NULL | Query | 0 | NULL | show processlist |
+----+-------------+-----------+------+---------+------+-----------------------------------------------------------------------+------------------+
3 rows in set (0.00 sec)
Replication成功后slave server上也应该有如上三个进程。
查看/var/lib/mysql下的错误日志文件,这里在slave server上的日志文件是Tssavl03.err,一般的错误都能在这里查出。