怎样设置MySQL的主从复制【MySQL教程】,MySQL
设置细节:
主服务器:192.168.1.10
从服务器:192.168.1.20
数据库:mydb
1.设置MySQL主服务器
在主服务器上建立一个具有REPLICATION SLAVE权限的mysql帐户,复制客户端将连接到master。
mysql> GRANT REPLICATION SLAVE ON *.* TO 'repl_user'@'192.168.1.20' IDENTIFIED BY 'secretpassword'; mysql> FLUSH PRIVILEGES;
在所有表上都有block write语句,因而不要在备份后举行变动。
mysql> use mydb; mysql> FLUSH TABLES WITH READ LOCK; mysql> exit;
编辑mysql设置文件并在[mysqld]部份下增加以下代码。
# vim /etc/my.cnf
[mysqld] log-bin=mysql-bin binlog-do-db=mydb server-id=1 innodb_flush_log_at_trx_commit=1 sync_binlog=1
重新启动master mysql服务器以使变动见效。
# service mysqld restart
运用以下敕令搜检当前二进制日记文件名(File)和当前偏移量(Position)值。
mysql > SHOW MASTER STATUS; +------------------+----------+--------------+------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | +------------------+----------+--------------+------------------+ | mysql-bin.000002 | 107 | mydb | | +------------------+----------+--------------+------------------+
以上输出显现当前二进制文件运用的是mysql-bin.000002,偏移值为107。记下这些值以在隶属服务器上运用。
备份数据库并将其复制到slave mysql server。
# mysqldump -u root -p mydb > mydb.sql # scp mydb.sql 192.168.1.20:/opt/
完成备份后,从表中删除READ LOCK,以便举行变动。
mysql> UNLOCK TABLES;
2.设置MySQL Slave Server
编辑salve mysql设置文件并在[mysqld]部份下增加以下值。
# vim /etc/my.cnf
[mysqld] server-id=2 replicate-do-db=mydb
server-id一直为非零数值。这些值永久不会与其他主服务器和从服务器类似。
重启mysql slave server,假如你已设置了复制,请在启动时运用-skip-slave-start,不要马上连接到主服务器。
# /etc/init.d/mysqld restart
运用以下敕令在隶属服务器上设置选项值。
mysql> CHANGE MASTER TO MASTER_HOST='192.168.1.10', -> MASTER_USER='repl_user', -> MASTER_PASSWORD='secretpassword', -> MASTER_LOG_FILE='mysql-bin.000002', -> MASTER_LOG_POS=107;
末了启动隶属线程
mysql> SLAVE START;
搜检从服务器的状况。
mysql> show slave status G
*************************** 1. row *************************** Slave_IO_State: Master_Host: 192.168.1.15 Master_User: repl_user Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000002 Read_Master_Log_Pos: 107 Relay_Log_File: mysqld-relay-bin.000001 Relay_Log_Pos: 4 Relay_Master_Log_File: mysql-bin.000002 Slave_IO_Running: No Slave_SQL_Running: No Replicate_Do_DB: mydb 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: 107 Relay_Log_Space: 107 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: NULL 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 1 row in set (0.00 sec) mysql>
MySQL主从复制已在你的体系和事情形式下胜利设置。
以上就是怎样设置MySQL的主从复制的细致内容,更多请关注ki4网别的相干文章!