mysql 主從部署

1.準備3臺服務器  (如果是鏡像的同一個mysql 需要注意,同步的時候要修改uuid)

   192.168.0.74 主服務器

   192.168.0.75 從服務器

   192.168.0.18 maxscale 服務器

2.配置主服務器 192.168.0.74

  1.vim /etc/my.cnf

# 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

# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0

log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
#指定服務器id
server_id=74
#啓用binlog日誌,並指定文件名後綴
log-bin=master74
## 複製過濾:也就是指定哪個數據庫不用同步(mysql庫一般不同步)
binlog-ignore-db=mysql
binlog-ignore-db=sys
binlog-ignore-db=information_schema
binlog-ignore-db=performance_schem
## 爲每個session 分配的內存,在事務過程中用來存儲二進制日誌的緩存
binlog_cache_size=16M
### 主從複製的格式(mixed,statement,row,默認格式是statement)
binlog_format=mixed
### 二進制日誌自動刪除/過期的天數。默認值爲0,表示不自動刪除。
expire_logs_days=30
### 跳過主從複製中遇到的所有錯誤或指定類型的錯誤,避免slave端複製中斷。
### 如:1062錯誤是指一些主鍵重複,1032錯誤是因爲主從數據庫數據不一致
slave_skip_errors=1062
### 控制binlog的寫入頻率。每執行多少次事務寫入一次
### 這個參數性能消耗很大,但可減小MySQL崩潰造成的損失,爲0表示不控制
sync_binlog = 1
innodb_flush_log_at_trx_commit = 1
~

  2.重啓生效   systemctl restart mysqld

2.主服務器添加授權用戶,並查看bInlog日誌信息

 登錄主服務器

mysql -u root -p 123456

mysql> grant all on *.*to repluser@'%' identified by '111111';

  mysql> show master status;

3.從服務器配置  

vim /etc/my.cnf

