CentOS6.5搭建MySQL5.1主從複製

環境:mysql

mysql> select version();
+------------+
| version()  |
+------------+
| 5.1.73-log |
+------------+
1 row in set (0.00 sec)

  

[root@10-4-14-168 ~]# cat /etc/redhat-release 
CentOS release 6.5 (Final)

  

1:已經事先購買了兩臺雲主機sql

10.4.14.168      101.69.178.208 [聯通] Master數據庫

10.4.5.9         101.69.178.219 [聯通] Slave spa

2:分別安裝MySQL日誌

 

 
# yum install mysql-server –enablerepo=remi
 
# yum install mysql-devel

 

3:查看MySQL安裝狀況code

 [root@10-4-14-168 ~]#  rpm -qa | grep mysql*server

mysql-5.1.73-3.el6_5.x86_64
mysql-libs-5.1.73-3.el6_5.x86_64
mysql-server-5.1.73-3.el6_5.x86_64
mysql-devel-5.1.73-3.el6_5.x86_64

 

4:啓動MySQLblog

 

service mysqld start

 

 

5:登錄MySQL而且修改密碼而且刪除空用戶ip

 

mysql> UPDATE mysql.user SET password = PASSWORD('ge0513.mysql') 
    -> WHERE user = 'root';
Query OK, 3 rows affected (0.00 sec)
Rows matched: 3  Changed: 3  Warnings: 0

mysql> SELECT user,host,password FROM mysql.user;
+------+-------------+-------------------------------------------+
| user | host        | password                                  |
+------+-------------+-------------------------------------------+
| root | localhost   | *7AE39BE5035D5C32361400FF7DEDD757AA76896A |
| root | 10-4-14-168 | *7AE39BE5035D5C32361400FF7DEDD757AA76896A |
| root | 127.0.0.1   | *7AE39BE5035D5C32361400FF7DEDD757AA76896A |
|      | localhost   |                                           |
|      | 10-4-14-168 |                                           |
+------+-------------+-------------------------------------------+
5 rows in set (0.00 sec)
mysql> DROP user ''@localhost;
Query OK, 0 rows affected (0.00 sec)

mysql> DROP user ''@'10-4-14-168';
Query OK, 0 rows affected (0.00 sec)

 

6 :主庫需改配置文件,在/etc/my.cnf中添加rem

port = 3306
log_bin = /var/lib/mysql/mysql-binlog
server-id = 1 //只要主從不同就行
binlog_do_db = test//要作同步的數據庫名字,能夠是多個數據庫,之間用分號分割。

  從庫的配置文件中添加

server-id = 2 
master-host = 10.4.14.168 
master-user = gechong
master-password = gechong
master-port = 3306
master-connect-retry = 5
replicate-do-db = test

  分別重啓服務,登錄數據庫。

 

7:主庫上建立複製用戶

GRANT REPLICATION SLAVE,REPLICATION CLIENT ON *.* TO gechong@'%' IDENTIFIED BY 'gechong';

8:在主庫上執行

mysql> show master status;
+---------------+----------+--------------+------------------+
| File          | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+---------------+----------+--------------+------------------+
| binlog.000003 |      412 |              |                  |
+---------------+----------+--------------+------------------+
1 row in set (0.00 sec)

從庫上執行

 

mysql> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 10.4.14.168
                  Master_User: gechong
                  Master_Port: 3306
                Connect_Retry: 5
              Master_Log_File: mysql-binlog.000001
          Read_Master_Log_Pos: 325
               Relay_Log_File: mysqld-relay-bin.000002
                Relay_Log_Pos: 473
        Relay_Master_Log_File: mysql-binlog.000001
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB: test
          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: 325
              Relay_Log_Space: 629
              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: 
1 row in set (0.00 sec)

 

能夠看到:Slave_IO_Running | Slave_SQL_Running兩個值都是YES,說明配置成功了。能夠在主庫的test庫裏執行DML或者DDL驗證下。

 

 

 

若是同步不成功:

 

1:停掉從庫
mysql> slave stop
2:主庫上找到日誌和位置
mysql> show master status;
3:手動同步
mysql> change master to
> master_host='master_ip',
> master_user='gechong',
> master_password='gechong',
> master_port=3306,
> master_log_file='mysql-bin.000020',
> master_log_pos=135617781;
1 row in set (0.00 sec)
4:啓動從庫
mysql> slave start;
1 row in set (0.00 sec)

 

  

 

 

若是有異常須要跳過:

>slave stop;
>SET GLOBAL sql_slave_skip_counter = 1;
>slave start;

  

主從搭建通常步驟:

相關文章
相關標籤/搜索