1、首先到https://dev.mysql.com/downloads/mysql/网站下载tar.gz包,如下图所示:
2、然后将下载的包使用tar -xzvf mysql-8.0.16-macos10.14-x86_64.tar.gz 解压,如下图所示:
3、然后将mysql-8.0.16-macos10.14-x86_64通过sudo mv /sunxj/InstallFile/mysql-8.0.16-macos10.14-x86_64 mysql命令移动到/usr/local/目录中,意思是移动并改名为mysql,如下图所示:
4、vim /etc/profile修改/etc/profile文件,将mysql的路径添加进去:
export MYSQL_HOME=/usr/local/mysql
export PATH=$MYSQL_HOME/bin:$MYSQL_HOME/support-files:$PATH
如下图所示:
5、然后执行source /etc/profile,使配置的环境变量生效。
6、修改mysql安装目录所属用户与用户组,使用如下命令:sudo chown -R root:admin mysql,如下图所示:
注意:如果此处的用户和组不对就会出现启动错误,如: ERROR! The server quit without updating PID file (/usr/local/mysql/data/sunxj-mac-air.local.pid).,如下图所示:
7、然后执行sudo mysqld --initialize --user=mysql命令来初始化,会初始化一个密码,此密码是一个root密码,注意:此处的--user=mysql对应的是mysql用户,如果是root系统用户则使用--user=root如下信息:
2019-06-05T03:04:56.635280Z 0 [System] [MY-013169] [Server] /usr/local/mysql/bin/mysqld (mysqld 8.0.16) initializing of server in progress as process 3394
2019-06-05T03:04:56.637769Z 0 [Warning] [MY-010159] [Server] Setting lower_case_table_names=2 because file system for /usr/local/mysql/data/ is case insensitive
2019-06-05T03:04:59.906744Z 5 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: LMkaovg46.2)
2019-06-05T03:05:02.008501Z 0 [System] [MY-013170] [Server] /usr/local/mysql/bin/mysqld (mysqld 8.0.16) initializing of server has completed
如下图所示:
注意: MySQL 5.7.6 以上版本取消了 scripts 目录, 初始化命令改成了sudo bin/mysqld --initialize --user=mysql
8、MySQL 5.7.18及以上不再自带默认配置文件。手动写一个就行:使用如下命令:sudo vim /etc/my.cnf,然后输入如下内容:
[mysqld]
basedir=/usr/local/mysql
default_authentication_plugin=mysql_native_password
如下图所示:
注意:如果想免密码登录则只需要在配置文件中加入:skip-grant-tables即可
9、保存之后修改权限,使用命令修改:sudo chmod 664 /etc/my.cnf
10、然后执行mysql.server start来启动,如下图所示:
11、输入命令mysql -u root -p,输入:LMkaovg46.2,然后即可进入mysql,如下图所示:
12、输入:flush privileges;表示将root@localhost密码改为复杂形式,mysql8.0以上要求严格,如下图所示:
13、修改密码alter user'root'@'localhost' IDENTIFIED BY 'WinCom@1234';将root的密码修改为WinCom@1234,如下图所示:
14、然后exit退出,重启mysql服务,如下图所示:
15、此时使用工具连接会出现2059 - Authentication plugin 'caching_sha2_password' cannot be loaded: dlopen(../Frameworks/caching_sha2_password.so, 2): image not found错误,如下图所示:
16、再次进入mysql命令行中,修改加密规则,输入:ALTER USER 'root'@'localhost' IDENTIFIED BY 'password' PASSWORD EXPIRE NEVER; ,如下图所示:
17、使用命令:ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'WinCom@1234';更新用户密码,如下图所示:
18、再次测试即可成功,如下图所示:
19、此时如果远程登录会出现失败,如下图所示:
20、允许远程登录,首先切换到mysql数据库,如下图所示:
21、然后查询用户信息:select user,host from user;,如下图所示:
22、然后更改root用户的host,将localhost更改为%即可,执行:update user set host="%" where user="root";,如下图所示:
23、然后刷新权限:flush privileges;如下图所示:
24、再次测试通过,如下图所示:
26、通过brew install mysql命令安装,其位置在/usr/local/Cellar/mysql/8.0.18,如下图所示:
此时会提示Error: The `brew link` step did not complete successfully错误,如下图所示:
27、然后依次执行:rm '/usr/local/lib/libprotobuf-lite.dylib'、brew link --overwrite mysql、brew link --overwrite --dry-run mysql,如下图所示:
28、然后在次执行brew unlink mysql && brew link mysql,如下图所示:
29、然后执行mysql.server start启动mysql,如下图所示:
30、然后执行mysql_secure_installation进行初始化,如下信息所示:
Securing the MySQL server deployment.
Connecting to MySQL using a blank password.
VALIDATE PASSWORD COMPONENT can be used to test passwords
and improve security. It checks the strength of password
and allows the users to set only those passwords which are
secure enough. Would you like to setup VALIDATE PASSWORD component?
// 这个选yes的话密码长度就必须要设置为8位以上,但我只想要6位的
Press y|Y for Yes, any other key for No: N
Please set the password for root here.
New password: // 设置密码
Re-enter new password: // 再一次确认密码
By default, a MySQL installation has an anonymous user,
allowing anyone to log into MySQL without having to have
a user account created for them. This is intended only for
testing, and to make the installation go a bit smoother.
You should remove them before moving into a production
environment.
// 移除不用密码的那个账户
Remove anonymous users? (Press y|Y for Yes, any other key for No) : Y
Success.
Normally, root should only be allowed to connect from
'localhost'. This ensures that someone cannot guess at
the root password from the network.
//是否禁止远程登录
Disallow root login remotely? (Press y|Y for Yes, any other key for No) : n
... skipping.
By default, MySQL comes with a database named 'test' that
anyone can access. This is also intended only for testing,
and should be removed before moving into a production
environment.
// 是否删除test库
Remove test database and access to it? (Press y|Y for Yes, any other key for No) : y
- Dropping test database...
Success.
- Removing privileges on test database...
Success.
Reloading the privilege tables will ensure that all changes
made so far will take effect immediately.
Reload privilege tables now? (Press y|Y for Yes, any other key for No) : y
Success.
All done!
31、此时进行测试会出现2059 - Authentication plugin 'caching_sha2_password' cannot be loaded: dlopen(../Frameworks/caching_sha2_password.so, 2): image not found,如下图所示:
32、以上信息与15步出现的情况相同,按照之前的信息一步一步的配置即可,最后会连接成功。
33、相关命令:
# 启动
sudo mysql.server start或brew services start mysql
# 重启
sudo mysql.server restart或brew services restart mysql
# 停止
sudo mysql.server stop或brew services stop mysql
# 检查 MySQL 运行状态
sudo mysql.server status
————————————————
版权声明:本文为CSDN博主「sxjlinux」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/sunxiaoju/java/article/details/90899339