1.默认安装的mysql不能连接127.0.0.1
1.错误实例
mysql -h 127.0.0.1 -u root -p
Enter password:
ERROR 1698 (28000): Access denied for user 'root'@'localhost'
2.解决方案
停止mysql服务
sudo service mysql stop
以安全模式启动mysql
sudo mysqld_safe --skip-grant-tables & sudo service mysql start
直接不用密码登录
mysql -u root
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.10 MySQL Community Server (GPL)
查看一下user表,错误的起因就是在这里, root的plugin被修改成了auth_socket,用密码登陆的plugin应该是mysql_native_password。
mysql> select user, plugin from mysql.user;
+-----------+-----------------------+
| user | plugin |
+-----------+-----------------------+
| root | auth_socket |
| mysql.sys | mysql_native_password |
| dev | mysql_native_password |
+-----------+-----------------------+
3 rows in set (0.01 sec)
设置root的plugin
mysql> update mysql.user set authentication_string=PASSWORD(''), plugin='mysql_native_password' where user='root';
Query OK, 1 row affected, 1 warning (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 1
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
为了防止远程访问被拒绝,注释掉 bind-address 相关内容
cd /etc/mysql/mysql.conf.d
vi mysqld.cnf
# bind-address = 127.0.0.1
重启服务
- 方法一
sudo service mysql stop
sudo service mysql start
- 方法二
sudo service mysql restart
再次执行 mysql -h 127.0.0.1 -u root -p
mysql -h 127.0.0.1 -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.10 MySQL Community Server (GPL)
未完待续
网友评论