一、准备工作
主服务器: 192.168.1.199
从服务器: 192.168.1.200
Mysql版本:mysql-5.6.15-linux-glibc2.5-x86_64.tar.gz
二、在主服务器和从服务器上安装MySQL数据库软件
MySQL数据库软件安装完成后,把mysql初始化生成的/usr/my.cnf(如果是从源文件编译安装时,路径应该是在/usr/local/mysql/mysql.cnf)删除,然后把优化好的mysql配置文件my.cnf放到/etc下。
三、修改主数据库的配置文件/etc/my.cnf
内容如下:
log-bin=mysqlmaster-bin.log
sync_binlog=1
innodb_buffer_pool_size=512M
innodb_flush_log_at_trx_commit=1
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
lower_case_table_names=1
log_bin_trust_function_creators=1
四、修改从数据库配置文件/usr/my.cnf
内容如下:
server-id=2
log-bin=mysqlslave-bin.log
sync_binlog=1
innodb_buffer_pool_size=512M
innodb_flush_log_at_trx_commit=1
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
lower_case_table_names=1
log_bin_trust_function_creators=1
五、重新启动主数据库和从数据库
[root@master ~]# service mysql restart
[root@slave ~]# service mysql restart
六、在主数据库上创建用于主从复制的账户
[root@master ~]# mysql –u root -p
mysql> GRANT REPLICATION SLAVE ON *.* TO 'repl'@'192.168.1.199' IDENTIFIED BY '111111';
Query OK, 0 rows affected (0.00 sec)
注意:以上命令中的IP地址,是从数据库服务器的IP地址。
七、主数据库锁表
禁止再插入数据以获取主数据库的的二进制日志坐标
mysql> flush tables with read lock;
Query OK, 0 rows affected (0.00 sec)
八、查看主数据库的状态
记录下File字段和Position字段的值,在配置从服务器时有用到
mysql> show master status;
+------------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------------+----------+--------------+------------------+-------------------+
| mysqlmaster-bin.000004 | 327 | | | |
+------------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
九、创建主数据库的快照文件
[root@master ~]# cd /usr/bin/
# ./mysqldump -uroot -p -h127.0.0.1 -P3306 --all-databases --triggers --routines --events >>/mnt/windows/all.sql
上面命令中的目录是一个共享目录,这个目录可以同时被主数据库服务器和从数据库服务器访问到。
如果没有这样的共享目录,可以将all.sql放在其它任何目录下,然后使用scp命令复制到远程从数据库服务器的某个目录中
这条命令的执行时间根据数据量的不同,会有所不同,如果主数据库的数据量很大,可能需要很长时间,那么在这种情况下,就最好在晚上没有业务的时候进行这个操作,否则第七步中的锁表操作会对业务系统造成很大的影响
十、解锁主数据库的锁表操作
[root@master ~]# mysql -uroot -p (本命令在主数据库服务器上执行)
mysql> unlock tables;
Query OK, 0 rows affected (0.00 sec)
十一、在从数据库服务器上导入第七步创建的快照文件到从数据库中
[root@slave ~]# mysql -uroot -p -h127.0.0.1 -P3306 < /mnt/windows/all.sql
十二、在从数据库服务器上设置主数据库服务器向从数据库服务器同步
[root@slave ~]# mysql –u root -p
mysql> change master to master_host = '192.168.1.199',master_user='repl',master_password='111111',master_log_file='mysqlmaster-bin.000004',master_log_pos=327;
注意:master_log_file和master_log_pos值,是在第八步中查出来的,这里不能弄错了
十三、启动从数据库复制线程
mysql> start slave;
Query OK, 0 rows affected (0.01 sec)
十四、查询从数据库的复制线程状态
mysql> show slave status \G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.1.199
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysqlmaster-bin.000004
Read_Master_Log_Pos: 327
Relay_Log_File: slave-relay-bin.000002
Relay_Log_Pos: 289
Relay_Master_Log_File: mysqlmaster-bin.000004
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: 327
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: 2e5e1b22-f0a9-11e3-bbac-000c297799e0
Master_Info_File: /var/lib/mysql/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)
如果Slave_IO_Running和Slave_SQL_Running两项都为yes,就表示主从复制配置成功了。
注意:如果锁表,主从导入数据库这几步不能实现的话,可以用一种笨办法,主从数据库用mysql可视化工具导入一模一样的数据库即可。