1. 程式人生 > 實用技巧 >Mac MySQL 8.0 (免安裝版) 主從叢集搭建

Mac MySQL 8.0 (免安裝版) 主從叢集搭建

一、下載解壓包

開啟 MySQL 官網地址:https://dev.mysql.com/downloads/mysql/ ,選擇面安裝版本。

二、解壓檔案

下載到合適資料夾,解壓壓縮包。

解壓 mysql-8.0.22-macos10.15-x86_64.tar.gz 三份,分別命名資料夾為 masterslave1slave2

三、編輯資料庫配置檔案

master 資料夾下的 support-files 資料夾下面新建 my.conf 配置檔案

[client]
  default-character-set=utf8
  #password   = your_password
  port        = 3306
  #修改的socket檔案的位置,預設是走的/tmp下的mysql.sock會有衝突
  #這是報錯  Another process with pid 77346 is using unix socket file.
  #客戶端也需要這個和服務端的一致
  socket      = /Users/zeyangg/SynologyDrive/ee/mysql/master/mysql.sock
[mysqld]
  event_scheduler=ON
  character-set-server=utf8
  init_connect='SET NAMES utf8
  port        = 3306
  mysqlx_port = 33060
  bind-address=127.0.0.1
  #修改的socket檔案的位置,預設是走的/tmp下的mysql.sock會有衝突
  socket      = /Users/zeyangg/SynologyDrive/ee/mysql/master/mysql.sock
  mysqlx_socket= /Users/zeyangg/SynologyDrive/ee/mysql/master/mysqlx.sock
  skip-external-locking
  key_buffer_size = 16M
  max_allowed_packet = 1M
  table_open_cache = 64
  sort_buffer_size = 512K
  net_buffer_length = 8K
  read_buffer_size = 256K
  read_rnd_buffer_size = 512K
  myisam_sort_buffer_size = 8M
  character-set-server=utf8
  init_connect='SET NAMES utf8'
  #修改mysql的主目錄
  basedir=/Users/zeyangg/SynologyDrive/ee/mysql/master
  #新增data檔案的目錄,儲存各種資料和日誌
  datadir=/Users/zeyangg/SynologyDrive/ee/mysql/master/data
  
  log-bin=mysql-bin
  binlog_format=mixed
  server-id   = 1
  sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
  
[mysqldump]
  quick
  max_allowed_packet = 16M

[mysql]
  no-auto-rehash
  # Remove the next comment character if you are not familiar with SQL
  #safe-updates
  default-character-set=utf8

[myisamchk]
  key_buffer_size = 20M
  sort_buffer_size = 20M
  read_buffer = 2M
  write_buffer = 2M

[mysqlhotcopy]
  interactive-timeout

slave1 資料夾下的 support-files 資料夾下面新建 my.conf 配置檔案

[client]
  default-character-set=utf8
  #password   = your_password
  #修改埠號不要和主庫一致
  port        = 3316
  #修改的socket檔案的位置,預設是走的/tmp下的mysql.sock會有衝突
  #這是報錯  Another process with pid 77346 is using unix socket file.
  #客戶端也需要這個和服務端的一致
  socket      = /Users/zeyangg/SynologyDrive/ee/mysql/slave1/mysql.sock
[mysqld]
  event_scheduler=ON
  character-set-server=utf8
  init_connect='SET NAMES utf8
  #修改埠號
  port        = 3316
  mysqlx_port = 33061
  bind-address=127.0.0.1
  #修改的socket檔案的位置,預設是走的/tmp下的mysql.sock會有衝突
  socket      = /Users/zeyangg/SynologyDrive/ee/mysql/slave1/mysql.sock
  mysqlx_socket= /Users/zeyangg/SynologyDrive/ee/mysql/slave1/mysqlx.sock
  skip-external-locking
  key_buffer_size = 16M
  max_allowed_packet = 1M
  table_open_cache = 64
  sort_buffer_size = 512K
  net_buffer_length = 8K
  read_buffer_size = 256K
  read_rnd_buffer_size = 512K
  myisam_sort_buffer_size = 8M
  character-set-server=utf8
  init_connect='SET NAMES utf8'
  #修改mysql的主目錄
  basedir=/Users/zeyangg/SynologyDrive/ee/mysql/slave1
  #新增data檔案的目錄,儲存各種資料和日誌
  datadir=/Users/zeyangg/SynologyDrive/ee/mysql/slave1/data
  
  log-bin=mysql-bin
  binlog_format=mixed
  #不要和主庫一致
  server-id   = 2
  sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
  
[mysqldump]
  quick
  max_allowed_packet = 16M

[mysql]
  no-auto-rehash
  # Remove the next comment character if you are not familiar with SQL
  #safe-updates
  default-character-set=utf8

[myisamchk]
  key_buffer_size = 20M
  sort_buffer_size = 20M
  read_buffer = 2M
  write_buffer = 2M

[mysqlhotcopy]
  interactive-timeout

slave2 資料夾下的 support-files 資料夾下面新建 my.conf 配置檔案

[client]
  default-character-set=utf8
  #password   = your_password
  #修改埠號不要和主庫一致
  port        = 3326
  #修改的socket檔案的位置,預設是走的/tmp下的mysql.sock會有衝突
  #這是報錯  Another process with pid 77346 is using unix socket file.
  #客戶端也需要這個和服務端的一致
  socket      = /Users/zeyangg/SynologyDrive/ee/mysql/slave2/mysql.sock
