mac mysql Access denied for user问题有效解决方法

可能有些读者在安装完 mysql 之后出现以下错误提示:

mysql -uroot -p
Enter password:

ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)

这是 mysql 在 linux 系统下的一个小缺陷,未给出 root 密码,导致初次无法登陆。这里给出 mysql 密码修改的有效方案,该方案在5.7.12亲测有效:

0) 在用户目录下输入以下命令:

touch bashrc
sudo chmod +w bashrc
sudo vi /etc/bashrc
在bashrc的末尾增加以下两个命令别名,便于快速使用mysql
alias mysql='/usr/local/mysql/bin/mysql'
alias mysqladmin='/usr/local/mysql/bin/mysqladmin'

1) 停止 mysql 服务

sudo /usr/local/mysql/support-files/mysql.server stop

2) 在安全模式下启动 mysql

sudo /usr/local/mysql/bin/mysqld_safe --skip-grant-tables

3)  新建一个命令窗口(可在当前窗口下按快捷键command+N创建):

mysql -u root

4) 在命令窗口下修改命令

mysql > UPDATE mysql.user SET Password=PASSWORD('1234') WHERE User='root';

若上面的命令不行,试试下面的:

mysql> update mysql.user set authentication_string=password('1234') where user='root';

Query OK, 1 row affected, 1 warning (0.01 sec)

Rows matched: 1  Changed: 1  Warnings: 1

如未成功请参看【补充】

5) 

mysql > FLUSH PRIVILEGES;

6) 新建命令窗口,用密码1234再尝试登陆

mysql -uroot -p
Enter password:

Welcome to the MySQL monitor.  Commands end with ; or \g.

Your MySQL connection id is 3

Server version: 5.7.12 MySQL Community Server (GPL)

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

看到上述信息,就表明成功用 root 登陆 mysql。

【补充】也许不同版本 mysql.user 栏目名称会有不同,如果上述 4)命令报错:

ERROR 1054 (42S22): Unknown column 'password' in 'field list'

那么就更改命令为:

mysql> update mysql.user set authentication_string=password('1234') where user='root';

  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值