日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

基于 CentOS Mysql 安装与主从同步配置详解

發布時間:2024/4/14 数据库 40 豆豆
生活随笔 收集整理的這篇文章主要介紹了 基于 CentOS Mysql 安装与主从同步配置详解 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

CentOS Mysql 安裝
Mysql (Master/Slave) 主從同步

1、為什么要使用主從同步

1.如果主服務器出現問題,可以快速切換到從服務器提供的服務
2.可以在從服務器上執行查詢操作,降低主服務器的訪問壓力
3.可以在從服務器上執行備份,以避免備份期間影響主服務器的服務

注意:一般只有更新不頻繁的數據或者對實時性要求不高的數據可以通過從服務器查詢,實時性要求高的數據仍然需要從主服務器獲得。

Window 數據庫主從(Master/Slave)同步安裝與配置詳解

2、 CentOS安裝Mysql 5.7.19

Centos7.3 安裝Mysql并修改初始密碼

Window 安裝Mysql并修改初始密碼

3、測試環境

我這里使用parallels desktop 虛擬機安裝的Centos操作系統
數據庫版本: 5.7.19
主機A:10.211.55.3(Master)
主機B:10.211.55.4(Slave)

service mysqld stop #停止數據庫 service mysqld start #啟動數據庫 service mysqld restart #重啟數據庫

4、配置主服務器Master

4.1 給從服務器設置授權用戶(創建復制帳號)

建立一個帳戶javen,并且只能允許從 10.211.55.4 這個地址上來登陸,密碼是123456。

mysql> grant replication slave on *.* to 'javen'@'10.211.55.4' identified by '123456'; mysql> flush privileges;

4.2 主服務器Master配置

在 etc/my.cnf的[mysqld]下添加如下內容

port=3306#[必須]啟用二進制日志 binlog-ignore-db=mysql #[必須]服務器唯一ID,默認是1 server-id= 1 #只保留7天的二進制日志,以防磁盤被日志占滿 expire-logs-days = 7 #不備份的數據庫 binlog-ignore-db=information_schema binlog-ignore-db=performation_schema binlog-ignore-db=sys binlog-ignore-db=gogs

4.3 重啟MySQL服務并設置讀取鎖定

service mysqld restart

在主服務器上設置讀取鎖定有效,確保沒有數據庫操作,以便獲得一個一致性的快照

mysql -u root -proot -P3306 mysql> flush tables with read lock;

4.4 查看主服務器上當前的二進制日志名和偏移量值

mysql> show master status; +------------------+----------+--------------+-------------------------------------------------------+-------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | +------------------+----------+--------------+-------------------------------------------------------+-------------------+ | mysql-bin.000001 | 154 | | mysql,information_schema,performation_schema,sys,gogs | | +------------------+----------+--------------+-------------------------------------------------------+-------------------+ 1 row in set (0.00 sec)

這里的 File 、Position 是在配置Salve的時候要使用到的,Binlog_Do_DB表示要同步的數據庫,Binlog_Ignore_DB 表示Ignore的數據庫,這些都是在配置的時候進行指定的。

另外:如果執行這個步驟始終為Empty set(0.00 sec),那說明前面的my.cnf 沒配置對。

5、配置從服務器Slave

5.1 修改從數據庫的配置

修改之后完整的配置如下:

# For advice on how to change settings please see # http://dev.mysql.com/doc/refman/5.7/en/server-configuration-defaults.html[mysqld] # # Remove leading # and set to the amount of RAM for the most important data # cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%. # innodb_buffer_pool_size = 128M # # Remove leading # to turn on a very important data integrity option: logging # changes to the binary log between backups. # log_bin # # Remove leading # to set options mainly useful for reporting servers. # The server defaults are faster for transactions and fast SELECTs. # Adjust sizes as needed, experiment to find the optimal values. # join_buffer_size = 128M # sort_buffer_size = 2M # read_rnd_buffer_size = 2M datadir=/var/lib/mysql socket=/var/lib/mysql/mysql.sock##以下是添加的內容log-bin=mysql-bin server-id=3 binlog-ignore-db = mysql binlog-ignore-db = information_schema binlog-ignore-db = performation_schema binlog-ignore-db = sys log-slave-updates slave-skip-errors=all slave-net-timeout=60# Disabling symbolic-links is recommended to prevent assorted security risks symbolic-links=0log-error=/var/log/mysqld.log pid-file=/var/run/mysqld/mysqld.pid

5.2 重啟從數據庫并設置Slave數據庫

service mysqld restart

登錄從數據庫并做如下設置

mysql> stop slave; #關閉Slave mysql> change master to master_host='10.211.55.3',master_user='javen',master_password='123456',master_log_file='mysql-bin.000001', master_log_pos= 154; mysql> start slave; #開啟Slave

注意:在這里指定Master的信息,master_log_file是在配置Master的時候的File選項, master_log_pos是在配置Master的Position 選項,這里要進行對應。
### 5.3 查看Slave配置的信息

mysql> show slave status \G; *************************** 1. row ***************************Slave_IO_State:Master_Host: 10.211.55.3Master_User: javenMaster_Port: 3306Connect_Retry: 60Master_Log_File: mysql-bin.000001Read_Master_Log_Pos: 154Relay_Log_File: centos-linux-2-relay-bin.000001Relay_Log_Pos: 4Relay_Master_Log_File: mysql-bin.000001Slave_IO_Running: NoSlave_SQL_Running: NoReplicate_Do_DB:Replicate_Ignore_DB:Replicate_Do_Table:Replicate_Ignore_Table:Replicate_Wild_Do_Table:Replicate_Wild_Ignore_Table:Last_Errno: 0Last_Error:Skip_Counter: 0Exec_Master_Log_Pos: 154Relay_Log_Space: 154Until_Condition: NoneUntil_Log_File:Until_Log_Pos: 0Master_SSL_Allowed: NoMaster_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: NoLast_IO_Errno: 0Last_IO_Error:Last_SQL_Errno: 0Last_SQL_Error:Replicate_Ignore_Server_Ids:Master_Server_Id: 0Master_UUID:Master_Info_File: /var/lib/mysql/master.infoSQL_Delay: 0SQL_Remaining_Delay: NULLSlave_SQL_Running_State:Master_Retry_Count: 86400Master_Bind:Last_IO_Error_Timestamp:Last_SQL_Error_Timestamp:Master_SSL_Crl:Master_SSL_Crlpath:Retrieved_Gtid_Set:Executed_Gtid_Set:Auto_Position: 0Replicate_Rewrite_DB:Channel_Name:Master_TLS_Version: 1 row in set (0.00 sec)ERROR: No query specifiedmysql> unlock tables; Query OK, 0 rows affected (0.00 sec)mysql> start slave; Query OK, 0 rows affected (0.00 sec)mysql>

6、關閉掉主數據庫的讀取鎖定

mysql> unlock tables;

7、在主數據庫中創建一個表以及添加數據測試

轉載于:https://www.cnblogs.com/zyw-205520/p/7349663.html

總結

以上是生活随笔為你收集整理的基于 CentOS Mysql 安装与主从同步配置详解的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。