Mac 下 MySQL 主从复制搭建 [ 未完待续 ]

MySQL 集群搭建

https://dev.mysql.com/downloads/mysql/ 下载 8.0

Compressed TAR Archive,是源码,必须用源码方式安装。 ---- 这个是源码,需要自己编译的,也有编译好,但不是安装包的

DMG 包在图形化界面下按提示安装

image-20191113095750798

下载 TAR 版本. 解压后

image-20191113100311047

我打算建立 3 个实例, 1 个 master , 2 个 slave. 第一个 slave 放在 : /Users/yuyang/Downloads/JavaWeb/Mysql_slave/mysql_package1目录执行初始化 :

mysqld --datadir=/Users/yuyang/Downloads/JavaWeb/Mysql_slave/mysql_package1/data --initialize --initialize-insecure

YYDCYY-HUAWEI-Matebook:~ yuyang$ mysqld --datadir=/Users/yuyang/Downloads/JavaWeb/Mysql_slave/mysql_package1/data  --initialize --initialize-insecure
2019-11-13T02:11:22.808367Z 0 [System] [MY-013169] [Server] /usr/local/Cellar/mysql/8.0.17/bin/mysqld (mysqld 8.0.17) initializing of server in progress as process 52257
2019-11-13T02:11:22.814665Z 0 [Warning] [MY-010159] [Server] Setting lower_case_table_names=2 because file system for /Users/yuyang/Downloads/JavaWeb/Mysql_slave/mysql_package1/data/ is case insensitive
2019-11-13T02:11:23.977127Z 5 [Warning] [MY-010453] [Server] root@localhost is created with an empty password ! Please consider switching off the --initialize-insecure option.
2019-11-13T02:11:24.619059Z 0 [System] [MY-013170] [Server] /usr/local/Cellar/mysql/8.0.17/bin/mysqld (mysqld 8.0.17) initializing of server has completed
YYDCYY-HUAWEI-Matebook:~ yuyang$ 

提示初始化成功. 在执行第二个 slave 初始化 [ 记得输如正确地址路径 ]

mysqld --datadir=/Users/yuyang/Downloads/JavaWeb/Mysql_master/data --initialize --initialize-insecure

他教的是一个一个配置, 我看的博客是说把所有的配置写一起, 可以统一执行

配置文件同一启动有点麻烦. 先试试一个一个启动, ok 后再配置文件方式启动?

mysqld_multi 解析

http://blog.itpub.net/29773961/viewspace-1816397/

$ mysqld_multi --help
mysqld_multi version 2.16 by Jani Tolonen

Description:
mysqld_multi can be used to start, reload, or stop any number of separate
mysqld processes running in different TCP/IP ports and UNIX sockets.

