主服務器(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...