如题。 首先是基本系统情况:
我做的一些排查工作:
➜ blog service mysql status
* Percona Server 5.7.13-6 is running
➜ blog netstat -ap | grep mysql
tcp 0 0 localhost:mysql *:* LISTEN 20978/mysqld
➜ blog mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Percona日志显示:
2016-08-07T11:49:26.189089Z 0 [Note] /usr/sbin/mysqld: ready for connections.
Version: '5.7.13-6' socket: '/var/run/mysqld/mysqld.sock' port: 3306 Percona Server (GPL), Release '6', Revision 'e3d58bb'
但是无论 WordPress 还是 phpmyadmin ,均不能连接 percona 。数据库主机尝试过设置 localhost 和 127.0.0.1 ,均无果。
下面是 percona 配置文件,完全官方,没有任何更改(/etc/mysql/my.cnf ):
#
# The MySQL database server configuration file.
#
# You can copy this to one of:
# - "/etc/mysql/my.cnf" to set global options,
# - "~/.my.cnf" to set user-specific options.
#
# One can use all long options that the program supports.
# Run program with --help to get a list of available options and with
# --print-defaults to see which it would actually understand and use.
#
# For explanations see
# http://dev.mysql.com/doc/mysql/en/server-system-variables.html
# This will be passed to all mysql clients
# It has been reported that passwords should be enclosed with ticks/quotes
# escpecially if they contain "#" chars...
# Remember to edit /etc/mysql/debian.cnf when changing the socket location.
[client]
port = 3306
socket = /var/run/mysqld/mysqld.sock
# Here is entries for some specific programs
# The following values assume you have at least 32M ram
# This was formally known as [safe_mysqld]. Both versions are currently parsed.
[mysqld_safe]
socket = /var/run/mysqld/mysqld.sock
nice = 0
[mysqld]
#
# * Basic Settings
#
user = mysql
pid-file = /var/run/mysqld/mysqld.pid
socket = /var/run/mysqld/mysqld.sock
port = 3306
basedir = /usr
datadir = /var/lib/mysql
tmpdir = /tmp
lc-messages-dir = /usr/share/mysql
skip-external-locking
#
# Instead of skip-networking the default is now to listen only on
# localhost which is more compatible and is not less secure.
bind-address = 127.0.0.1
#
# * Fine Tuning
#
#key_buffer = 16M
max_allowed_packet = 16M
thread_stack = 192K
thread_cache_size = 8
# This replaces the startup script and checks MyISAM tables if needed
# the first time they are touched
#myisam-recover = BACKUP
#max_connections = 100
#table_cache = 64
#thread_concurrency = 10
#
# * Query Cache Configuration
#
query_cache_limit = 1M
query_cache_size = 16M
#
# * Logging and Replication
#
# Both location gets rotated by the cronjob.
# Be aware that this log type is a performance killer.
# As of 5.1 you can enable the log at runtime!
#general_log_file = /var/log/mysql/mysql.log
#general_log = 1
#
# Error log - should be very few entries.
#
log_error = /var/log/mysql/error.log
#
# Here you can see queries with especially long duration
#log_slow_queries = /var/log/mysql/mysql-slow.log
#long_query_time = 2
#log-queries-not-using-indexes
#
# The following can be used as easy to replay backup logs or for replication.
# note: if you are setting up a replication slave, see README.Debian about
# other settings you may need to change.
#server-id = 1
#log_bin = /var/log/mysql/mysql-bin.log
expire_logs_days = 10
max_binlog_size = 100M
#binlog_do_db = include_database_name
#binlog_ignore_db = include_database_name
#
# * InnoDB
#
# InnoDB is enabled by default with a 10MB datafile in /var/lib/mysql/.
# Read the manual for more InnoDB related options. There are many!
#
# * Security Features
#
# Read the manual, too, if you want chroot!
# chroot = /var/lib/mysql/
#
# For generating SSL certificates I recommend the OpenSSL GUI "tinyca".
#
# ssl-ca=/etc/mysql/cacert.pem
# ssl-cert=/etc/mysql/server-cert.pem
# ssl-key=/etc/mysql/server-key.pem
[mysqldump]
quick
quote-names
max_allowed_packet = 16M
[mysql]
#no-auto-rehash # faster start of mysql but no tab completition
[isamchk]
#key_buffer = 16M
#
# * IMPORTANT: Additional settings that can override those from this file!
# The files must end with '.cnf', otherwise they'll be ignored.
#
!includedir /etc/mysql/conf.d/
另外, PHP 和 MySQL 以及 nginx 的错误日志都有排查过,没有发现任何报错……
求教各位大神,感谢!
感谢各位大神的回复。
综合了一下,看来是MySQL拒绝了PHP的连接,拒绝原因是root用户作用域只授权给localhost,而mysql根据连接地址127.0.0.1(本地连接)反向解析出的竟然是我的主机名(fourstring),然后与root用户localhost的作用域不符,因此拒绝连接。
解决办法是@kn007提出的:
USE mysql;CREATE USER 'root'@'%' IDENTIFIED BY '密码';GRANT ALL PRIVILEGES ON *.* TO 'root'@'%';FLUSH PRIVILEGES;
即新建一个作用域为'%'的root用户并授予所有权限即可解决。
非常感谢!
1
UnisandK 2016-08-07 20:13:04 +08:00
telnet 127.0.0.1 3306 看看
|
2
fourstring OP @UnisandK
➜ log telnet 127.0.0.1 3306 Trying 127.0.0.1... Connected to 127.0.0.1. Escape character is '^]'. CHost 'fourstring' is not allowed to connect to this MySQL serverConnection closed by foreign host. 这种情况是正常的吗? |
4
UnisandK 2016-08-07 20:32:08 +08:00
@fourstring 不正常。。本地来的连接都 is not allowed 你说其他应用连得上个鬼。。
|
5
fourstring OP |
6
fourstring OP @UnisandK 如果本地连接都被 ban 了的话,为什么用 mysql -u root -p 仍然能连接上呢?
|
7
extreme 2016-08-07 20:45:44 +08:00
@fourstring 依我看,最好试试自己写 PHP 代码连,看看属性 connect_error 的值……
|
8
fourstring OP @extreme
➜ ~ php test.php PHP Fatal error: Uncaught PDOException: SQLSTATE[HY000] [1130] Host 'fourstring' is not allowed to connect to this MySQL server in /root/test.php:2 我的 VPS 主机名就是'fourstring',这难道意味着不允许这个主机名连接? |
9
kn007 2016-08-07 21:02:56 +08:00
cat /etc/resolv.conf 来看看。
你不是用 localhost 和 127.0.0.1 来链接吗? 怎么还会有 hostname ? 解决这个问题,就是登录 mysql ,把对应的用户的域改为*而非本地主机。 |
10
kn007 2016-08-07 21:05:06 +08:00
你确定你是全新数据库?不要把其他地方的库,硬生生导入 mysql 。
直接 mysql -uroot -p 密码 -h 127.0.0.1 登录试试。 不行就--skip-grant-tables ,然后 use mysql;查看下用户什么情况 |
11
kn007 2016-08-07 21:06:34 +08:00
对了,第一条回复,应该是把作用域改成%,不是*。
|
12
fourstring OP @kn007 这个。。。我之前是把 mariadb 的库导入 percona ,都是 MySQL 的一个分支,应该问题不大?
而且关键以前是可以正常运行的,今天突然挂掉了。。。 |
13
kn007 2016-08-07 21:08:24 +08:00
刚看到评论,说你 mysql 可以登上去。
现在你有两个解决方案: 第一个,最节省时间成本,把作用域改成%。 第二个,检查 /etc/hosts ,看下为何 mysql 会认为你不是以本地登入 |
14
fourstring OP @kn007 cat 的真的是 resolv.conf ?不是 hosts ?
resolv.conf : # Generated by SolusVM nameserver 8.8.8.8 nameserver 8.8.4.4 hosts : # Generated by SolusVM 127.0.0.1 localhost localhost.localdomain ::1 localhost localhost.localdomain xx.xx.xx.xx(本机 IP) fourstring |
15
fourstring OP @kn007 好的,我看看,感谢!
|
16
kn007 2016-08-07 21:09:56 +08:00
@fourstring 一般来说,不能互导入,监测下 mysql-error.log 看看。 mysql5.5 导入到 5.7 都会出错。
导入的话,建议备份非内置表,然后全新创建后,导入。 |
17
kn007 2016-08-07 21:11:48 +08:00
|
18
fourstring OP @kn007 我的做法就是新建一个数据库'wordpress',然后导出老服务器上的数据库再导入新数据库
|
19
kn007 2016-08-07 21:12:58 +08:00
@fourstring 那这样导入是不存在的问题。
|
20
kn007 2016-08-07 21:16:25 +08:00
@fourstring 重启 mysql 或 FLUSH PRIVILEGES;试过么?
|
21
kn007 2016-08-07 21:16:52 +08:00
重启 mysql ,看下 mysql-error.log 有没有报错。
|
22
fourstring OP @kn007 您说的 mysql-error.log 就是配置文件里配置的 /var/log/mysql/error.log 吗?
是的话好像并没有报错。。。全都是[Note]信息。 另外求教一下更改用户权限的 SQL 怎么写呢?(我以前都是用 phpmyadmin= = ) |
23
kn007 2016-08-07 21:26:44 +08:00
|
24
fourstring OP @kn007
mysql> USE mysql;GRANT ALL PRIVILEGES ON *.* TO 'root'@'%';FLUSH PRIVILEGES; Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A Database changed ERROR 1133 (42000): Can't find any matching row in the user table Query OK, 0 rows affected (0.00 sec) 这是什么原因呢? user 表中没有符合条件的数据? |
25
kn007 2016-08-07 21:30:54 +08:00
|
26
fourstring OP @kn007 是的。。。重启并且执行了这个刷新权限的语句。。。然而 依然无解
|
27
kn007 2016-08-07 21:35:34 +08:00
@fourstring show me:
use mysql; select host,user from user ; |
28
fourstring OP @kn007
mysql> use mysql; select host,user from user ; Database changed +-----------+-----------+ | host | user | +-----------+-----------+ | localhost | mysql.sys | | localhost | root | +-----------+-----------+ 2 rows in set (0.00 sec) |
29
kn007 2016-08-07 21:38:40 +08:00
@fourstring 哦,忘记了,你不存在用户。
下面命令改下密码 USE mysql;CREATE USER 'root'@'%' IDENTIFIED BY '密码';GRANT ALL PRIVILEGES ON *.* TO 'root'@'%';FLUSH PRIVILEGES; |
30
billlee 2016-08-07 21:50:28 +08:00
你是不是把 hosts 里面的 127.0.0.1 localhost 去掉了?
|
31
extreme 2016-08-07 22:00:07 +08:00
@fourstring 刚刚干其他事情去了
@kn007 真热情呢。 你连接数据库用的应该是 localhost ?配置文件有 bind-address 127.0.0.1 ,对 MySQL 来说来源 IP 应该是 127.0.0.1 了,那么 MySQL 会反解出 127.0.0.1 的 hostname ,不过解析出了你的 hostname fourstring 的确有点怪,按道理应该根据 /etc/hosts 的 127.0.0.1 的记录来解析的。 这问题值得研究…… |
32
fourstring OP @extreme 对啊,我自己都觉得很奇怪。。。
另外有人跟我说他的 percona 也出过一次各种地方毫无错误但是就是连不上启动不了的错误…… |
33
fourstring OP @kn007 好了,非常感谢!
|
34
kn007 2016-08-07 22:07:54 +08:00
|
35
kn007 2016-08-07 22:13:23 +08:00
|
36
kn007 2016-08-07 22:15:55 +08:00
还有一个可能就是 iptables 把 lo 流量禁止了,或把 3306 端口流量给 eth0 了。。。
|
37
kn007 2016-08-07 22:19:26 +08:00
我记得 ubuntu 和 debian 有个坑就是 lo 木有,要手动开。。。
还是 centos 好。。。 |
38
fourstring OP @kn007 lo 是有的,这个我确认
|
39
fourstring OP @kn007 可能原因是我开了 tincd ,怪不得最近这几天才挂……
|
40
julyclyde 2016-08-08 17:55:52 +08:00
mysql 语境下, localhost 是一个神奇的词,意思是通过 unix domain socket 连接,而不经过网络
|
41
davehandong 2016-08-08 18:13:23 +08:00
配置文件 /etc/mysql/my.cnf 里的 bind-address 值,改成 0.0.0.0 就好了
|
42
fourstring OP @julyclyde 对,这个我知道。然而我在 php.ini 里配置了 default-socket 也无果
|
43
julyclyde 2016-08-08 22:23:45 +08:00
@fourstring 你得确认你改的那个 php.ini 是你运行的那套 PHP 承认的那个 ini 。先看看 phpinfo 输出结果对不对
|