mysql5.7 主從

一、安裝mysql5.7

1.1 下載mysql源安裝包

wget http://dev.mysql.com/get/mysql57-community-release-el7-8.noarch.rpm

1.2 安裝mysql源

yum install mysql57-community-release-el7-8.noarch.rpm

1.3 檢查mysql源是否安裝成功

yum repolist enabled | grep "mysql.*-community.*"

1.4 安裝

yum install mysql-community-server

1.5 啓動mysql服務

systemctl start mysqld #啓動
systemctl status mysqld #狀態

1.6 開機啓動

systemctl enable mysqld
systemctl daemon-reload

1.7 修改root本地登陸密碼

grep 'temporary password' /var/log/mysqld.log
ALTER USER 'root'@'localhost' IDENTIFIED BY 'TabY_opaw5';

二、主從配置

2.1 主庫建立同步帳號

    咱們要在主數據庫裏建立一個帳號,而且該帳號要授予 REPLICATION SLAVE 權限。mysql

create user 'repl'@'%' identified by 'TabX_opBo5';
GRANT REPLICATION SLAVE ON *.* TO 'repl'@'%';

2.2 配置主數據庫

    要主數據庫,你必需要啓用二進制日誌(binary logging),而且建立一個惟一的Server ID,這步驟可能要重啓MySQL。 
主服務器發送變動記錄到從服務器依賴的是二進制日誌,若是沒啓用二進制日誌,複製操做不能實現(主庫複製到從庫)。 
    複製組中的每臺服務器都要配置惟一的Server ID,取值範圍是1到(232)−1,你本身決定取值。 
配置二進制日誌和Server ID,你須要關閉MySQL和編輯my.cnf或者my.ini文件,在 [mysqld] 節點下添加配置。 編輯my.cnf:sql

vi /etc/my.cnf
server-id = 1
log_bin = master-bin
log_bin_index = master-bin.index
binlog_do_db = test
binlog_ignore_db = mysql

    備註:server-id 服務器惟一標識,log_bin 啓動MySQL二進制日誌,binlog_do_db 指定記錄二進制日誌的數據庫,binlog_ignore_db 指定不記錄二進制日誌的數據庫。 
    重啓mysql:數據庫

systemctl restart mysqld

    登錄主數據庫,查看數據庫狀態:服務器

mysql> show master status;
+-------------------+----------+--------------+------------------+-------------------+
| File              | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+-------------------+----------+--------------+------------------+-------------------+
| master-bin.000003 |      154 | test         | mysql            |                   |
+-------------------+----------+--------------+------------------+-------------------+
1 row in set

    注意:master-bin.000003和154這兩個值要記錄下來ide

2.3 配置從數據庫測試

    編輯my.cnf:spa

vi /etc/my.cnf

    添加rest

server-id = 2
relay-log = slave-relay-bin
relay-log-index = slave-relay-bin.index

    重啓mysql日誌

systemctl restart mysqld

    在slave服務器中登錄mysql,鏈接master主服務器數據庫code

change master to master_host='主庫ip', master_port=3306, master_user='repl', master_password='TabX_opBo5', master_log_file='master-bin.000003', master_log_pos=154;

    啓動slave:

