如果在尝试连接到 MySQL 服务器时遇到问题,以下内容描述了一些可以采取的措施来解决问题。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.
确保服务器正在运行。如果服务器未运行,客户端将无法连接。例如,如果尝试连接服务器失败,并出现如下消息之一,一个可能的原因是服务器未运行: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:
$>
mysqlERROR 2003: Can't connect to MySQL server on 'host_name' (111) $>mysqlERROR 2002: Can't connect to local MySQL server through socket '/tmp/mysql.sock' (111)可能服务器正在运行,但您尝试使用的 TCP/IP 端口、命名管道或 Unix 套接字文件与服务器正在监听的不同。要纠正此问题,在调用客户端程序时,请指定
--port选项以指示正确的端口号,或指定--socket选项以指示正确的命名管道或 Unix 套接字文件。要查找套接字文件的位置,可以使用以下命令: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. To correct this when you invoke a client program, specify a--portoption to indicate the proper port number, or a--socketoption to indicate the proper named pipe or Unix socket file. To find out where the socket file is, you can use this command:$>
netstat -ln | grep mysql确保服务器未被配置为忽略网络连接,或者(如果您尝试远程连接)未被配置为仅在其网络接口上本地监听。如果服务器是使用启用的
skip_networking系统变量启动的,则不接受任何 TCP/IP 连接。如果服务器是使用设置为127.0.0.1的bind_address系统变量启动的,它只在环回接口本地监听 TCP/IP 连接,并且不接受远程连接。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 theskip_networkingsystem variable enabled, no TCP/IP connections are accepted. If the server was started with thebind_addresssystem variable set to127.0.0.1, it listens for TCP/IP connections only locally on the loopback interface and does not accept remote connections.检查确保没有防火墙阻止对 MySQL 的访问。您的防火墙可能是根据正在执行的应用程序或 MySQL 用于通信的端口号(默认是 3306)配置的。在 Linux 或 Unix 下,检查您的 IP 表(或类似)配置以确保端口没有被阻止。在 Windows 下,可能需要配置 ZoneAlarm 或 Windows 防火墙等应用程序,不要阻止 MySQL 端口。Check to make sure that there is no firewall blocking access to 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). Under Linux or Unix, check your IP tables (or similar) configuration to ensure that the port has not been blocked. Under Windows, applications such as ZoneAlarm or Windows Firewall may need to be configured not to block the MySQL port.
授权表必须正确设置,以便服务器可以使用它们进行访问控制。对于某些发行版类型(例如 Windows 上的二进制发行版,或 Linux 上的 RPM 和 DEB 发行版),安装过程会初始化 MySQL 数据目录,包括包含授权表的
mysql系统数据库。对于没有执行此操作的发行版,必须手动初始化数据目录。有关详细信息,请参见 第 2.9 节,“安装后设置和测试”。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 themysqlsystem database containing the grant tables. For distributions that do not do this, you must initialize the data directory manually. For details, see Section 2.9, “Postinstallation Setup and Testing”.要确定是否需要初始化权限表,请检查数据目录下是否有一个
mysql目录。(数据目录通常命名为data或var,位于 MySQL 安装目录下。)确保在mysql数据库目录中有一个名为user.MYD的文件。如果没有,请初始化数据目录。完成此操作并启动服务器后,您应该能够连接到服务器。To determine whether you need to initialize the grant tables, look for amysqldirectory under the data directory. (The data directory normally is nameddataorvarand is located under your MySQL installation directory.) Make sure that you have a file nameduser.MYDin themysqldatabase directory. If not, initialize the data directory. After doing so and starting the server, you should be able to connect to the server.在全新安装后,如果您尝试作为
root登录服务器而不使用密码,可能会收到以下错误消息。After a fresh installation, if you try to log on to the server asrootwithout using a password, you might get the following error message.$>
mysql -u rootERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)这意味着在安装过程中已经分配了 root 密码,并且必须提供该密码。请参阅 第 2.9.4 节,“保护初始 MySQL 帐户”,了解密码可能分配的不同方式,以及在某些情况下如何找到密码。如果您需要重置 root 密码,请参阅 第 B.3.3.2 节,“如何重置 root 密码” 中的说明。在找到或重置密码后,请使用
root重新登录,并使用--password(或-p)选项:It means a root password has already been assigned during installation and it has to be supplied. See Section 2.9.4, “Securing the Initial MySQL Account” on the different ways the password could have been assigned and, in some cases, how to find it. If you need to reset the root password, see instructions in Section B.3.3.2, “How to Reset the Root Password”. After you have found or reset your password, log on again asrootusing the--password(or-p) option:$>
mysql -u root -pEnter password:但是,如果您使用 mysqld --initialize-insecure 初始化了 MySQL,服务器将允许您以
root身份连接而无需使用密码(详情请参见 第 2.9.1 节,“初始化数据目录”)。这是一个安全风险,因此您应该为root账户设置密码;具体说明请参见 第 2.9.4 节,“保护初始 MySQL 账户”。However, the server is going to let you connect asrootwithout using a password if you have initialized MySQL using mysqld --initialize-insecure (see Section 2.9.1, “Initializing the Data Directory” for details). That is a security risk, so you should set a password for therootaccount; see Section 2.9.4, “Securing the Initial MySQL Account” for instructions.如果您已将现有的 MySQL 安装更新到较新版本,是否执行了 MySQL 升级程序?如果没有,请执行升级程序。当增加新功能时,授权表的结构偶尔会发生变化,因此升级后应始终确保表具有当前的结构。有关说明,请参见 第 3 章,升级 MySQL。If you have updated an existing MySQL installation to a newer version, did you perform the MySQL upgrade procedure? If not, do so. 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 Chapter 3, Upgrading 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:
$>
mysqlClient does not support authentication protocol requested by server; consider upgrading MySQL client请记住,客户端程序使用选项文件或环境变量中指定的连接参数。如果客户端程序在命令行中未指定连接参数时似乎发送了错误的默认连接参数,请检查任何相关的选项文件和环境。例如,如果在运行客户端而不带任何选项时收到
Access denied错误,请确保没有在任何选项文件中指定过旧的密码! 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 getAccess deniedwhen you run a client without any options, make sure that you have not specified an old password in any of your option files!您可以通过使用
--no-defaults选项来禁止客户端程序使用选项文件。例如:You can suppress the use of option files by a client program by invoking it with the--no-defaultsoption. For example:$>
mysqladmin --no-defaults -u root version客户端使用的选项文件列在第 6.2.2.2 节,“使用选项文件”中。环境变量列在第 6.9 节,“环境变量”中。The option files that clients use are listed in Section 6.2.2.2, “Using Option Files”. Environment variables are listed in Section 6.9, “Environment Variables”.
如果出现以下错误,这意味着您使用了错误的
root密码:If you get the following error, it means that you are using an incorrectrootpassword:$>
mysqladmin -u root -pAccess denied for user 'root'@'localhost' (using password: YES)xxxxver如果即使您没有指定密码也出现上述错误,这意味着您在某些选项文件中列出了错误的密码。请尝试使用前一项中描述的
--no-defaults选项。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-defaultsoption as described in the previous item.有关更改密码的信息,请参见 第 8.2.14 节,“分配账户密码”。For information on changing passwords, see Section 8.2.14, “Assigning Account Passwords”.
如果您丢失或忘记了
root密码,请参见 第 B.3.3.2 节,“如何重置 Root 密码”。If you have lost or forgotten therootpassword, see Section B.3.3.2, “How to Reset the Root Password”.localhost是您本地主机名的同义词,也是客户端在未明确指定主机时尝试连接的默认主机。localhostis 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.您可以使用
--host=127.0.0.1选项来明确指定服务器主机名。这会导致与本地 mysqld 服务器的 TCP/IP 连接。您还可以通过指定使用本地主机实际主机名的--host选项来使用 TCP/IP。在这种情况下,即使您在与服务器相同的主机上运行客户端程序,也必须在服务器主机上的user表行中指定主机名。You can use a--host=127.0.0.1option to name the server host explicitly. This causes a TCP/IP connection to the local mysqld server. You can also use TCP/IP by specifying a--hostoption that uses the actual host name of the local host. In this case, the host name must be specified in ausertable row on the server host, even though you are running the client program on the same host as the server.Access denied错误信息会告诉你尝试以哪个用户名登录、尝试连接的客户端主机,以及你是否使用了密码。通常,你在user表中应该有一行与错误信息中给出的主机名和用户名完全匹配。例如,如果你收到包含using password: NO的错误信息,这意味着你尝试在没有密码的情况下登录。TheAccess deniederror 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. Normally, you should have one row in theusertable that exactly matches the host name and user name that were given in the error message. For example, if you get an error message that containsusing password: NO, it means that you tried to log in without a password.如果在尝试使用
Access denied连接数据库时收到mysql -u user_name错误,可能是user表存在问题。可以通过执行mysql -u root mysql并运行以下 SQL 语句来检查:If you get anAccess deniederror when trying to connect to the database withmysql -u, you may have a problem with theuser_nameusertable. Check this by executingmysql -u root mysqland issuing this SQL statement:SELECT * FROM user;
结果应包含一行,其
Host和User列与您的客户端主机名和 MySQL 用户名匹配。The result should include a row with theHostandUsercolumns matching your client's host name and your MySQL user name.如果在尝试从 MySQL 服务器所在主机以外的主机连接时出现以下错误,这意味着
user表中没有Host值与客户端主机匹配的行: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 theusertable with aHostvalue that matches the client 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.
如果您不知道连接机器的 IP 地址或主机名,您应该在
user表中插入一行,Host列的值为'%'。在尝试从客户端机器连接之后,使用SELECT USER()查询查看您实际上是如何连接的。然后将user表中那一行的'%'更改为日志中显示的实际主机名。否则,因为允许任意主机使用该用户名连接,您的系统将处于不安全状态。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'%'as theHostcolumn value in theusertable. After trying to connect from the client machine, use aSELECT USER()query to see how you really did connect. Then change the'%'in theusertable row to the actual host name that shows up in the log. Otherwise, your system is left insecure because it permits connections from any host for the given user name.在 Linux 上,这个错误可能出现的另一个原因是你使用的二进制 MySQL 版本是用与你当前使用的不同版本的
glibc库编译的。在这种情况下,你应该升级操作系统或glibc,或者下载 MySQL 的源代码版本并自行编译。源代码 RPM 通常很容易编译和安装,所以这不是大问题。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 theglibclibrary than the one you are using. In this case, you should either upgrade your operating system orglibc, or download a source distribution of MySQL version and compile it yourself. A source RPM is normally trivial to compile and install, so this is not a big problem.如果在尝试连接时指定了主机名,但收到的错误消息中未显示主机名或显示的是 IP 地址,这意味着 MySQL 服务器在尝试将客户端主机的 IP 地址解析为名称时遇到了错误: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:
$>
mysqladmin -u root -pAccess denied for user 'root'@'' (using password: YES)xxxx-hsome_hostnamever如果你尝试以
root用户连接并收到以下错误,这意味着你在user表中没有User列值为'root'的行,并且 mysqld 无法解析你的客户端主机名:If you try to connect asrootand get the following error, it means that you do not have a row in theusertable with aUsercolumn value of'root'and that mysqld cannot resolve the host name for your client:Access denied for user ''@'unknown'
这些错误表明是 DNS 问题。要解决它,请执行 mysqladmin flush-hosts 来重置内部 DNS 主机缓存。参见 第 7.1.12.3 节,“DNS 查找和主机缓存”。These errors indicate a DNS problem. To fix it, execute mysqladmin flush-hosts to reset the internal DNS host cache. See Section 7.1.12.3, “DNS Lookups and the Host Cache”.
一些永久性的解决方案包括:Some permanent solutions are:
确定你的 DNS 服务器出了什么问题并修复它。Determine what is wrong with your DNS server and fix it.
在 MySQL 授权表中指定 IP 地址而不是主机名。Specify IP addresses rather than host names in the MySQL grant tables.
在 Unix 系统的
/etc/hosts或 Windows 系统的\windows\hosts中为客户端机器名添加一条记录。Put an entry for the client machine name in/etc/hostson Unix or\windows\hostson Windows.启动 mysqld,启用
skip_name_resolve系统变量。Start mysqld with theskip_name_resolvesystem variable enabled.启动 mysqld,并使用
--host-cache-size=0。Start mysqld with--host-cache-size=0.在 Unix 上,如果你在同一台机器上运行服务器和客户端,请连接到
localhost。对于连接到localhost的情况,MySQL 程序会尝试通过 Unix 套接字文件连接到本地服务器,除非指定了确保客户端进行 TCP/IP 连接的连接参数。有关更多信息,请参阅 第 6.2.4 节,“使用命令选项连接到 MySQL 服务器”。On Unix, if you are running the server and the client on the same machine, connect tolocalhost. For connections tolocalhost, 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. For more information, see Section 6.2.4, “Connecting to the MySQL Server Using Command Options”.在 Windows 上,如果你在同一台机器上运行服务器和客户端,并且服务器支持命名管道连接,请连接到主机名
.(点号)。连接到.使用命名管道而不是 TCP/IP。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.(period). Connections to.use a named pipe rather than TCP/IP.
If
mysql -u rootworks butmysql -hresults inyour_hostname-u rootAccess denied(whereyour_hostnameis the actual host name of the local host), you may not have the correct name for your host in theusertable. A common problem here is that theHostvalue in theusertable row specifies an unqualified host name, but your system's name resolution routines return a fully qualified domain name (or vice versa). For example, if you have a row with host'pluto'in theusertable, but your DNS tells MySQL that your host name is'pluto.example.com', the row does not work. Try adding a row to theusertable that contains the IP address of your host as theHostcolumn value. (Alternatively, you could add a row to theusertable with aHostvalue that contains a wildcard (for example,'pluto.%'). However, use ofHostvalues ending with%is insecure and is 在多线程系统中 recommended!)Ifmysql -u rootworks butmysql -hresults inyour_hostname-u rootAccess denied(whereyour_hostnameis the actual host name of the local host), you may not have the correct name for your host in theusertable. A common problem here is that theHostvalue in theusertable row specifies an unqualified host name, but your system's name resolution routines return a fully qualified domain name (or vice versa). For example, if you have a row with host'pluto'in theusertable, but your DNS tells MySQL that your host name is'pluto.example.com', the row does not work. Try adding a row to theusertable that contains the IP address of your host as theHostcolumn value. (Alternatively, you could add a row to theusertable with aHostvalue that contains a wildcard (for example,'pluto.%'). However, use ofHostvalues ending with%is insecure and is not recommended!)如果
mysql -u user_name可以工作,但mysql -u user_name some_db不行,则说明您尚未授予该用户对名为some_db的数据库的访问权限。Ifmysql -uworks butuser_namemysql -udoes not, you have not granted access to the given user for the database nameduser_namesome_dbsome_db.如果在服务器主机上执行
mysql -u user_name可以工作,但在远程客户端主机上执行mysql -h host_name -u user_name不行,则说明您尚未允许该用户名从远程主机访问服务器。Ifmysql -uworks when executed on the server host, butuser_namemysql -hdoes 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-uuser_name如果你无法弄清楚为什么会出现
Access denied错误,请从user表中删除所有Host值包含通配符的行(即包含'%'或'_'字符的行)。一个非常常见的错误是插入一行新记录,Host='%'且User=',以为这可以让你用some_user'localhost从同一台机器连接。之所以不行,是因为默认权限中包含一行Host='localhost'且User=''。因为该行的Host值'localhost'比'%'更具体,所以在从localhost连接时优先使用这行,而不是新插入的行!正确的做法是插入第二行,设Host='localhost'且User=',或者删除some_user'Host='localhost'且User=''的行。删除该行后,记得执行FLUSH PRIVILEGES语句以重新加载权限表。另请参见 第 8.2.6 节,“访问控制,第 1 阶段:连接验证”。If you cannot figure out why you getAccess denied, remove from theusertable all rows that haveHostvalues containing wildcards (rows that contain'%'or'_'characters). A very common error is to insert a new row withHost='%'andUser=', thinking that this enables you to specifysome_user'localhostto connect from the same machine. The reason that this does not work is that the default privileges include a row withHost='localhost'andUser=''. Because that row has aHostvalue'localhost'that is more specific than'%', it is used in preference to the new row when connecting fromlocalhost! The correct procedure is to insert a second row withHost='localhost'andUser=', or to delete the row withsome_user'Host='localhost'andUser=''. After deleting the row, remember to issue aFLUSH PRIVILEGESstatement to reload the grant tables. See also Section 8.2.6, “Access Control, Stage 1: Connection Verification”.如果您能够连接到 MySQL 服务器,但每当您执行
Access denied或LOAD DATA语句时都会收到SELECT ... INTO OUTFILE消息,则说明user表中的该行没有启用FILE权限。If you are able to connect to the MySQL server, but get anAccess deniedmessage whenever you issue aSELECT ... INTO OUTFILEorLOAD DATAstatement, your row in theusertable does not have theFILEprivilege enabled.如果您直接更改授权表(例如,通过使用
INSERT、UPDATE或DELETE语句),而且您的更改似乎没有生效,请记住,您必须执行FLUSH PRIVILEGES语句或 mysqladmin flush-privileges 命令以使服务器重新加载权限表。否则,您的更改在下次服务器重启之前不会生效。请记住,当您使用UPDATE语句更改root密码后,在刷新权限之前,您无需指定新密码,因为服务器在此之前不知道您已经更改了密码。If you change the grant tables directly (for example, by usingINSERT,UPDATE, orDELETEstatements) and your changes seem to be ignored, remember that you must execute aFLUSH PRIVILEGESstatement or a mysqladmin flush-privileges command to cause the server to reload the privilege tables. Otherwise, your changes have no effect until the next time the server is restarted. Remember that after you change therootpassword with anUPDATEstatement, 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.如果您的权限在会话中途似乎发生了变化,这可能是因为 MySQL 管理员已经更改了这些权限。重新加载授权表会影响新的客户端连接,但也会影响现有连接,如第 8.2.13 节,“权限变更生效时间”所示。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 8.2.13, “When Privilege Changes Take Effect”.
如果您在使用 Perl、PHP、Python 或 ODBC 程序时遇到访问问题,请尝试使用
mysql -u user_name db_name或mysql -u user_name -ppassword db_name连接到服务器。如果您能够使用 mysql 客户端连接,则问题出在您的程序,而不是访问权限。(-p和密码之间没有空格;您也可以使用--password=语法来指定密码。如果您在password-p或--password选项中未提供密码值,MySQL 会提示您输入密码。)If you have access problems with a Perl, PHP, Python, or ODBC program, try to connect to the server withmysql -uoruser_namedb_namemysql -u. If you are able to connect using the mysql client, the problem lies with your program, not with the access privileges. (There is no space betweenuser_name-ppassworddb_name-pand the password; you can also use the--password=syntax to specify the password. If you use thepassword-por--passwordoption with no password value, MySQL prompts you for the password.)为了测试目的,请使用 mysqld 服务器启动时加上
--skip-grant-tables选项。然后,您可以更改 MySQL 授权表,并使用SHOW GRANTS语句检查您的修改是否达到预期效果。当您对更改满意后,执行 mysqladmin flush-privileges 命令通知 mysqld 服务器重新加载权限。这使您无需停止和重启服务器即可开始使用新的授权表内容。For testing purposes, start the mysqld server with the--skip-grant-tablesoption. Then you can change the MySQL grant tables and use theSHOW GRANTSstatement to check whether your modifications have the desired effect. When you are satisfied with your changes, execute mysqladmin flush-privileges to tell the mysqld server to reload the privileges. This enables you to begin using the new grant table contents without stopping and restarting the server.如果所有其他方法都失败,请使用调试选项启动 mysqld 服务器(例如,
--debug=d,general,query)。这会打印有关尝试连接的主机和用户信息,以及每个发出命令的信息。请参见 第 7.9.4 节,“DBUG 包”。If everything else fails, start the mysqld server with a debugging option (for example,--debug=d,general,query). This prints host and user information about attempted connections, as well as information about each command issued. See Section 7.9.4, “The DBUG Package”.如果您在 MySQL 授权表方面有任何其他问题,并在 MySQL Community Slack 上提问,请始终提供 MySQL 授权表的转储。您可以使用 mysqldump mysql 命令来转储表。要提交错误报告,请参见 第 1.6 节,“如何报告错误或问题” 的说明。在某些情况下,您可能需要使用
--skip-grant-tables重启 mysqld 以运行 mysqldump。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. You can dump the tables with the mysqldump mysql command. To file a bug report, see the instructions at Section 1.6, “How to Report Bugs or Problems”. In some cases, you may need to restart mysqld with--skip-grant-tablesto run mysqldump.