[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

# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0

log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
server_id=75
## 複製過濾:也就是指定哪個數據庫不用同步(mysql庫一般不同步)
binlog-ignore-db=mysql
binlog-ignore-db=sys
binlog-ignore-db=information_schema
binlog-ignore-db=performance_schem
## 爲每個session 分配的內存,在事務過程中用來存儲二進制日誌的緩存
binlog_cache_size=16M
### 主從複製的格式(mixed,statement,row,默認格式是statement)
binlog_format=mixed
### 二進制日誌自動刪除/過期的天數。默認值爲0,表示不自動刪除。
expire_logs_days=30
### 跳過主從複製中遇到的所有錯誤或指定類型的錯誤,避免slave端複製中斷。
### 如:1062錯誤是指一些主鍵重複,1032錯誤是因爲主從數據庫數據不一致
slave_skip_errors=1062
### relay_log配置中繼日誌
relay_log=mysql-relay-bin
### log_slave_updates表示slave將複製事件寫進自己的二進制日誌
### 主要爲了作爲其他的master
log_slave_updates=ON
### 防止改變數據(除了特殊的線程)
read_only=1 #(爲了使備機隨時轉正,所以這裏允許寫)
### MySQL主從複製的時候,當Master和Slave之間的網絡中斷,但是Master和Slave無法察覺的情況下(比如防火牆或者路由問題)。Slave會等待slave_net_timeout設置的秒數後,才能認爲網絡出現故障,
然後纔會重連並且追趕這段時間主庫的數據,默認60
slave-net-timeout = 20
### 如果啓用,此變量將在服務器啓動後立即啓用自動中繼日誌恢復。
relay_log_recovery = ON
### 該變量確定從站在中繼日誌中的位置是寫入FILE還是寫入表
relay_log_info_repository = TABLE

 保存 生效  systemctl restart mysqld

3.登錄 從服務器 配置好ip ,賬號,密碼   注意, master_log_file 的路徑,master重啓一次,這個值就會變化一次,要重新運行

change master to master_host='192.168.1.50',master_user='repluser',master_password='123456',master_log_file='master50.000001',master_log_pos=154;
show slave status\G; 查看同步狀況
start slave; 開啓同步

mysql> show slave status\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_User: repluser
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: master74.000002
          Read_Master_Log_Pos: 629
               Relay_Log_File: mysql-relay-bin.000002
                Relay_Log_Pos: 794
        Relay_Master_Log_File: master74.000002
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB:
          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: 629
              Relay_Log_Space: 1001
              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:
  Replicate_Ignore_Server_Ids:
             Master_Server_Id: 74
                  Master_UUID: d1dcb1cd-5e38-11ec-9d29-fa202017ac1f
             Master_Info_File: /var/lib/mysql/master.info
                    SQL_Delay: 0
          SQL_Remaining_Delay: NULL
      Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
           Master_Retry_Count: 86400
                  Master_Bind:
      Last_IO_Error_Timestamp:
     Last_SQL_Error_Timestamp:
               Master_SSL_Crl:
           Master_SSL_Crlpath:
           Retrieved_Gtid_Set:
            Executed_Gtid_Set:
                Auto_Position: 0
         Replicate_Rewrite_DB:
                 Channel_Name:
           Master_TLS_Version:

 

Slave_IO_Running: Yes
Slave_SQL_Running: Yes 

表示成功

如果有false  則表示同步失敗

報錯: Fatal error: The slave I/O thread stops because master and slave have equal MySQL server UUIDs; these UUIDs must be different for replication to work.

表示從庫和主庫的uuid 相同

解決: 找到auto.cnf文件,修改裏面的uuid值,保證各個db的uuid不一樣,重啓db即可

[root@localhost ~]# find / -name auto.cnf
/var/lib/mysql/auto.cnf

[root@localhost ~]# cat /var/lib/mysql/auto.cnf    //查看主數據庫auto.cnf文件中的UUID信息
[auto]
server-uuid=e46c9961-5780-11ea-bf2f-000c291a8b6b

[root@localhost ~]# mysql -uroot -p123qqq...A
...
mysql> show variables like '%server_uuid%';   //查看主數據庫UUID
+---------------+--------------------------------------+
| Variable_name | Value                                |
+---------------+--------------------------------------+
| server_uuid   | e46c9961-5780-11ea-bf2f-000c291a8b6b |
+---------------+--------------------------------------+

更改從庫數據
[root@dbsrv2 ~]# mv /data/mysqldata/auto.cnf  /data/mysqldata/auto.cnf.bk  ###重命名該文件
[root@dbsrv2 ~]#  systemctl restart mysqld          ###重啓mysql

 添加數據在主庫上添加後,可以在從庫上看到數據 從庫上添加數據後,主庫看不到 

三、配置MaxScale代理服務器

maxscale下載地址:https://downloads.mariadb.com/MaxScale/

[root@instance-0k9n9mw6 solf]# rpm -ivh maxscale-2.4.5-1.centos.7.x86_64.rpm
warning: maxscale-2.4.5-1.centos.7.x86_64.rpm: Header V4 RSA/SHA1 Signature, key ID 28c12247: NOKEY
error: Failed dependencies:
libgnutls.so.28()(64bit) is needed by maxscale-2.4.5-1.x86_64
libgnutls.so.28(GNUTLS_1_4)(64bit) is needed by maxscale-2.4.5-1.x86_64
libgnutls.so.28(GNUTLS_3_0_0)(64bit) is needed by maxscale-2.4.5-1.x86_64
libgnutls.so.28(GNUTLS_3_1_0)(64bit) is needed by maxscale-2.4.5-1.x86_64
[root@instance-0k9n9mw6 solf]# rpm -ivh maxscale-2.4.5-1.centos.7.x86_64.rpm --force --nodeps
warning: maxscale-2.4.5-1.centos.7.x86_64.rpm: Header V4 RSA/SHA1 Signature, key ID 28c12247: NOKEY
Preparing... ################################# [100%]
Updating / installing...
1:maxscale-2.4.5-1 ################################# [100%]

修改 配置文件

vim /etc/maxscale.cnf

內容

2.4中移除MySQL驅動,全部統一使用MariaDB驅動,即配置文件中,選擇驅動的時候,不需要修改**[MariaDB Monitor]爲[MySQL Monitor]**

# MaxScale documentation:
# https://mariadb.com/kb/en/mariadb-maxscale-24/

# Global parameters
#
# Complete list of configuration options:
# https://mariadb.com/kb/en/mariadb-maxscale-24-mariadb-maxscale-configuration-guide/

[maxscale]
# 開啓線程個數,默認爲1.設置爲auto會同cpu核數相同

threads=auto

# timestamp精度

#ms_timestamp=1

# 將日誌寫入到syslog中

#syslog=1

# 將日誌寫入到maxscale的日誌文件中

#maxlog=1

# 不將日誌寫入到共享緩存中,開啓debug模式時可打開加快速度

#log_to_shm=0

# 記錄告警信息

#log_warning=1

# 記錄notice

#log_notice=1

# 記錄info

#log_info=1

# 不打開debug模式

#log_debug=0

# 日誌遞增

#log_augmentation=1
# 相關目錄設置
#basedir=/usr/local/maxscale/

#logdir=/u01/maxscale/logs/trace/

#datadir=/u01/maxscale/data/

#cachedir=/u01/maxscale/cache/

#piddir=/u01/maxscale/tmp/

# Server definitions # # Set the address of the server to the network # address of a MariaDB server. # [server1] type=server address=192.168.0.74 port=3306 protocol=MariaDBBackend #serv_weight=3 #讀的比重 [server2] type=server address=192.168.0.75 port=3306 protocol=MariaDBBackend #serv_weight=3 #讀的比重 # Monitor for the servers # # This will keep MaxScale aware of the state of the servers. # MariaDB Monitor documentation: # https://mariadb.com/kb/en/mariadb-maxscale-24-mariadb-monitor/ [MariaDB-Monitor] type=monitor module=mariadbmon servers=server1,server2 user=maxscale_monitor password=12345
# 心跳間隔20s
monitor_interval=2000
# 如果有5.1的mysql版本,一定要設置此項,否者slave頻繁報lost_slave

#mysql51_replication=true

  # 當slave斷掉時,是否將所有訪問指向master

  #detect_stale_master = true

# Service definitions # # Service Definition for a read-only service and # a read/write splitting service. # # ReadConnRoute documentation: # https://mariadb.com/kb/en/mariadb-maxscale-24-readconnroute/ #[Read-Only-Service] #type=service #router=readconnroute #servers=server1 #user=myuser #password=mypwd #router_options=slave # ReadWriteSplit documentation: # https://mariadb.com/kb/en/mariadb-maxscale-24-readwritesplit/ [Read-Write-Service] type=service router=readwritesplit servers=server1,server2 user=maxscale_route password=12345
#所有的slave提供select查詢服務
max_slave_connections=100% #master_accept_reads=true #master是否接受讀請求
#auth_all_servers=true  #

#log_auth_warnings=true #身份驗證失敗和警告的日誌記錄,記錄那些試圖連接到MaxScale和來自哪裏

#filters=Hint   #強制select走master的選項

#允許slave落後master多少秒

max_slave_replication_lag=3600



# Listener definitions
for the services # # These listeners represent the ports the # services will listen on. # #[Read-Only-Listener] #type=listener #service=Read-Only-Service #protocol=MariaDBClient #port=4008 [Read-Write-Listener] type=listener service=Read-Write-Service protocol=MariaDBClient port=4006

保存cnf

然後在主從服務器上配置 cnf中的賬戶

1.監視賬戶  這個地方感覺應該用到% 如果用到固定ip的話,客戶端連接會失敗

grant all privileges on *.* to 'maxscale_monitor'@'%' identified by '123456';

2.路由賬戶

grant all privileges on *.* to 'maxscale_route'@'%' identified by '123456';

 

 

然後運行 maxscale.cnf 如果報錯 libgnutls.so.28, 安裝 yum install gnutls

[root@instance-0k9n9mw6 solf]# maxscale -f /etc/maxscale.cnf
maxscale: error while loading shared libraries: libgnutls.so.28: cannot open shared object file: No such file or directory
[root@instance-0k9n9mw6 solf]# yum install gnutls

重新運行 maxscale.cnf  加用戶運行 

maxscale -f /etc/maxscale.cnf -U maxscale

查看是否運行成功



netstat -ntlup |grep maxscale

網上說的有配置 

 

maxadmin -uadmin -pmariadb -P4016  這種的是低版本的,高版本的已經去掉這個了

直接運行  查看服務

maxctrl list services

maxctrl list servers  

 

[root@instance-0k9n9mw6 solf]# maxscale -f /etc/maxscale.cnf
Error: MaxScale cannot be run as root.
Failed to write child process message!
[root@instance-0k9n9mw6 solf]# maxscale -f /etc/maxscale.cnf -U maxscale

 

找到一臺安裝mysql的服務器  然後鏈接maxscale 

 mysql -u maxscale_monitor -p -h 192.168.0.18 -P4006             192.168.0.18是maxscale 服務器,4006是cnf中配置的監聽ip  maxscale_monitor 是在主機上創建的賬號

然後  在maxscale 運行 

 

  maxctrl list services

 maxctrl list servers  

 

查看連接數

[root@instance-0k9n9mw6 solf]# maxctrl list servers
┌─────────┬──────────────┬──────┬─────────────┬─────────────────┬──────┐
│ Server  │ Address      │ Port │ Connections │ State           │ GTID │
├─────────┼──────────────┼──────┼─────────────┼─────────────────┼──────┤
│ server1 │ 192.168.0.7433061           │ Master, Running │      │
├─────────┼──────────────┼──────┼─────────────┼─────────────────┼──────┤
│ server2 │ 192.168.0.7533061           │ Slave, Running  │      │
└─────────┴──────────────┴──────┴─────────────┴─────────────────┴──────┘
[root@instance-0k9n9mw6 solf]# maxctrl list services
┌────────────────────┬────────────────┬─────────────┬───────────────────┬──────────────────┐
│ Service            │ Router         │ Connections │ Total Connections │ Servers          │
├────────────────────┼────────────────┼─────────────┼───────────────────┼──────────────────┤
│ Read-Write-Service │ readwritesplit │ 11                 │ server1, server2 │
└────────────────────┴────────────────┴─────────────┴───────────────────┴──────────────────┘

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章