无法连接到127.0.0.1,但`mysqld`正在侦听

时间:2017-05-07 14:29:26

标签: php mysql connection

在新安装的服务器版本上,无法通过TCP连接到mysql上的localhost:5.7.18-0ubuntu0.17.04.1(Ubuntu)

这有效:

# 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登录?

更新localhost CREATE USER 'root'@'127.0.0.1'; GRANT ALL PRIVILEGES ON *.* TO 'root'@'127.0.0.1'; FLUSH PRIVILEGES; 表示本地套接字,因此我为' root' @&#39创建了另一条记录; 127.0.0.1'用于TCP连接。

# 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)

未设置密码。

mysql -h 127.0.0.1

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

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

但是在重新创建用户之后,它开始工作:

storybook

为什么它不能与开箱即用的同一个用户合作?

2 个答案:

答案 0 :(得分:1)

您的root用户是否有密码?尝试命令

mysql -h 127.0.0.1 -u root -p

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

停止MySQL服务

#/etc/init.d/mysql stop

使用--skip-grant-tables执行MySQL服务器

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

以root身份执行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

答案 1 :(得分:1)

这是解决方案:

 public function report_slide()
    {
        //report
        $this->db->select('a.*, b.category_name, c.co_abbreviated_name, d.country_name, e.language, f.report_type, g.year, h.type');
        $this->db->from('rc_report a, rc_category b, rc_company c, rc_country d, rc_language e, rc_report_type f, rc_year g, rc_gri h');
        $this->db->where('b.id_category = a.id_category');
        $this->db->where('c.id_company = a.id_company');
        $this->db->where('d.id_country = a.id_country');
        $this->db->where('e.id_language = a.id_language');
        $this->db->where('f.id_report_type = a.id_report_type');
        $this->db->where('g.id_year = a.id_year');
        $this->db->where('h.id_gri = a.id_gri');
        $this->db->order_by('g.year','desc');
        $this->db->limit(5);
        $query_report = $this->db->get();
        $result_array=$query_report->result();

        return $result_array;
    }

默认情况下,UPDATE mysql.user SET plugin='mysql_native_password' WHERE user='root' AND host ='localhost'; FLUSH PRIVILEGES; 配置为对仅适用于本地unix套接字的插件进行身份验证。此命令切换到用户的'root'@'localhost'插件,该插件适用于TCP(本地PHP安装所需)。

在更新2之后,我在重新制作mysql_native_password之前和之后比较了mysql.user,并在root@localhost列中找到了差异。