赞
踩
因为项目开发需要,多个项目使用不同的mysql版本,不同的mysql版本又有不同的特性,所以需要在本机安装多个版本的mysql使用。
废话不多说直接开始正文
# 安装 mysql5.7 和 mysql8.0
brew install mysql@5.7
brew install mysql@8.0
# 复制默认的配置文件创建各自版本的独立配置文件
cp /usr/local/etc/my.cnf /usr/local/etc/my57.cnf
cp /usr/local/etc/my.cnf /usr/local/etc/my80.cnf
my57.cnf 文件内容
# Default Homebrew MySQL server config
[mysqld]
# Only allow connections from localhost
bind-address = 0.0.0.0
default_authentication_plugin = mysql_native_password
sql_mode = NO_ENGINE_SUBSTITUTION
port = 33065
# pid文件路径
pid-file = /usr/local/var/run/mysqld57.pid
# sock文件路径 nginx 会用得到
socket = /usr/local/var/run/mysqld57.sock
# mysql的数据目录
datadir = /usr/local/var/mysql_57
my80.cnf 文件内容
# Default Homebrew MySQL server config
[mysqld]
# Only allow connections from localhost
bind-address = 0.0.0.0
mysqlx-bind-address = 0.0.0.0
default_authentication_plugin = mysql_native_password
sql_mode = NO_ENGINE_SUBSTITUTION
port = 33068
# pid文件路径
pid-file = /usr/local/var/run/mysqld80.pid
# sock文件路径 nginx 会用得到
socket = /usr/local/var/run/mysqld80.sock
# mysql的数据目录
datadir = /usr/local/var/mysql_80
# 初始化 mysql5.7的数据目录 /usr/local/opt/mysql@5.7/bin/mysqld --defaults-file=/usr/local/etc/my57.cnf --initialize --explicit_defaults_for_timestamp # 初始化 mysql8.0的数据目录 /usr/local/opt/mysql@8.0/bin/mysqld --defaults-file=/usr/local/etc/my80.cnf --initialize --explicit_defaults_for_timestamp # 初始化目录成功后 控制台会打印一个root的临时密码,可以用mysql命令登录。 # 登录并修改密码 mysql -u root --host 127.0.0.1 --port 33065 -p # 在mysql中修改密码 方式1 SET PASSWORD = PASSWORD('your_new_password'); # 在mysql中修改密码 方式2 ALTER USER 'root'@'localhost' IDENTIFIED BY 'your_new_password'; # 密码修改成功 退出 exit
/usr/local/Cellar/mysql@5.7/5.7.44_1/homebrew.mysql@5.7.service
[Unit]
Description=Homebrew generated unit for mysql@5.7
[Install]
WantedBy=default.target
[Service]
Type=simple
ExecStart=/usr/local/opt/mysql@5.7/bin/mysqld_safe --defaults-file\=/usr/local/etc/my57.cnf --datadir\=/usr/local/var/mysql_57
Restart=always
WorkingDirectory=/usr/local/var/mysql_57
/usr/local/Cellar/mysql@5.7/5.7.44_1/homebrew.mxcl.mysql@5.7.plist
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> <plist version="1.0"> <dict> <key>KeepAlive</key> <true/> <key>Label</key> <string>homebrew.mxcl.mysql@5.7</string> <key>LimitLoadToSessionType</key> <array> <string>Aqua</string> <string>Background</string> <string>LoginWindow</string> <string>StandardIO</string> <string>System</string> </array> <key>ProgramArguments</key> <array> <string>/usr/local/opt/mysql@5.7/bin/mysqld_safe</string> <string>--defaults-file=/usr/local/etc/my57.cnf</string> <string>--datadir=/usr/local/var/mysql_57</string> </array> <key>RunAtLoad</key> <true/> <key>WorkingDirectory</key> <string>/usr/local/var/mysql_57</string> </dict> </plist>
/usr/local/Cellar/mysql@8.0/8.0.35/homebrew.mysql@8.0.service
[Unit]
Description=Homebrew generated unit for mysql@8.0
[Install]
WantedBy=default.target
[Service]
Type=simple
ExecStart=/usr/local/opt/mysql@8.0/bin/mysqld_safe --defaults-file\=/usr/local/etc/my80.cnf --datadir\=/usr/local/var/mysql_80
Restart=always
WorkingDirectory=/usr/local/var/mysql_80
/usr/local/Cellar/mysql@8.0/8.0.35/homebrew.mxcl.mysql@8.0.plist
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> <plist version="1.0"> <dict> <key>KeepAlive</key> <true/> <key>Label</key> <string>homebrew.mxcl.mysql@8.0</string> <key>LimitLoadToSessionType</key> <array> <string>Aqua</string> <string>Background</string> <string>LoginWindow</string> <string>StandardIO</string> <string>System</string> </array> <key>ProgramArguments</key> <array> <string>/usr/local/opt/mysql@8.0/bin/mysqld_safe</string> <string>--defaults-file=/usr/local/etc/my80.cnf</string> <string>--datadir=/usr/local/var/mysql_80</string> </array> <key>RunAtLoad</key> <true/> <key>WorkingDirectory</key> <string>/usr/local/var/mysql_80</string> </dict> </plist>
# 启动 mysql5.7
brew services start mysql@5.7
# 启动 mysql8.0
brew services start mysql@8.0
# 查看启动状态
ps -ef | grep mysql
某些软件使用 mysql 命令的时候不能指定自定义参数 比如 DBeaver 的导出导入数据库功能,会使用 mysqldump 命令,但是使用了默认的/tmp/mysql.sock。
但是我们安装多版本 mysql 的时候,指定了自定义的 sock 文件位置,所以在我们需要用到 /tmp/mysql.sock 的时候需要手动创建一个链接。
比如我们现在需要对 mysql5.7 进行导入导出,它会提示 /tmp/mysql.sock 的错误,我们直接用链接方式解决。
# -- 删除默认的 sock 文件
rm /tmp/mysql.sock
# -- 创建 mysql5.7 的 sock 文件链接
ln -s /usr/local/var/run/mysqld57.sock /tmp/mysql.sock
然后我们就可以正常使用 DBeaver 的导出导入数据库功能了,如果想要操作 mysql8.0 只需要把 sock 文件链接到 mysql8.0 的 sock 文件即可。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。