2017-05-07 68 views
0

无法通过TCP连接到mysqllocalhost在刚安装的服务器版本:5.7.18-0ubuntu0.17.04.1(Ubuntu的)无法连接到127.0.0.1,但`mysqld`是听

这工作:

# mysql 
Welcome to the MySQL monitor. Commands end with ; or \g. 
Your MySQL connection id is 27 
Server version: 5.7.18-0ubuntu0.17.04.1 (Ubuntu) 

这不:

# mysql -h 127.0.0.1 
ERROR 1698 (28000): Access denied for user 'root'@'localhost' 

从客户端中我看到mysql被配置为侦听127.0.0.1:

mysql> show variables like '%bind%'; 
+---------------+-----------+ 
| Variable_name | Value  | 
+---------------+-----------+ 
| bind_address | 127.0.0.1 | 
+---------------+-----------+ 
1 row in set (0.00 sec) 

而且它确实听:

# netstat -tl 
Active Internet connections (only servers) 
Proto Recv-Q Send-Q Local Address   Foreign Address   State 
tcp  0  0 localhost:mysql   0.0.0.0:*    LISTEN 

的,因为这同时,PHP也失败:

Warning: mysqli_connect(): (HY000/1698): Access denied for user 'root'@'localhost' 

如何解决呢?也许有一个设置禁用基于密码的TCP登录?

UPDATE:这是appeared意味着localhost本地套接字,所以我创建了TCP连接“root'@'127.0.0.1”再创历史新高。

CREATE USER 'root'@'127.0.0.1'; 
GRANT ALL PRIVILEGES ON *.* TO 'root'@'127.0.0.1'; 
FLUSH PRIVILEGES; 

但它仍然不能正常工作:

# mysql --protocol=TCP --host=127.0.0.1 
ERROR 1698 (28000): Access denied for user 'root'@'localhost' 

没有设置口令。

mysql> SELECT host, user, authentication_string FROM mysql.user; 
+-----------+------------------+-------------------------------------------+ 
| host  | user    | authentication_string      | 
+-----------+------------------+-------------------------------------------+ 
| localhost | root    |           | 
| localhost | mysql.sys  | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE | 
| localhost | debian-sys-maint | *4CF7B81390C295E6AF38067F3B621BBF736D3366 | 
| 127.0.0.1 | root    |           | 
+-----------+------------------+-------------------------------------------+ 
4 rows in set (0.01 sec) 

更新2:我从头开始,所以这是一个失败的试验:

mysql -h 127.0.0.1 

但重建用户后,它开始工作:

DROP USER 'root'@'localhost'; 
CREATE USER 'root'@'localhost'; 
GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' WITH GRANT OPTION; 
FLUSH PRIVILEGES; 

为什么它不适用于开箱即用的同一用户?

+0

仔细检查你的mysql的用户名和密码 –

+0

请参阅[this](https://stackoverflow.com/questions/10823854/using-for-host-when-creating-a-mysql-user)文章。 –

+0

你有一个MySQL的密码,我猜。在这两种情况下都需要传递根密码 – Exprator

回答

2

你有root用户的密码吗?尝试命令

mysql -h 127.0.0.1 -u root -p 

如果您运行了mysql_secure_installation,它会阻止远程root登录。您可以尝试以下操作来重置root用户。

停止MySQL服务

#/etc/init.d/mysql stop 

Exec的MySQL服务器用--skip-赠款表

#mysqld -u mysql --skip-grant-tables & 

Exec的MySQL客户端为根

#mysql -u root 

更新您的密码

mysql> update mysql.user set password=password('newpassword') where user='anotheruser'; 

刷新特权

mysql> flush privileges; 

杀死mysqld的

#killall mysql 

启动MySQL守护

#/etc/init.d/mysql start 
+0

我需要一个没有密码的连接。更新了问题。 –

0

这是解决方案:

UPDATE mysql.user SET plugin='mysql_native_password' WHERE user='root' AND host ='localhost'; 
FLUSH PRIVILEGES; 

默认情况下,'root'@'localhost'被配置为通过仅适用于本地unix套接字的插件进行身份验证。该命令切换为用户的mysql_native_password插件,该插件与TCP(我的本地PHP安装需要)一起使用。

更新2之后我比较了mysql.user前后重新创建[email protected],并在plugin列中找到差异列。

相关问题