[mysqld]
  event_scheduler=ON
  character-set-server=utf8
  init_connect='SET NAMES utf8
  #修改埠號
  port        = 3326
  mysqlx_port = 33062
  #修改的socket檔案的位置,預設是走的/tmp下的mysql.sock會有衝突
  socket      = /Users/zeyangg/SynologyDrive/ee/mysql/slave2/mysql.sock
  mysqlx_socket= /Users/zeyangg/SynologyDrive/ee/mysql/slave2/mysqlx.sock
  skip-external-locking
  key_buffer_size = 16M
  max_allowed_packet = 1M
  table_open_cache = 64
  sort_buffer_size = 512K
  net_buffer_length = 8K
  read_buffer_size = 256K
  read_rnd_buffer_size = 512K
  myisam_sort_buffer_size = 8M
  character-set-server=utf8
  init_connect='SET NAMES utf8'
  #修改mysql的主目錄
  basedir=/Users/zeyangg/SynologyDrive/ee/mysql/slave2
  #新增data檔案的目錄,儲存各種資料和日誌
  datadir=/Users/zeyangg/SynologyDrive/ee/mysql/slave2/data
  
  log-bin=mysql-bin
  binlog_format=mixed
  #不要和主庫一致
  server-id   = 3
  sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
  
[mysqldump]
  quick
  max_allowed_packet = 16M

[mysql]
  no-auto-rehash
  # Remove the next comment character if you are not familiar with SQL
  #safe-updates
  default-character-set=utf8

[myisamchk]
  key_buffer_size = 20M
  sort_buffer_size = 20M
  read_buffer = 2M
  write_buffer = 2M

[mysqlhotcopy]
  interactive-timeout

四、資料庫初始化

使用命令列進入對應目錄,執行以下語句

# 進入 master 目錄
cd /Users/zeyangg/SynologyDrive/ee/mysql/master/bin

# 執行 master 初始化
./mysqld --defaults-file=/Users/zeyangg/SynologyDrive/ee/mysql/master/support-files/my.cnf --initialize-insecure

# 進入 slave1 目錄
cd /Users/zeyangg/SynologyDrive/ee/mysql/slave1/bin

# 執行 slave1 初始化
./mysqld --defaults-file=/Users/zeyangg/SynologyDrive/ee/mysql/slave1/support-files/my.cnf --initialize-insecure

# 進入 slave2 目錄
cd /Users/zeyangg/SynologyDrive/ee/mysql/slave2/bin

# 執行 slave2 初始化
./mysqld --defaults-file=/Users/zeyangg/SynologyDrive/ee/mysql/slave2/support-files/my.cnf --initialize-insecure

執行過程中可能出現提示 mysqld 不安全

選擇 Cancel,然後進入系統安全裡面,選擇 allow anyway,然後再次執行上述命令,選擇 open 就可以繼續運行了。

五、啟動主資料庫

分別啟動對應資料庫

# 進入 master 目錄
cd /Users/zeyangg/SynologyDrive/ee/mysql/master/bin

# 啟動 master
./mysqld --defaults-file=/Users/zeyangg/SynologyDrive/ee/mysql/master/support-files/my.cnf 

# 進入 slave1 目錄
cd /Users/zeyangg/SynologyDrive/ee/mysql/slave1/bin

# 啟動 slave1 
./mysqld --defaults-file=/Users/zeyangg/SynologyDrive/ee/mysql/slave1/support-files/my.cnf 

# 進入 slave2 目錄
cd /Users/zeyangg/SynologyDrive/ee/mysql/slave2/bin

# 啟動 slave2 
./mysqld --defaults-file=/Users/zeyangg/SynologyDrive/ee/mysql/slave2/support-files/my.cnf 

啟動 master 截圖如下

六、建立使用者

# 進入 master 目錄
cd /Users/zeyangg/SynologyDrive/ee/mysql/master/bin

# 進入主 master 庫
./mysql -h 127.0.0.1 -uroot -P3306

# 建立使用者(此處使用隨機生成密碼,需要記住密碼)
# create user 'holddie'@'%' identified by '123456';
create user 'holddie'@'%' IDENTIFIED BY RANDOM PASSWORD;

# 授權遠端同步
grant replication slave on *.* to 'holddie'@'%';

# 儲存重新整理
flush privileges;

# 檢視主庫的狀態
show master status\G;

檢視主庫的狀態

從庫連線主庫

# 進入 slave1 從庫的狀態
cd /Users/zeyangg/SynologyDrive/ee/mysql/slave1/bin

# 在從庫使用剛才主庫建立的賬號,連線一次,看否賬號可用(使用剛才建立賬號對應的密碼)
./mysql -h 127.0.0.1 -uholddie -P3306

# 如果登入沒有問題則 quit 退出,進入 slave1 自己資料庫
./mysql -h 127.0.0.1 -uroot -P3316

# 使用命令 (注意此處替換自己建立資料庫賬號,以及修改對應 master_log_file 名稱以及偏移位置)
change master to master_host='127.0.0.1',master_port=3306,master_user='holddie',master_password='VrQ>-YtFPGw&-sJ,hI2Q', master_log_file='mysql-bin.000004',master_log_pos=156;

# 啟動 slave1 同步
start slave;

# 檢視同步狀態
show salve status;

注意觀察 Slave_IO_Running 和 Slave_SQL_Running 的狀態值,只有都為 Yes 的時候才表明同步 ok,同理 Slave2 也是相同的操作步驟。

檢視同步狀態

# 進入 slave1 目錄
cd /Users/zeyangg/SynologyDrive/ee/mysql/slave1/bin

./mysql -h 127.0.0.1 -uroot -P3316

show salve status;

注意觀察 Slave_IO_Running 和 Slave_SQL_Running 的狀態值,只有都為 Yes 的時候才表明同步 ok,同理 Slave2 也是相同的操作步驟。

此時我們使用 DataGrip 連線資料庫,在 master 修改資料,然後在從庫檢視,是否資料同步。