使用HOMEbrew安装完MySQL5.7的时候,发现服务启动后连接不上,查资料说是需要安装组件mysql_secure_installation ,

➜  # mysql_secure_installation

Securing the MySQL server deployment.

The password file '/Users/kk/.mysql_secret' is corrupt! Skipping.
Enter password for user root:
Error: Plugin '*A4B6157319038724E3560894F7F932C8886EBFCF' is not loaded

查了各种资料都没找到报这个错的原因,实在没办法了,只好MySQL卸载后重新安装 1.先把涉及到MySQL的文件夹全删掉

➜ # ps ax |grep mysqld
 1401   ??  S      0:00.02 /bin/sh /usr/local/opt/mysql/bin/mysqld_safe --datadir=/usr/local/var/mysql
 1476   ??  S      0:00.27 /usr/local/opt/mysql/bin/mysqld --basedir=/usr/local/opt/mysql --datadir=/usr/local/var/mysql --plugin-dir=/usr/local/opt/mysql/lib/plugin --log-error=kaideMacBook-Pro.local.err --pid-file=kaideMacBook-Pro.local.pid
 1478 s000  R+     0:00.00 grep --color=auto --exclude-dir=.bzr --exclude-dir=CVS --exclude-dir=.git --exclude-dir=.hg --exclude-dir=.svn mysqld

包括/usr/local/opt/mysql/,/usr/local/var/mysql,/usr/local/celler/mysql/5.7/

2.重新安装MySQL


➜ # brew install mysql
Updating Homebrew...
^C==> Downloading https://homebrew.bintray.com/bottles/mysql-5.7.19.sierra.bottle.tar.gz
Already downloaded: /Users/kk/Library/Caches/Homebrew/mysql-5.7.19.sierra.bottle.tar.gz
==> Pouring mysql-5.7.19.sierra.bottle.tar.gz
==> /usr/local/Cellar/mysql/5.7.19/bin/mysqld --initialize-insecure --user=kk --basedir=/usr/local/Cellar/mysql/5.7.19 --datadir=/usr/local/var/mysql --tmpdir=/tmp
==> Caveats
We've installed your MySQL database without a root password. To secure it run:
    mysql_secure_installation

MySQL is configured to only allow connections from localhost by default

To connect run:
    mysql -uroot

To have launchd start mysql now and restart at login:
  brew services start mysql
Or, if you don't want/need a background service you can just run:
  mysql.server start
==> Summary
  /usr/local/Cellar/mysql/5.7.19: 322 files, 233MB

3.执行mysql_secure_installation,安装MySQL安全组件

➜  # mysql_secure_installation

Securing the MySQL server deployment.

The password file '/Users/kk/.mysql_secret' is corrupt! Skipping.
Enter password for user root:
Error: Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)

4.这时想起来MySQL还没有启动,先去启动

➜  # brew services start mysql
==> Successfully started `mysql` (label: homebrew.mxcl.mysql)
➜  # brew services list
Name      Status  User Plist
memcached started kk   /Users/kk/Library/LaunchAgents/homebrew.mxcl.memcached.plist
mysql     started kk   /Users/kk/Library/LaunchAgents/homebrew.mxcl.mysql.plist
nginx     started kk   /Users/kk/Library/LaunchAgents/homebrew.mxcl.nginx.plist
php56     started kk   /Users/kk/Library/LaunchAgents/homebrew.mxcl.php56.plist
php70     started kk   /Users/kk/Library/LaunchAgents/homebrew.mxcl.php70.plist
php72     stopped
redis     stopped

5.再次执行mysql_secure_installation


➜  # mysql_secure_installation

Securing the MySQL server deployment.

使用HOMEbrew安装完MySQL5.7之后,默认密码为空,需要安装安全组件才能修改root密码
Connecting to MySQL using a blank password.

VALIDATE PASSWORD PLUGIN 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 plugin?

Press y|Y for Yes, any other key for No: y

There are three levels of password validation policy:

LOW    Length >= 8
MEDIUM Length >= 8, numeric, mixed case, and special characters
STRONG Length >= 8, numeric, mixed case, special characters and dictionary                  file

选择一种密码验证规则
Please enter 0 = LOW, 1 = MEDIUM and 2 = STRONG: 0
Please set the password for root here.

输入新密码
New password:

Re-enter new password:

Estimated strength of the password: 50
Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) : y
默认安装完MySQL5.7之后,会生成一个匿名用户,匿名用户不需要密码登录,安全组件安装之后建议删除此用户
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) : n

 ... skipping.

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) : y
Success.

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.

Remove test database and access to it? (Press y|Y for Yes, any other key for No) : n

 ... skipping.
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!

6.使用设置好的密码登录root账号

➜  # mysql -uroot -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 6
Server version: 5.7.19 Homebrew

Copyright (c) 2000, 2017, 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.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> select user();
+----------------+
| user()         |
+----------------+
| root@localhost |
+----------------+
1 row in set (0.00 sec)

mysql>

完成~~~

7.创建个数据库试试

CREATE DATABASE `kk_test` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;