If you encounter problems when you try to connect to the MySQL server, the following items describe some courses of action you can take to correct the problem.如果你在尝试连接MySQL服务器时遇到问题,以下项目描述了你可以采取的一些行动来纠正问题。
Make sure that the server is running. If it is not, clients cannot connect to it. For example, if an attempt to connect to the server fails with a message such as one of those following, one cause might be that the server is not running:确保服务器正在运行。如果不是,则客户端无法连接到它。例如,如果尝试连接到服务器失败,并显示以下消息之一,则一个原因可能是服务器未运行:
shell>mysql
ERROR 2003: Can't connect to MySQL server on 'host_name
' (111) shell>mysql
ERROR 2002: Can't connect to local MySQL server through socket '/tmp/mysql.sock' (111)
It might be that the server is running, but you are trying to connect using a TCP/IP port, named pipe, or Unix socket file different from the one on which the server is listening. 可能是服务器正在运行,但您正试图使用与服务器正在侦听的端口不同的TCP/IP端口、命名管道或Unix套接字文件进行连接。To correct this when you invoke a client program, specify a 要在调用客户端程序时纠正此问题,请指定--port
option to indicate the proper port number, or a --socket
option to indicate the proper named pipe or Unix socket file. --port
选项以指示正确的端口号,或指定--socket
选项以指示适当的命名管道或Unix套接字文件。To find out where the socket file is, you can use this command:要找出套接字文件的位置,可以使用以下命令:
shell> netstat -ln | grep mysql
Make sure that the server has not been configured to ignore network connections or (if you are attempting to connect remotely) that it has not been configured to listen only locally on its network interfaces. 确保服务器未配置为忽略网络连接,或者(如果您尝试远程连接)未配置为仅在其网络接口上本地侦听。If the server was started with the 如果服务器在启动时启用了skip_networking
system variable enabled, no TCP/IP connections are accepted. skip_nonetworking
系统变量,则不接受TCP/IP连接。If the server was started with the 如果服务器启动时bind_address
system variable set to 127.0.0.1
, it listens for TCP/IP connections only locally on the loopback interface and does not accept remote connections.bind_address
系统变量设置为127.0.0.1
,则它仅在环回接口上本地侦听TCP/IP连接,不接受远程连接。
Check to make sure that there is no firewall blocking access to MySQL. 检查以确保没有防火墙阻止对MySQL的访问。Your firewall may be configured on the basis of the application being executed, or the port number used by MySQL for communication (3306 by default). 您的防火墙可能会根据正在执行的应用程序或MySQL用于通信的端口号(默认为3306)进行配置。Under Linux or Unix, check your IP tables (or similar) configuration to ensure that the port has not been blocked. 在Linux或Unix下,检查您的IP表(或类似)配置,以确保端口未被阻止。Under Windows, applications such as ZoneAlarm or Windows Firewall may need to be configured not to block the MySQL port.在Windows下,可能需要将ZoneAlarm或Windows防火墙等应用程序配置为不阻止MySQL端口。
The grant tables must be properly set up so that the server can use them for access control. 必须正确设置授权表,以便服务器可以使用它们进行访问控制。For some distribution types (such as binary distributions on Windows, or RPM and DEB distributions on Linux), the installation process initializes the MySQL data directory, including the 对于某些分发类型(如Windows上的二进制分发,或Linux上的RPM和DEB分发),安装过程会初始化MySQL数据目录,包括包含授权表的mysql
system database containing the grant tables. mysql
系统数据库。For distributions that do not do this, you must initialize the data directory manually. For details, see Section 2.10, “Postinstallation Setup and Testing”.对于不这样做的发行版,您必须手动初始化数据目录。有关详细信息,请参阅第2.10节,“安装后设置和测试”。
To determine whether you need to initialize the grant tables, look for a 要确定是否需要初始化授权表,请在data目录下查找mysql
directory under the data directory. mysql
目录。(The data directory normally is named (数据目录通常命名为data
or var
and is located under your MySQL installation directory.) data
或var
,位于MySQL安装目录下。)Make sure that you have a file named 确保user.MYD
in the mysql
database directory. If not, initialize the data directory. mysql
数据库目录中有一个名为user.MYD
的文件。如果没有,请初始化数据目录。After doing so and starting the server, you should be able to connect to the server.这样做并启动服务器后,您应该能够连接到服务器。
After a fresh installation, if you try to log on to the server as 新安装后,如果您尝试以root
without using a password, you might get the following error message.root
身份登录服务器而不使用密码,则可能会收到以下错误消息。
shell> mysql -u root
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
It means a root password has already been assigned during installation and it has to be supplied. 这意味着在安装过程中已经分配了根密码,必须提供。See Section 2.10.4, “Securing the Initial MySQL Account” on the different ways the password could have been assigned and, in some cases, how to find it. 请参阅第2.10.4节,“保护初始MySQL帐户”,了解分配密码的不同方式,以及在某些情况下如何找到密码。If you need to reset the root password, see instructions in Section B.3.3.2, “How to Reset the Root Password”. 如果需要重置根密码,请参阅第B.3.3.2节,“如何重置根密码”中的说明。After you have found or reset your password, log on again as 找到或重置密码后,使用root
using the --password
(or -p
) option:--password
(或-p
)选项以root
身份再次登录:
shell> mysql -u root -p
Enter password:
However, the server is going to let you connect as 但是,如果您使用root
without using a password if you have initialized MySQL using mysqld --initialize-insecure (see Section 2.10.1, “Initializing the Data Directory” for details). mysqld --initialize-insecure
初始化MySQL,服务器将允许您以root
身份连接,而无需使用密码(有关详细信息,请参阅第2.10.1节,“初始化数据目录”)。That is a security risk, so you should set a password for the 这是一个安全风险,因此您应该为root帐户设置密码;有关说明,请参阅第2.10.4节,“保护初始MySQL帐户”。root
account; see Section 2.10.4, “Securing the Initial MySQL Account” for instructions.
If you have updated an existing MySQL installation to a newer version, did you perform the MySQL upgrade procedure? If not, do so. 如果您已将现有MySQL安装更新到较新版本,您是否执行了MySQL升级过程?如果没有,就这样做。The structure of the grant tables changes occasionally when new capabilities are added, so after an upgrade you should always make sure that your tables have the current structure. 当添加新功能时,授权表的结构偶尔会发生变化,因此在升级后,您应该始终确保您的表具有当前的结构。For instructions, see Section 2.11, “Upgrading MySQL”.有关说明,请参阅第2.11节,“升级MySQL”。
If a client program receives the following error message when it tries to connect, it means that the server expects passwords in a newer format than the client is capable of generating:如果客户端程序在尝试连接时收到以下错误消息,则意味着服务器需要比客户端能够生成的格式更新的密码:
shell> mysql
Client does not support authentication protocol requested
by server; consider upgrading MySQL client
Remember that client programs use connection parameters specified in option files or environment variables. If a client program seems to be sending incorrect default connection parameters when you have not specified them on the command line, check any applicable option files and your environment. 请记住,客户端程序使用选项文件或环境变量中指定的连接参数。如果客户端程序在您未在命令行上指定默认连接参数时似乎发送了不正确的默认连接参数,请检查任何适用的选项文件和您的环境。For example, if you get 例如,如果在没有任何选项的情况下运行客户端时访问被拒绝,请确保您没有在任何选项文件中指定旧密码!Access denied
when you run a client without any options, make sure that you have not specified an old password in any of your option files!
You can suppress the use of option files by a client program by invoking it with the 您可以通过使用--no-defaults
option. For example:--no-defaults
选项调用客户端程序来抑制其使用选项文件。例如:
shell> mysqladmin --no-defaults -u root version
The option files that clients use are listed in Section 4.2.2.2, “Using Option Files”. Environment variables are listed in Section 4.9, “Environment Variables”.客户使用的选项文件列在第4.2.2.2节,“使用选项文件”中。第4.9节,“环境变量”列出了环境变量。
If you get the following error, it means that you are using an incorrect 如果您收到以下错误,则表示您使用的root
password:root
密码不正确:
shell> mysqladmin -u root -pxxxx
ver
Access denied for user 'root'@'localhost' (using password: YES)
If the preceding error occurs even when you have not specified a password, it means that you have an incorrect password listed in some option file. 如果即使您没有指定密码,也会出现上述错误,这意味着您在某些选项文件中列出了不正确的密码。Try the 如前一项所述,尝试使用--no-defaults
option as described in the previous item.--no-defaults
选项。
For information on changing passwords, see Section 6.2.14, “Assigning Account Passwords”.有关更改密码的信息,请参阅第6.2.14节,“分配帐户密码”。
If you have lost or forgotten the 如果您丢失或忘记了root
password, see Section B.3.3.2, “How to Reset the Root Password”.root
密码,请参阅第B.3.3.2节,“如何重置根密码”。
localhost
is a synonym for your local host name, and is also the default host to which clients try to connect if you specify no host explicitly.localhost
是本地主机名的同义词,如果您没有明确指定主机,它也是客户端尝试连接的默认主机。
You can use a 您可以使用--host=127.0.0.1
option to name the server host explicitly. --host=127.0.0.1
选项显式命名服务器主机。This causes a TCP/IP connection to the local mysqld server. 这会导致与本地mysqld服务器的TCP/IP连接。You can also use TCP/IP by specifying a 您还可以通过指定使用本地主机实际主机名的--host
option that uses the actual host name of the local host. --host
选项来使用TCP/IP。In this case, the host name must be specified in a 在这种情况下,即使您在与服务器相同的主机上运行客户端程序,也必须在服务器主机上的user
table row on the server host, even though you are running the client program on the same host as the server.user
表行中指定主机名。
The Access denied
error message tells you who you are trying to log in as, the client host from which you are trying to connect, and whether you were using a password. Access denied
(拒绝访问)错误消息告诉您试图以谁身份登录,您试图连接的客户端主机,以及您是否使用了密码。Normally, you should have one row in the 通常,user
table that exactly matches the host name and user name that were given in the error message. user
表中应该有一行与错误消息中给出的主机名和用户名完全匹配。For example, if you get an error message that contains 例如,如果您收到一条错误消息,其中包含using password: NO
, it means that you tried to log in without a password.using password: NO
,则表示您试图在没有密码的情况下登录。
If you get an 如果在尝试使用Access denied
error when trying to connect to the database with mysql -u
, you may have a problem with the user_name
user
table. mysql-u user_name
连接到数据库时出现拒绝访问错误,则user
表可能有问题。Check this by executing 通过执行mysql -u root mysql
and issuing this SQL statement:mysql-u root mysql
并发出以下SQL语句来检查这一点:
SELECT * FROM user;
The result should include a row with the 结果应包含一行,其中Host
and User
columns matching your client's host name and your MySQL user name.Host
和User
列与客户端的主机名和MySQL用户名匹配。
If the following error occurs when you try to connect from a host other than the one on which the MySQL server is running, it means that there is no row in the 如果当您尝试从MySQL服务器运行的主机以外的主机连接时发生以下错误,则意味着user
table with a Host
value that matches the client host:user
表中没有与客户端主机匹配的Host
值的行:
Host ... is not allowed to connect to this MySQL server
You can fix this by setting up an account for the combination of client host name and user name that you are using when trying to connect.您可以通过为尝试连接时使用的客户端主机名和用户名的组合设置帐户来解决此问题。
If you do not know the IP address or host name of the machine from which you are connecting, you should put a row with 如果您不知道要连接的计算机的IP地址或主机名,则应在'%'
as the Host
column value in the user
table. user
表中添加一行'%'
作为Host
列值。After trying to connect from the client machine, use a 在尝试从客户端计算机连接后,使用SELECT USER()
query to see how you really did connect. SELECT USER()
查询查看实际连接方式。Then change the 然后将'%'
in the user
table row to the actual host name that shows up in the log. user
表行中的'%'
更改为日志中显示的实际主机名。Otherwise, your system is left insecure because it permits connections from any host for the given user name.否则,您的系统将变得不安全,因为它允许来自任何主机的给定用户名的连接。
On Linux, another reason that this error might occur is that you are using a binary MySQL version that is compiled with a different version of the 在Linux上,可能发生此错误的另一个原因是,您使用的是二进制MySQL版本,该版本是用与您使用的版本不同的glibc
library than the one you are using. glibc
库编译的。In this case, you should either upgrade your operating system or 在这种情况下,您应该升级您的操作系统或glibc
, or download a source distribution of MySQL version and compile it yourself. glibc
,或者下载MySQL版本的源代码分发并自己编译。A source RPM is normally trivial to compile and install, so this is not a big problem.源RPM的编译和安装通常很简单,所以这不是一个大问题。
If you specify a host name when trying to connect, but get an error message where the host name is not shown or is an IP address, it means that the MySQL server got an error when trying to resolve the IP address of the client host to a name:如果您在尝试连接时指定了主机名,但收到一条错误消息,其中主机名未显示或是IP地址,则意味着MySQL服务器在尝试将客户端主机的IP地址解析为名称时出错:
shell> mysqladmin -u root -pxxxx
-h some_hostname
ver
Access denied for user 'root'@'' (using password: YES)
If you try to connect as 如果您尝试以root
and get the following error, it means that you do not have a row in the user
table with a User
column value of 'root'
and that mysqld cannot resolve the host name for your client:root
身份连接并得到以下错误,则意味着您的user
表中没有User
列值为'root'
的行,并且mysqld无法解析客户端的主机名:
Access denied for user ''@'unknown'
These errors indicate a DNS problem. To fix it, execute mysqladmin flush-hosts to reset the internal DNS host cache. 这些错误表明存在DNS问题。要修复此问题,请执行mysqladmin flush-hosts以重置内部DNS主机缓存。See Section 5.1.12.3, “DNS Lookups and the Host Cache”.请参阅第5.1.12.3节,“DNS查找和主机缓存”。
Some permanent solutions are:一些永久性的解决方案是:
Determine what is wrong with your DNS server and fix it.确定DNS服务器的问题并修复它。
Specify IP addresses rather than host names in the MySQL grant tables.在MySQL授权表中指定IP地址而不是主机名。
Put an entry for the client machine name in 在Unix上的/etc/hosts
on Unix or \windows\hosts
on Windows./etc/hosts
或windows上的\windows\hosts
中输入客户端机器名。
Start mysqld with the 在启用skip_name_resolve
system variable enabled.skip_name_resolve
系统变量的情况下启动mysqld。
Start mysqld with the 使用--skip-host-cache
option.--skip-host-cache
选项启动mysqld。
On Unix, if you are running the server and the client on the same machine, connect to 在Unix上,如果您在同一台机器上运行服务器和客户端,请连接到localhost
. localhost
。For connections to 对于连接到localhost
, MySQL programs attempt to connect to the local server by using a Unix socket file, unless there are connection parameters specified to ensure that the client makes a TCP/IP connection. localhost
,MySQL程序会尝试使用Unix套接字文件连接到本地服务器,除非指定了连接参数以确保客户端进行TCP/IP连接。For more information, see Section 4.2.4, “Connecting to the MySQL Server Using Command Options”.有关更多信息,请参阅第4.2.4节,“使用命令选项连接到MySQL服务器”。
On Windows, if you are running the server and the client on the same machine and the server supports named pipe connections, connect to the host name 在Windows上,如果您在同一台计算机上运行服务器和客户端,并且服务器支持命名管道连接,请连接到主机名.
(period). .
(句点)。Connections to 连接到。使用命名管道而不是TCP/IP。.
use a named pipe rather than TCP/IP.
If 如果mysql -u root
works but mysql -h
results in your_hostname
-u rootAccess denied
(where your_hostname
is the actual host name of the local host), you may not have the correct name for your host in the user
table. mysql -u root
有效,但mysql -h your_hostname -u root
导致访问被拒绝(其中your_hostname
是本地主机的实际主机名),则user
表中可能没有正确的主机名。A common problem here is that the 这里的一个常见问题是,Host
value in the user
table row specifies an unqualified host name, but your system's name resolution routines return a fully qualified domain name (or vice versa). user
表行中的Host
值指定了一个不合格的主机名,但系统的名称解析例程返回了一个完全合格的域名(反之亦然)。For example, if you have a row with host 例如,如果'pluto'
in the user
table, but your DNS tells MySQL that your host name is 'pluto.example.com'
, the row does not work. user
表中有一行的主机为'pluto'
,但您的DNS服务器告诉MySQL您的主机名是'pluto.example.com'
,则该行无效。Try adding a row to the 尝试在user
table that contains the IP address of your host as the Host
column value. user
表中添加一行,其中包含主机的IP地址作为Host
列值。(Alternatively, you could add a row to the (或者,您可以向user
table with a Host
value that contains a wildcard (for example, 'pluto.%'
). user
表中添加一行包含通配符的Host
值(例如'pluto.%'
)。However, use of 但是,使用以Host
values ending with %
is insecure and is not recommended!)%
结尾的Host
值是不安全的,不建议使用!)
If 如果mysql -u
works but user_name
mysql -u
does not, you have not granted access to the given user for the database named user_name
some_db
some_db
.mysql -u user_name
有效,但mysql -u user_name some_db
无效,则表示您没有授予给定用户访问名为some_db
的数据库的权限。
If 如果mysql -u
works when executed on the server host, but user_name
mysql -h
does not work when executed on a remote client host, you have not enabled access to the server for the given user name from the remote host.host_name
-u user_name
mysql -u user_name
在服务器主机上执行时有效,但mysql -h host_name -u user_name
在远程客户端主机上运行时不起作用,则您没有从远程主机为给定的用户名启用对服务器的访问。
If you cannot figure out why you get 如果无法找出Access denied
, remove from the user
table all rows that have Host
values containing wildcards (rows that contain '%'
or '_'
characters). Access denied
的原因,请从user
表中删除所有Host
值包含通配符的行(包含'%'
或'_'
字符的行)。A very common error is to insert a new row with 一个非常常见的错误是插入Host
='%'
and User
='
, thinking that this enables you to specify some_user
'localhost
to connect from the same machine. Host='%'
和User='some_user'
的新行,认为这使您能够指定localhost
从同一台机器连接。The reason that this does not work is that the default privileges include a row with 这不起作用的原因是默认权限包括Host
='localhost'
and User
=''
. Host='localhost'
和User=''
的行。Because that row has a 因为该行的Host
value 'localhost'
that is more specific than '%'
, it is used in preference to the new row when connecting from localhost
! Host
值'localhost'
比'%'
更具体,所以从localhost
连接时,它优先于新行使用!The correct procedure is to insert a second row with 正确的步骤是插入第二行Host
='localhost'
and User
='
, or to delete the row with some_user
'Host
='localhost'
and User
=''
. Host='localhost'和User='some_User'
,或者删除Host='localhost'
和User=''
的行。After deleting the row, remember to issue a 删除行后,记得发出FLUSH PRIVILEGES
statement to reload the grant tables. FLUSH PRIVILES
语句以重新加载授权表。See also Section 6.2.6, “Access Control, Stage 1: Connection Verification”.另请参阅第6.2.6节,“访问控制,第1阶段:连接验证”。
If you are able to connect to the MySQL server, but get an 如果您能够连接到MySQL服务器,但每次发出Access denied
message whenever you issue a SELECT ... INTO OUTFILE
or LOAD DATA
statement, your row in the user
table does not have the FILE
privilege enabled.SELECT ... INTO OUTFILE
或LOAD DATA
语句时都会收到Access denied
(拒绝访问)消息,则user
表中的行未启用FILE
权限。
If you change the grant tables directly (for example, by using 如果您直接更改授权表(例如,使用INSERT
, UPDATE
, or DELETE
statements) and your changes seem to be ignored, remember that you must execute a FLUSH PRIVILEGES
statement or a mysqladmin flush-privileges command to cause the server to reload the privilege tables. INSERT
、UPDATE
或DELETE
语句),并且您的更改似乎被忽略了,请记住,您必须执行FLUSH PRIVILES
语句或mysqladmin flush-privileges命令,以使服务器重新加载权限表。Otherwise, your changes have no effect until the next time the server is restarted. 否则,在下次重新启动服务器之前,您的更改将无效。Remember that after you change the 请记住,使用root
password with an UPDATE
statement, you do not need to specify the new password until after you flush the privileges, because the server does not know until then that you have changed the password.UPDATE
语句更改root
密码后,在刷新权限之前,您不需要指定新密码,因为服务器在那之前不知道您更改了密码。
If your privileges seem to have changed in the middle of a session, it may be that a MySQL administrator has changed them. Reloading the grant tables affects new client connections, but it also affects existing connections as indicated in Section 6.2.13, “When Privilege Changes Take Effect”.如果您的权限似乎在会话过程中发生了更改,那么可能是MySQL管理员更改了权限。重新加载授权表会影响新的客户端连接,但也会影响第6.2.13节,“权限更改生效时”中所述的现有连接。
If you have access problems with a Perl, PHP, Python, or ODBC program, try to connect to the server with 如果您在访问Perl、PHP、Python或ODBC程序时遇到问题,请尝试使用mysql -u
or user_name
db_name
mysql -u
. user_name
-ppassword
db_name
mysql -u user_name db_name
或mysql -u user_name -ppassword db_name
连接到服务器。If you are able to connect using the mysql client, the problem lies with your program, not with the access privileges. 如果您能够使用mysql客户端进行连接,那么问题在于您的程序,而不是访问权限。(There is no space between (-p
and the password; you can also use the --password=
syntax to specify the password. password
-p
和密码之间没有空格;您还可以使用--password=password
语法指定密码。If you use the 如果您使用-p
or --password
option with no password value, MySQL prompts you for the password.)-p
或--password
选项而没有密码值,MySQL会提示您输入密码。)
For testing purposes, start the mysqld server with the 出于测试目的,使用--skip-grant-tables
option. --skip-grant-tables
选项启动mysqld服务器。Then you can change the MySQL grant tables and use the 然后,您可以更改MySQL授权表,并使用SHOW GRANTS
statement to check whether your modifications have the desired effect. SHOW GRANTS
语句检查您的修改是否具有所需的效果。When you are satisfied with your changes, execute mysqladmin flush-privileges to tell the mysqld server to reload the privileges. 当您对更改感到满意时,执行mysqladmin flush-privileges以告诉mysqld服务器重新加载权限。This enables you to begin using the new grant table contents without stopping and restarting the server.这使您能够开始使用新的授权表内容,而无需停止和重新启动服务器。
If everything else fails, start the mysqld server with a debugging option (for example, 如果其他一切都失败了,请使用调试选项启动mysqld服务器(例如,--debug=d,general,query
). --debug=d,general,query
)。This prints host and user information about attempted connections, as well as information about each command issued. See Section 5.9.4, “The DBUG Package”.这将打印有关尝试连接的主机和用户信息,以及有关发出的每个命令的信息。参阅第5.9.4节,“DBUG包”。
If you have any other problems with the MySQL grant tables and ask on the MySQL Community Slack, always provide a dump of the MySQL grant tables. 如果您对MySQL授权表有任何其他问题,并在MySQL社区Slack上询问,请始终提供MySQL授权表的转储。You can dump the tables with the mysqldump mysql command. 您可以使用mysqldump -mysql命令转储表。To file a bug report, see the instructions at Section 1.6, “How to Report Bugs or Problems”. 要提交错误报告,请参阅第1.6节,“如何报告错误或问题”中的说明。In some cases, you may need to restart mysqld with 在某些情况下,您可能需要使用--skip-grant-tables
to run mysqldump.--skip-grant-tables
重新启动mysqld来运行mysqldump。