start slave;
mysql> show slave status
;
+----------------------------------+---------------+-------------+-------------+---------------+-------------------+---------------------+------------------------+---------------+-----------------------+------------------+-------------------+-----------------+---------------------+--------------------+------------------------+-------------------------+-----------------------------+------------+------------+--------------+---------------------+-----------------+-----------------+----------------+---------------+--------------------+--------------------+--------------------+-----------------+-------------------+----------------+-----------------------+-------------------------------+---------------+---------------+----------------+----------------+-----------------------------+------------------+--------------------------------------+----------------------------+-----------+---------------------+--------------------------------------------------------+--------------------+-------------+-------------------------+--------------------------+----------------+--------------------+--------------------+-------------------+---------------+----------------------+--------------+--------------------+
| Slave_IO_State                   | Master_Host   | Master_User | Master_Port | Connect_Retry | Master_Log_File   | Read_Master_Log_Pos | Relay_Log_File         | Relay_Log_Pos | Relay_Master_Log_File | Slave_IO_Running | Slave_SQL_Running | Replicate_Do_DB | Replicate_Ignore_DB | Replicate_Do_Table | Replicate_Ignore_Table | Replicate_Wild_Do_Table | Replicate_Wild_Ignore_Table | Last_Errno | Last_Error | Skip_Counter | Exec_Master_Log_Pos | Relay_Log_Space | Until_Condition | Until_Log_File | Until_Log_Pos | Master_SSL_Allowed | Master_SSL_CA_File | Master_SSL_CA_Path | Master_SSL_Cert | Master_SSL_Cipher | Master_SSL_Key | Seconds_Behind_Master | Master_SSL_Verify_Server_Cert | Last_IO_Errno | Last_IO_Error | Last_SQL_Errno | Last_SQL_Error | Replicate_Ignore_Server_Ids | Master_Server_Id | Master_UUID                          | Master_Info_File           | SQL_Delay | SQL_Remaining_Delay | Slave_SQL_Running_State                                | Master_Retry_Count | Master_Bind | Last_IO_Error_Timestamp | Last_SQL_Error_Timestamp | Master_SSL_Crl | Master_SSL_Crlpath | Retrieved_Gtid_Set | Executed_Gtid_Set | Auto_Position | Replicate_Rewrite_DB | Channel_Name | Master_TLS_Version |
+----------------------------------+---------------+-------------+-------------+---------------+-------------------+---------------------+------------------------+---------------+-----------------------+------------------+-------------------+-----------------+---------------------+--------------------+------------------------+-------------------------+-----------------------------+------------+------------+--------------+---------------------+-----------------+-----------------+----------------+---------------+--------------------+--------------------+--------------------+-----------------+-------------------+----------------+-----------------------+-------------------------------+---------------+---------------+----------------+----------------+-----------------------------+------------------+--------------------------------------+----------------------------+-----------+---------------------+--------------------------------------------------------+--------------------+-------------+-------------------------+--------------------------+----------------+--------------------+--------------------+-------------------+---------------+----------------------+--------------+--------------------+
| Waiting for master to send event | 172.31.150.25 | root        |        3306 |            60 | master-bin.000003 |                 154 | slave-relay-bin.000010 |           321 | master-bin.000003     | Yes              | Yes               |                 |                     |                    |                        |                         |                             |          0 |            |            0 |                 154 |             528 | None            |                |             0 | No                 |                    |                    |                 |                   |                |                     0 | No                            |             0 |               |              0 |                |                             |                1 | fa758afe-7a75-11e8-988c-00163e058649 | /var/lib/mysql/master.info |         0 | NULL                | Slave has read all relay log; waiting for more updates |              86400 |             |                         |                          |                |                    |                    |                   |             0 |                      |              |                    |
+----------------------------------+---------------+-------------+-------------+---------------+-------------------+---------------------+------------------------+---------------+-----------------------+------------------+-------------------+-----------------+---------------------+--------------------+------------------------+-------------------------+-----------------------------+------------+------------+--------------+---------------------+-----------------+-----------------+----------------+---------------+--------------------+--------------------+--------------------+-----------------+-------------------+----------------+-----------------------+-------------------------------+---------------+---------------+----------------+----------------+-----------------------------+------------------+--------------------------------------+----------------------------+-----------+---------------------+--------------------------------------------------------+--------------------+-------------+-------------------------+--------------------------+----------------+--------------------+--------------------+-------------------+---------------+----------------------+--------------+--------------------+
1 row in set

三、測試

    在主庫的test庫中新建表和添加數據庫,會自動同步到slave庫中。

相關文章
相關標籤/搜索