mac 安装mysql采坑记录

最近搭建项目时需要用到mysql,由于之前安装的mysql启动失败,无奈之下只好卸载。这一卸,噩梦来了,homebrew安装mysql后启动输入mysql.server start报各种错

Starting MySQL.. ERROR! The server quit without updating PID file (/usr/local/mysql/var/AYXXXXXXXXXXX.pid). 

删除文件也不行。无奈之下,只好又卸载安装,又报错~~~就这样折腾了一个晚上还是没有成功,开始怀疑人生了
早上来的时候仔细分析了下,卸载后又重新安装,启动失败的原因其实是卸载mysql相关文件没有删除干净。于是乎把关于mysql相关的文件都手动删了。参照网上的路径,一一执行

先执行brew uninstall mysql 或 brew remove mysql
sudo rm /usr/local/mysql
sudo rm -rf /usr/local/mysql*
sudo rm -rf /Library/StartupItems/MySQLCOM
sudo rm -rf /Library/PreferencePanes/My*
rm -rf ~/Library/PreferencePanes/My*
sudo rm -rf /Library/Receipts/mysql*
sudo rm -rf /Library/Receipts/MySQL*
sudo rm -rf /var/db/receipts/com.mysql.*
/usr/local/Cellar 里的mysql文件
/usr/local/var 里的mysql文件
/tmp 里的mysql.sock, mysql.sock.lock, my.cnf文件
pid文件和err文件都在/usr/local/var/mysql里确保删除了
brew安装的安装包存储在/usr/local/Library/Cache/Homebrew也可以一并删除
执行brew cleanup

确认卸载干净后,执行

brew install mysql

下载成功后的界面是这样的

==> Downloading https://homebrew.bintray.com/bottles/mysql-5.7.21.sierra.bottle.tar.gz
######################################################################## 100.0%
==> Pouring mysql-5.7.21.sierra.bottle.tar.gz
==> /usr/local/Cellar/mysql/5.7.21/bin/mysqld --initialize-insecure --user=zhuhaiyun --b
==> 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.21: 323 files, 233.9MB

之前执行下面这个,又报错~~~~

➜  ~ mysql_secure_installation

Securing the MySQL server deployment.

Enter password for user root:
Error: Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)

谷歌了下,
https://stackoverflow.com/questions/22436028/cant-connect-to-local-mysql-server-through-socket-tmp-mysql-sock-2
执行

➜  ~ mysql.server start


Starting MySQL
. SUCCESS!

居然成功了!!!后来知道之前的报错Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)是因为没有启动mysql服务,此文件不存在。
再次执行mysql_secure_installation命令,按提示输入密码和yes or no

➜  ~ mysql_secure_installation

Securing the MySQL server deployment.

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: n // yes要设置为8位以上的密码
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.


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!

终于解决了,真不容易啊~~~终于可以做接下来的事了。
参考文档:
https://blog.csdn.net/sarahhuangzht/article/details/51508112
https://github.com/arronf2e/blog/issues/63

posted @ 2018-03-23 11:36  kivi2  阅读(210)  评论(0编辑  收藏  举报