mysqld_multi can read group [mysqld_multi] from my.cnf file. You may
want to put options mysqld=... and mysqladmin=... there.  Since
version 2.10 these options can also be given under groups [mysqld#],
which gives more control over different versions.  One can have the
default mysqld and mysqladmin under group [mysqld_multi], but this is
not mandatory. Please note that if mysqld or mysqladmin is missing
from both [mysqld_multi] and [mysqld#], a group that is tried to be
used, mysqld_multi will abort with an error.

mysqld_multi will search for groups named [mysqld#] from my.cnf (or
the given --defaults-extra-file=...), where '#' can be any positive 
integer starting from 1. These groups should be the same as the regular
[mysqld] group, but with those port, socket and any other options
that are to be used with each separate mysqld process. The number
in the group name has another function; it can be used for starting,
reloading, stopping, or reporting any specific mysqld server.

Usage: mysqld_multi [OPTIONS] {start|reload|stop|report} [GNR,GNR,GNR...]
or     mysqld_multi [OPTIONS] {start|reload|stop|report} [GNR-GNR,GNR,GNR-GNR,...]

The GNR means the group number. You can start, reload, stop or report any GNR,
or several of them at the same time. (See --example) The GNRs list can
be comma separated or a dash combined. The latter means that all the
GNRs between GNR1-GNR2 will be affected. Without GNR argument all the
groups found will either be started, reloaded, stopped, or reported. Note that
syntax for specifying GNRs must appear without spaces.

Options:

These options must be given before any others:
--no-defaults      Do not read any defaults file
--defaults-file=...  Read only this configuration file, do not read the
                   standard system-wide and user-specific files
--defaults-extra-file=...  Read this configuration file in addition to the
                   standard system-wide and user-specific files
Using:  

--example          Give an example of a config file with extra information.
--help             Print this help and exit.
--log=...          Log file. Full path to and the name for the log file. NOTE:
                   If the file exists, everything will be appended.
                   Using: 
--mysqladmin=...   mysqladmin binary to be used for a server shutdown.
                   Since version 2.10 this can be given within groups [mysqld#]
                   Using: 
--mysqld=...       mysqld binary to be used. Note that you can give mysqld_safe
                   to this option also. The options are passed to mysqld. Just
                   make sure you have mysqld in your PATH or fix mysqld_safe.
                   Using: 
                   Please note: Since mysqld_multi version 2.3 you can also
                   give this option inside groups [mysqld#] in ~/.my.cnf,
                   where '#' stands for an integer (number) of the group in
                   question. This will be recognised as a special option and
                   will not be passed to the mysqld. This will allow one to
                   start different mysqld versions with mysqld_multi.
--no-log           Print to stdout instead of the log file. By default the log
                   file is turned on.
--password=...     Password for mysqladmin user.
--silent           Disable warnings.
--tcp-ip           Connect to the MySQL server(s) via the TCP/IP port instead
                   of the UNIX socket. This affects stopping and reporting.
                   If a socket file is missing, the server may still be
                   running, but can be accessed only via the TCP/IP port.
                   By default connecting is done via the UNIX socket.
--user=...         mysqladmin user. Using: root
--verbose          Be more verbose.
--version          Print the version number and exit.

简要说明 :

mysqld_multi可以从my.cnf文件中读取组[mysqld_multi]。你可以想要在其中放置选项mysqld = ...和mysqladmin =...。以来在版本2.10中,也可以在[mysqld#]组下提供这些选项,
从而可以更好地控制不同版本。一个可以拥有组[mysqld_multi]下的默认mysqld和mysqladmin,但这是不是强制性的。请注意,如果缺少mysqld或mysqladmin来自[mysqld_multi]和[mysqld#]的一个组使用时,mysqld_multi将中止并显示错误。

mysqld_multi --defaults-file=/etc/cluster.cnf start

然后去数据库执行 :

 show master status;
 
 change master to 
 master_host='127.0.0.1', 
 master_port=3306,
 master_user='backup',
 master_password='123123'

start slave;

show slave status;

结果 :

image-20191113123844972

白框内正确的情况应该是 两个 yes, 现在 slave_IO_Running 是 no.

Fatal error: The slave I/O thread stops because master and slave have equal MySQL server ids; these ids must be different for replication to work (or the --replicate-same-server-id option must be used on slave but this does not always make sense; please check the manual before using it).

致命错误:因为主服务器和从服务器具有相等的MySQL服务器ID,所以从服务器I / O线程停止。 这些ID必须不同才能使复制正常工作(或必须在从属服务器上使用--replicate-same-server-id选项,但这并不总是有意义;请在使用前查阅手册)。

说明从 MySQL auto.cnf uuid 重复 [ 因为是同一个 MySQL 压缩包解压复制的多份, 随便改成不一样的. ]

路径 : xxxx / mysql 名 / data/auto.cnf

image-20191113124052344
[auto]
server-uuid=e3eefcfc-05ba-11ea-94c2-9688e7ece9cc

我的是这个值, 同一吧最后一位 c 改为 a / b 吧

MySQL master 没有 data 目录, 忘初始化了

image-20191113124950878

没有加 --initialize --initialize-insecure 执行

最后一句提示 失败.

YYDCYY-HUAWEI-Matebook:~ yuyang$ mysqld --datadir=/usr/local/Cellar/mysql/8.0.17/data
2019-11-13T04:51:38.616453Z 0 [System] [MY-010116] [Server] /usr/local/Cellar/mysql/8.0.17/bin/mysqld (mysqld 8.0.17) starting as process 52763
2019-11-13T04:51:38.619670Z 0 [Warning] [MY-010091] [Server] Can't create test file /usr/local/Cellar/mysql/8.0.17/data/mysqld_tmp_file_case_insensitive_test.lower-test
2019-11-13T04:51:38.619679Z 0 [Warning] [MY-010159] [Server] Setting lower_case_table_names=2 because file system for /usr/local/Cellar/mysql/8.0.17/data/ is case insensitive
2019-11-13T04:51:38.619758Z 0 [ERROR] [MY-013276] [Server] Failed to set datadir to '/usr/local/Cellar/mysql/8.0.17/data/' (OS errno: 2 - No such file or directory)
2019-11-13T04:51:38.619809Z 0 [ERROR] [MY-010119] [Server] Aborting
2019-11-13T04:51:38.619949Z 0 [System] [MY-010910] [Server] /usr/local/Cellar/mysql/8.0.17/bin/mysqld: Shutdown complete (mysqld 8.0.17)  Homebrew.

$ mysqld --datadir=/usr/local/Cellar/mysql/8.0.17/data --initialize --initialize-insecure

提示初始化成功

YYDCYY-HUAWEI-Matebook:~ yuyang$ mysqld --datadir=/usr/local/Cellar/mysql/8.0.17/data --initialize --initialize-insecure
2019-11-13T04:58:01.013346Z 0 [System] [MY-013169] [Server] /usr/local/Cellar/mysql/8.0.17/bin/mysqld (mysqld 8.0.17) initializing of server in progress as process 52779
2019-11-13T04:58:01.015785Z 0 [Warning] [MY-010159] [Server] Setting lower_case_table_names=2 because file system for /usr/local/Cellar/mysql/8.0.17/data/ is case insensitive
2019-11-13T04:58:01.697040Z 5 [Warning] [MY-010453] [Server] root@localhost is created with an empty password ! Please consider switching off the --initialize-insecure option.
2019-11-13T04:58:02.295472Z 0 [System] [MY-013170] [Server] /usr/local/Cellar/mysql/8.0.17/bin/mysqld (mysqld 8.0.17) initializing of server has completed
YYDCYY-HUAWEI-Matebook:~ yuyang$ 
image-20191113130051490

mysql -u root mysql

不存在这个目录 /usr/local/mysql

建立一个软链接吧.

sudo ln -s /用户/yuyang/下载/JavaWeb/Mysql_slave /usr/local/mysql

软链接变成这样了? image-20191113135932828

1)mac版本的mysql没有my.cnf文件,创建一个主从共用的配置文件,文件随意命名, 执行以下命令,sudo touch /etc/cluster.cnf [ 这个位置原本不存在, 自己建一个 ]

image-20191113151633149

[mysqld_multi]
mysqld     = /usr/local/Cellar/mysql/8.0.17/bin/mysqld_safe
mysqladmin = /usr/local/Cellar/mysql/8.0.17/bin/mysqladmin
user       = root

log        = /usr/local/Cellar/mysql/8.0.17/data/mysql_multi.log



[mysqld3306] ##
basedir    = /usr/local/Cellar/mysql/8.0.17
datadir    = /usr/local/Cellar/mysql/8.0.17/data/3306
socket     = /usr/local/Cellar/mysql/8.0.17/data/3306/mysql.sock
port       = 3306
pid-file   = /usr/local/Cellar/mysql/8.0.17/data/3306/mysql.pid
user       = mysql
server_id  = 1
log_bin    = mysql-bin

[mysqld3307] ##
basedir    = /用户/yuyang/下载/JavaWeb/Mysql_slave/slave1
datadir    = /用户/yuyang/下载/JavaWeb/Mysql_slave/slave1/data/3307
socket     = /用户/yuyang/下载/JavaWeb/Mysql_slave/slave1/data/3307/mysql.sock
port       = 3307
pid-file   = /用户/yuyang/下载/JavaWeb/Mysql_slave/slave1/data/3307/mysql.pid
user       = mysql
server_id  = 2

[mysqld3308] ##
basedir    = /用户/yuyang/下载/JavaWeb/Mysql_slave/slave2
datadir    = /用户/yuyang/下载/JavaWeb/Mysql_slave/slave2/data/3308
socket     = /用户/yuyang/下载/JavaWeb/Mysql_slave/slave2/data/3308/mysql.sock
port       = 3308
pid-file   = /用户/yuyang/下载/JavaWeb/Mysql_slave/slave2/data/3307/mysql.pid
user       = mysql
server_id  = 3

[mysqld_multi]
#mysqld    = /usr/local/mysql/bin/mysqld_safe
mysqladmin = /usr/local/Cellar/mysql/8.0.17/bin/mysqladmin
user       = root
#password  = root
log        = /usr/local/Cellar/mysql/8.0.17/data/mysql_multi.log



[mysqld3307]
server-id=3307
port=3307

#以下为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=/Library/Java/mysql-cluster/master/mysqld.log
tmpdir=/Library/Java/mysql-cluster/master
slow_query_log=on
slow_query_log_file =/用户/yuyang/下载/JavaWeb/Mysql_slave/slave1/mysql-slow.log
long_query_time=1

socket=/用户/yuyang/下载/JavaWeb/Mysql_slave/slave1/mysql_3307.sock
pid-file=/用户/yuyang/下载/JavaWeb/Mysql_slave/slave1/mysql.pid

basedir=/用户/yuyang/下载/JavaWeb/Mysql_slave/slave1
datadir=/用户/yuyang/下载/JavaWeb/Mysql_slave/slave1/data

[mysqld3308]
server-id=3308
port=3308
log-bin=mysql-bin

log-error=/用户/yuyang/下载/JavaWeb/Mysql_slave/slave2/mysqld.log
tmpdir=/用户/yuyang/下载/JavaWeb/Mysql_slave/slave2

slow_query_log=on
slow_query_log_file =/用户/yuyang/下载/JavaWeb/Mysql_slave/slave2/mysql-slow.log
long_query_time=1

socket=/用户/yuyang/下载/JavaWeb/Mysql_slave/slave2/mysql_3308.sock
pid-file=/用户/yuyang/下载/JavaWeb/Mysql_slave/slave2/mysql.pid


basedir=/用户/yuyang/下载/JavaWeb/Mysql_slave/slave2
datadir=/用户/yuyang/下载/JavaWeb/Mysql_slave/slave2/data

read_only=1


[mysqld]
character_set_server=utf8

参考博客改几个版本了, 还是主从复制不对. 不知道为啥. 参考资料大多是 Windows , linux . 主要是没讲路径问题, 数据库密码问题. [ 都是刚刚下载的库, 没有初始化, 密码是啥 ? 不就是默认密码 root , root 嘛 ,但是我登录不进去呀 ]

参考资料 :

https://www.cnblogs.com/wkzhao/p/10293127.html mac下安装多个mysql实例

https://blog.csdn.net/u013897685/article/details/79615587 mysqld_multi启动mysqld_multi report报错

https://blog.csdn.net/u014674448/article/details/51834918 MySQL-5.6.27两种方式部署多实例以及mysqld_multi脚本BUG处理

https://blog.csdn.net/u014674448/article/details/51834918 这篇文章讲linux 下主从复制还算清楚.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值