一、配置過程
我們這裏借鑑網上的文章進行搭建,該網上的帖子裏面也有一些問題,然後我們這裏記錄一下搭建過程以及中間的所有的問題。https://blog.csdn.net/andyvera/article/details/93140839
我們這裏採用的主從配置是採用[mysqld_multi]進行主從配置
1.下載
https://dev.mysql.com/downloads/mysql/
下載下面這個即可
解壓並拷貝到一個位置
tar -zxvf mysql-8.0.20-macos10.15-x86_64.tar.gz
cp -r mysql-8.0.20-macos10.15-x86_64/* /Users/zhouzhenyong/software/mysql-8.0.6
創建連接
// 首先創建路徑:-p是強制創建路徑
sudo mkdir -p /usr/local/mysql
// 創建關聯
sudo ln -s /Users/zhouzhenyong/software/mysql-8.0.6 /usr/local/mysql
2.配置環境變量
sudo vi ~/.bash_profile
添加:export PATH=$PATH:/usr/local/mysql/mysql-8.0.6/bin
source ~/.bash_profile
注意:如果是在fish中,會有異常,記得請先退出fish
3.創建主從mysql
首先創建數據的目錄
mkdir -p /Users/zhouzhenyong/mysql-cluster/master/data
mkdir -p /Users/zhouzhenyong/mysql-cluster/slave1/data
然後執行
mysqld --datadir=/Users/zhouzhenyong/mysql-cluster/master/data --initialize --initialize-insecure
mysqld --datadir=/Users/zhouzhenyong/mysql-cluster/slave1/data --initialize --initialize-insecure
注意:加上–initialize-insecure參數則生成的root用戶沒有密碼,否則mysql初始化時隨機生成一個密碼並輸入到日誌文件中
zhouzhenyong@shizi-2 ~> mysqld --datadir=/Users/zhouzhenyong/mysql-cluster/master/data --initialize --initialize-insecure
2020-05-26T17:58:48.648103Z 0 [System] [MY-013169] [Server] /Users/zhouzhenyong/software/mysql-8.0.6/bin/mysqld (mysqld 8.0.20) initializing of server in progress as process 407
2020-05-26T17:58:48.650377Z 0 [Warning] [MY-010159] [Server] Setting lower_case_table_names=2 because file system for /Users/zhouzhenyong/mysql-cluster/master/data/ is case insensitive
2020-05-26T17:58:48.669113Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
2020-05-26T17:58:48.923259Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
2020-05-26T17:58:49.577968Z 6 [Warning] [MY-010453] [Server] root@localhost is created with an empty password ! Please consider switching off the --initialize-insecure option.
4.配置文件
1.首先查找我們本機的my.cnf路徑
注意:其實mac中是沒有對應的my.cnf路徑的,這個文件也是我們創建的在控制檯輸入命令
mysqld --help --verbose | more
然後在下面就可以看到這樣的一句話
mysqld Ver 8.0.12 for osx10.13 on x86_64 (Homebrew)
Copyright © 2000, 2018, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Starts the MySQL database server.
Usage: mysqld [OPTIONS]
Default options are read from the following files in the given order:
/etc/my.cnf /etc/mysql/my.cnf /usr/local/etc/my.cnf ~/.my.cnf
The following groups are read: mysqld server mysqld-8.0
上面其中紅色部分就是mysql配置文件所在的位置,除了上面的位置之外,也要看下這些位置是否確實有對應的配置,比如我的就在如下
/usr/local/etc/my.cnf
2.創建主從共用的文件
看到我們之前的文件在這個目錄,我們就在這個目錄中創建對應的主從共用文件
sudo touch /usr/local/etc/cluster.conf
下面一些配置基本是參考對應的網上配置,並做了一些修改,讓自己這裏能夠成功運行
[mysqld_multi]
mysqld = /usr/local/mysql/mysql-8.0.6/bin/mysqld
mysqladmin = /usr/local/mysql/mysql-8.0.6/bin/mysqladmin
user = root
password = root1234
[mysqld3307]
server-id=3307
port=3307
#以下爲binlog配置,備災及從機複製
# binlog的日誌文件名字
log-bin=mysql-bin
binlog_format=MIXED
expire_logs_days = 7 #binlog過期清理時間
max_binlog_size = 100m #binlog每個日誌文件大小
binlog_cache_size = 4m #binlog緩存大小
max_binlog_cache_size = 512m #最大binlog緩存大小
log-error=/Users/zhouzhenyong/mysql-cluster/master/mysqld.log
tmpdir=/Users/zhouzhenyong/mysql-cluster/master
slow_query_log=on
slow_query_log_file =/Users/zhouzhenyong/mysql-cluster/master/mysql-slow.log
long_query_time=1
socket=/Users/zhouzhenyong/mysql-cluster/master/mysql_3307.sock
pid-file=/Users/zhouzhenyong/mysql-cluster/master/mysql.pid
basedir=/Users/zhouzhenyong/mysql-cluster/master
datadir=/Users/zhouzhenyong/mysql-cluster/master/data
[mysqld3308]
server-id=3308
port=3308
log-bin=mysql-bin
log-error=/Users/zhouzhenyong/mysql-cluster/slave1/mysqld.log
tmpdir=/Users/zhouzhenyong/mysql-cluster/slave1
slow_query_log=on
slow_query_log_file =/Users/zhouzhenyong/mysql-cluster/slave1/mysql-slow.log
long_query_time=1
socket=/Users/zhouzhenyong/mysql-cluster/slave1/mysql_3308.sock
pid-file=/Users/zhouzhenyong/mysql-cluster/slave1/mysql.pid
basedir=/Users/zhouzhenyong/mysql-cluster/slave1
datadir=/Users/zhouzhenyong/mysql-cluster/slave1/data
read_only=1
[mysqld]
character_set_server=utf8
5.啓動主從實例
mysqld_multi --defaults-file=/usr/local/etc/cluster.conf start
查看狀態
mysqld_multi --defaults-file=/usr/local/etc/cluster.conf report
zhouzhenyong@shizi-2 ~/m/master> mysqld_multi --defaults-file=/usr/local/etc/cluster.conf report
WARNING: Log file disabled. Maybe directory or file isn't writable?
mysqld_multi log file version 2.16; run: 三 5 27 02:11:33 2020
Reporting MySQL servers
MySQL server from group: mysqld3307 is running
MySQL server from group: mysqld3308 is running
6.登錄主機,創建從機賬號和權限
sudo mysql -S /Users/zhouzhenyong/mysql-cluster/master/mysql_3307.sock
注意:這裏的要求輸入密碼,是我們自己機器的密碼,因爲root賬號,mysql我們設置的無密碼登錄
給從庫授權獲取二進制文件權限
create user ‘slave’@’%’ identified by ‘slave1234’;
grant replication slave on . to ‘slave’@’%’;
flush privileges;
7.查看主庫節點
show master status;
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 | 858 | | | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
8.配置從庫
登錄
sudo mysql -S /Users/zhouzhenyong/mysql-cluster/slave1/mysql_3308.sock
關閉從庫
stop slave;
配置從庫同步(同步的位置)
change master to master_host=‘127.0.0.1’,master_port=3307,master_user=‘slave’,master_password=‘slave1234’,master_log_file=‘mysql-bin.000001’,master_log_pos=858;
開啓從庫
start slave;
檢查從庫狀態
show slave status\G;
mysql> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 127.0.0.1
Master_User: slave
Master_Port: 3307
Connect_Retry: 60
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 4946
Relay_Log_File: shizi-2-relay-bin.000002
Relay_Log_Pos: 1059
Relay_Master_Log_File: mysql-bin.000001
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: 4946
Relay_Log_Space: 1270
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: 3307
Master_UUID: 8d40fc5c-9f7a-11ea-9e31-4cae848a2a31
Master_Info_File: mysql.slave_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:
Master_public_key_path:
Get_master_public_key: 0
Network_Namespace:
1 row in set (0.00 sec)
ERROR:
No query specified
從庫狀態爲如下就可以了
mysql> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
如果不是,則可能有對應的一些問題
9.從庫創建只讀賬號
create user ‘reader’@’%’ identified by ‘123456’;
grant select on . to ‘reader’@’%’;
flush privileges;
二、使用
在主庫中添加表,以及添加對應的數據,在從庫中都會有數據了
三、問題:
1.啓動失敗
zhouzhenyong@shizi-2 /u/l/etc> mysqld_multi --defaults-file=/usr/local/etc/cluster.conf start
WARNING: Log file disabled. Maybe directory or file isn't writable?
mysqld_multi log file version 2.16; run: 三 5 27 01:26:31 2020
Starting MySQL servers
Installing new database in /Users/zhouzhenyong/mysql-cluster/master/data
FATAL ERROR: Tried to start mysqld under group [mysqld3307],
but no data directory was found or could be created.
data directory used: /Users/zhouzhenyong/mysql-cluster/master/data
這裏執行失敗,是由於自己本機還有一個mysql實例,然後看了下
which mysql
發現並不是自己配置的這個mysql,而是brew 下載的[email protected],我這裏將這個brew 下載的刪除了
1.刪除流程:
brew uninstall [email protected]
brew remove [email protected]
2.刪除完畢後又遇到mysqld不識別的情況
通過路徑,進入到對應的目錄中
cd /usr/local/mysql/mysql-8.0.6/bin
open .
然後點擊鼠標,點擊對應的mysqld,然後會彈出告警框,這裏點擊打開,然後再重新輸入命令就可以識別了由於圖片沒有截圖,參考網上的一個,點擊打開即可
2.啓動失敗
zhouzhenyong@shizi-2 ~/m/master> mysqld_multi --defaults-file=/usr/local/etc/cluster.conf start
WARNING: Log file disabled. Maybe directory or file isn't writable?
mysqld_multi log file version 2.16; run: 三 5 27 02:06:48 2020
Starting MySQL servers
FATAL ERROR: Tried to start mysqld under group [mysqld3307], but no mysqld binary was found.
Please add "mysqld=..." in group [mysqld_multi], or add it to group [mysqld3307] separately.
這裏是要求將其中的mysqld配置上,上面其實是配置上了,在參考網上配置中mysqld是沒有配置的,這裏放開了mysqld的配置爲,但是網上的配置是這樣,是mysqld_safe,但是這個mysqld_safe還是有問題的,啓動會失敗
mysqld = /usr/local/mysql/bin/mysqld_safe
最後修改爲如下就沒問題了,也就是上面自己的這個配置
mysqld = /usr/local/mysql/mysql-8.0.6/bin/mysqld
參考:
mysql官方對於主從的支持
https://dev.mysql.com/doc/connector-j/5.1/en/connector-j-master-slave-replication-connection.html
https://blog.csdn.net/qq_21153619/article/details/81529880
主要借鑑
https://blog.csdn.net/andyvera/article/details/93140839
https://www.cnblogs.com/kylinlin/p/5258719.html
https://blog.csdn.net/weixin_43184819/article/details/84000936