主机:192.168.102.131 从机:192.168.102.130
配置主服务器(master)
1、编辑数据库配置文件my.cnf。
- #vi /etc/my.cnf
在[mysqld]的下面加入下面代码:
- log-bin=mysql-bin
- server-id=1
- innodb_flush_log_at_trx_commit=1
- sync_binlog=1
- skip_networking=0
2、然后重启MySQL:
- #service mysqld restart
3、登录MySQL服务器。
- #mysql -uroot -p
在主服务器新建一个用户赋予“REPLICATION SLAVE”的权限。你不需要再赋予其它的权限。在下面的命令,把X.X.X.X替换为从服务器的IP。
- mysql>CREATE USER 'user'@ 'X.X.X.X' IDENTIFIED BY 'password';
- mysql>GRANT REPLICATION SLAVE ON *.* TO 'user'@'X.X.X.X' IDENTIFIED BY 'password';
4、执行以下命令锁定数据库以防止写入数据。
- mysql>FLUSH TABLES WITH READ LOCK;
5、退出mysql命令行,导出数据库
- #mysqldump -u root -p123456 --all-databases --lock-tables=false -- > /home/all.sql
6、使用scp命令传输数据库文件all.sql到从服务器。
- #scp /home/all.sql root@192.168.102.130:/home
7、再次连接数据库进入mysql命令行查看master状态。
- mysql>SHOW MASTER STATUS;
请记下显示的信息,配置从服务器会用到。
+——————+———-+————–+——————+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+——————+———-+————–+——————+
| mysql-bin.000001 | 506 | | |
+——————+———-+————–+——————+
1 row in set (0.00 sec)
8、解锁数据表。
- mysql>UNLOCK TABLES;
配置从服务器(slave)
登录从服务器。
1、导入主服务器的数据库。
- #mysql -u root -p123456 < /home/all.sql
2、编辑配置文件my.cnf,在[mysqld]下面加入:
- server-id=2
2可以自己定义,只要保证唯一的就行。
3、保存文件并重启mysqld。
- #service mysqld restart
4、登录mysql服务器,执行以下命令。
- mysql>CHANGE MASTER TO
- MASTER_HOST='192.168.102.131',
- MASTER_USER='user',
- MASTER_PASSWORD='password',
- MASTER_PORT=3306,
- MASTER_LOG_FILE='mysql-bin.000001',
- MASTER_LOG_POS=506,
- MASTER_CONNECT_RETRY=10;
MASTER_HOST:主服务器的IP。
MASTER_USER:配置主服务器时建立的用户名
MASTER_PASSWORD:用户密码
MASTER_PORT:主服务器mysql端口,如果未曾修改,默认即可。
5、启动slave进程。
- mysql>START SLAVE;
mysql> start slave;
Query OK, 0 rows affected (0.02 sec)
mysql> show slave status \G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.102.131
Master_User: user
Master_Port: 3306
Connect_Retry: 10
Master_Log_File: mysql_bin.000001
Read_Master_Log_Pos: 506
Relay_Log_File: www-relay-bin.000002
Relay_Log_Pos: 291
Relay_Master_Log_File: mysql_bin.000001
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 506
Relay_Log_Space: 462
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 1
Master_UUID: cd8d7b2d-e113-11e4-83e2-37e1c433b3e9
Master_Info_File: /usr/local/mysql/data/master.info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set:
Executed_Gtid_Set:
Auto_Position: 0
1 row in set (0.00 sec)
执行 mysql> show slave status \G 可以看到IO和SQL进程都为Yes,即配置成功。
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
6、验证:
登陆主服务器更新数据。
mysql> use hi
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> show tables;
+--------------+
| Tables_in_hi |
+--------------+
| hi |
+--------------+
1 row in set (0.00 sec)
mysql> select * from hi;
+------+------+
| id | name |
+------+------+
| 1 | bobu |
+------+------+
1 row in set (0.00 sec)
mysql> insert into hi values(2,'test');
Query OK, 1 row affected (0.06 sec)
mysql> select * from hi;
+------+------+
| id | name |
+------+------+
| 1 | bobu |
| 2 | test |
+------+------+
2 rows in set (0.00 sec)
登陆到从服务器查询是否可以同步复制成功。
mysql> use hi
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> select * from hi;
+------+------+
| id | name |
+------+------+
| 1 | bobu |
| 2 | test |
+------+------+
2 rows in set (0.00 sec)
查询结果显示第二条数据库已同步复制成功。
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/30164674/viewspace-1699136/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/30164674/viewspace-1699136/