1、下载mysql资源包,并解压
2、解压tar包,获取解压后的文件
然后再解压mysql-8.0.13-macos10.14-x86_64.tar.gz
3、在support-files中添加配置文件 my.conf
my.conf 文件
[client]
default-character-set=utf8
password = root
port = 3307
#修改的socket文件的位置,默认是走的/tmp下的mysql.sock会有冲突
#这是报错 Another process with pid 77346 is using unix socket file.
#客户端也需要这个和服务端的一致,配置好路径,这个在初始化mysql,会自动生成
socket = /Users/scy/d_software/dev/mysql_cluster/master/mysql.sock
[mysqld]
event_scheduler=ON
character-set-server=utf8
init_connect='SET NAMES utf8
# 不重复就行
port = 3307
# 不重复就行
mysqlx_port = 33070
bind-address=127.0.0.1
#修改的socket文件的位置,默认是走的/tmp下的mysql.sock会有冲突
#配置好路径,这个在初始化mysql,会自动生成
socket = /Users/scy/d_software/dev/mysql_cluster/master/mysql.sock
mysqlx_socket= /Users/scy/d_software/dev/mysql_cluster/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/scy/d_software/dev/mysql_cluster/master
#添加data文件的目录,存储各种数据和日志,配置好路径,初始化数据库,自动生成
datadir=/Users/scy/d_software/dev/mysql_cluster/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
4、初始化数据库,进入bin目录,执行命令
./mysqld --defaults-file=/Users/scy/d_software/dev/mysql_cluster/master/support-files/my.conf --initialize-insecure
执行结果
5、启动mysql服务,进入bin目录,执行命令
./mysqld --defaults-file=/Users/scy/d_software/dev/mysql_cluster/master/support-files/my.conf
#后台启动
nohup ./mysqld --defaults-file=/Users/scy/d_software/dev/mysql_cluster/master/support-files/my.conf &
执行结果
7、如果想停掉mysql服务(这里我没有找到好的方式,应该是启动后,在后台运行,不能是停留在窗口除,所以停掉服务也没有好的方式,只能是杀掉进程。有大神帮忙补充一下,谢谢)
1、根据端口号查询mysql服务的pid
lsof -i :3307
2、杀掉这个进程
kill -9 pid
8、登录msyql
./mysql -h 127.0.0.1 -uroot -P3307 -p
启动mysql后,第一次登录,查看mysql.user这个表中,可以看到root用户是没有密码的,可以给root用户添加密码
# 修改root用户密码
ALTER USER 'username'@'localhost' IDENTIFIED BY 'new_password';
# 这里发现一个现象,如果是alter用户,如果这个用户的host字段是localhost,那么修改的时候必须是localhost,不能写成%,必须要和已经存在的host保持一致
# 刷新权限
FLUSH PRIVILEGES;
9、创建用户(mysql8后的要分开语句写,不能像版本5的那样,可以参考:https://blog.csdn.net/d_eng_/article/details/126922222)
create user 'test4'@'%' identified by 'test4';
# 这里也发现一个现象,创建完用户后,网上很多地方说,必须要授予远程登录,才可以使用客户端工具连接,比如Navicat,但是我不知道是否是mysql在我本地的安装,不需要授权也可以通过Navicat直接连上。
# 如果需要授权,需要执行下面的命令
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' WITH GRANT OPTION;
FLUSH PRIVILEGES;
10、配置从机:将第2点解压的的文件夹复制两分,分别命名slave1、slave2。两台从机
11、配置slave1的my.conf文件
slave1中的my.conf文件
[client]
default-character-set=utf8
password = root
port = 3308
#修改的socket文件的位置,默认是走的/tmp下的mysql.sock会有冲突
#这是报错 Another process with pid 77346 is using unix socket file.
#客户端也需要这个和服务端的一致
socket = /Users/scy/d_software/dev/mysql_cluster/slave1/mysql.sock
[mysqld]
event_scheduler=ON
character-set-server=utf8
init_connect='SET NAMES utf8
port = 3308
mysqlx_port = 33080
bind-address=127.0.0.1
#修改的socket文件的位置,默认是走的/tmp下的mysql.sock会有冲突
socket = /Users/scy/d_software/dev/mysql_cluster/slave1/mysql.sock
mysqlx_socket= /Users/scy/d_software/dev/mysql_cluster/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/scy/d_software/dev/mysql_cluster/slave1
#添加data文件的目录,存储各种数据和日志
datadir=/Users/scy/d_software/dev/mysql_cluster/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
12、slave2中my.conf的文件
[client]
default-character-set=utf8
password = root
port = 3309
#修改的socket文件的位置,默认是走的/tmp下的mysql.sock会有冲突
#这是报错 Another process with pid 77346 is using unix socket file.
#客户端也需要这个和服务端的一致
socket = /Users/scy/d_software/dev/mysql_cluster/slave2/mysql.sock
[mysqld]
event_scheduler=ON
character-set-server=utf8
init_connect='SET NAMES utf8
port = 3309
mysqlx_port = 33090
bind-address=127.0.0.1
#修改的socket文件的位置,默认是走的/tmp下的mysql.sock会有冲突
socket = /Users/scy/d_software/dev/mysql_cluster/slave2/mysql.sock
mysqlx_socket= /Users/scy/d_software/dev/mysql_cluster/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/scy/d_software/dev/mysql_cluster/slave2
#添加data文件的目录,存储各种数据和日志
datadir=/Users/scy/d_software/dev/mysql_cluster/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
13 设置主从配置
1、设置账户远程授权。这里我使用的是root权限,并没有执行下面的命令(如果没有主从复制,可以执行下面的命令)
# 授权远程同步
grant replication slave on *.* to 'holddie'@'%';
# 保存刷新
flush privileges;
2、查看主机状态
show master status;
其中mysql-bin.000006是mysql二进制文件,Postition是偏移量
3、设置从机
# 指定主机ip、二进制文件、偏移量位置
mysql>change master to master_host='127.0.0.1',master_port=3307,master_user='root',master_password='root', master_log_file='mysql-bin.000006',master_log_pos=9655;
# 启动从机
mysql>start slave;
# 查看从机状态
mysql> show slave status;
查看从机状态
14、slave2的配置和slave1的配置方式一样。
15、如果从机宕机了,并且重新启动了从机。如果从机上的库和表与主机的不一致,将会导致主从复制失效。从机一直处于失败状态。此时需要将从机停止(stop slave;)然后获取主机的二进制文件和偏移量,从新设置主从同步。除此之外,还必须手动将从机的表和库与主机设置的一样(这里是demo演示,不太清楚是否需要人工来处理这个从表和主表上的库、表一致,如果有其它的自动化方式,还请大佬指出)
参考:https://www.cnblogs.com/holddie/p/14079223.html