您的位置:首页 > 数据库 > MySQL

mac上mysql报错以及root密码忘记解决办法

2016-02-15 00:00 756 查看
摘要: 今天mac登陆mysql后异常错误ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2),root密码也给忘记了,然后成功登陆并找回root密码

1、对于前者ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)这个错误,一般是mysql服务没开,

命令行下执行sudo /Library/StartupItems/MySQLCOM/MySQLCOM restart即可。

或者按照下面的步骤也可以:

打开文件 vi ~/.bash_profile

文件内粘帖下面的代码

# mysql
alias mysql='/usr/local/mysql/bin/mysql'
alias mysqladmin='/usr/local/mysql/bin/mysqladmin'
# ls
alias ls='ls -G'


保存并退出 esc + :wq

source ~/.bash_profile 确定生效

后尝试直接输入mysql发现可以登陆进去,见下面的代码事例

$ sudo /Library/StartupItems/MySQLCOM/MySQLCOM restart
Restarting MySQL database server
$ mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.6.25 MySQL Community Server (GPL)

Copyright (c) 2000, 2015, 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();

发现登陆者只是普通用户,并非root,然后怎么办呢?

2、重新设置root密码,(标红命令直接输入回车后打开终端直接登陆)

安装navicat或其他客户端

苹果->系统偏好设置->最下边点mysql 在弹出页面中 关闭mysql服务

终端输入 cd /usr/local/mysql/bin/

回车输入sudo su

再回车后输入以下命令来禁止mysql验证功能 ./mysqld_safe –skip-grant-tables &

再回车后mysql会自动重启,重启好了之后打开navicat,这时候就可以不需要密码登录。直接修改root密码后重新启动MySQL即可。



然后?都到这一步了你还问我然后?想干什么就是你的事了,哈哈。。

我们终端测试下结果如何(上面密码我就设置为简单的123):

$ mysql -uroot -p123
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 87
Server version: 5.6.25 MySQL Community Server (GPL)

Copyright (c) 2000, 2015, 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@  |
+--------+
1 row in set (0.00 sec)

ok,大功告成!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  mac mysql