在站点迁移后建立数据库连接时出错?

时间:2017-07-26 19:57:30

标签: mysql wordpress database-migration

我已将wordpress网站移至其他托管服务,并在打开网站时收到以下错误消息:

Error establishing a database connection
This either means that the username and password information in your wp-
config.php file is incorrect or we can’t contact the database server at 
localhost. This could mean your host’s database server is down.

Are you sure you have the correct username and password?
Are you sure that you have typed the correct hostname?
Are you sure that the database server is running?

我尝试访问sitedomain.com/wp-admin

时收到相同的消息

我导入了数据库,在cpanel中我创建了一个新用户并添加了所有权限并将用户添加到数据库中。在cpanel中,我可以通过phpMyAdmin访问数据库而不会出现问题。

我也可以使用新创建的用户名和密码通过MySQL Workbench连接数据库。密码使用'sitedomain.com'作为网络地址。

我的config.php文件:

<?php

define( 'WP_DEBUG', true );

define('DB_NAME', 'databaseName');

define('DB_USER', 'userName');

/* MySQL database password */
define('DB_PASSWORD', 'password');

/* MySQL hostname */
define('DB_HOST', 'localhost');     

/* Database Charset to use in creating database tables. */
define('DB_CHARSET', 'utf8');

/* The Database Collate type. Don't change this if in doubt. */
define('DB_COLLATE', '');

/* WordPress Database Table prefix. */

$table_prefix  = 'wp_';

define('WP_DEBUG', false);

/* Absolute path to the WordPress directory. */
if ( !defined('ABSPATH') )
    define('ABSPATH', dirname(__FILE__) . '/');

/* Sets up WordPress vars and included files. */
require_once(ABSPATH . 'wp-settings.php');

databaseName是新创建的数据库的名称,userName是cpanel中新创建的用户,其中包含自己的password

我尝试过使用ip而不是localhost,并尝试将siteurl切换为http://www.sitedomain.com并将主页切换为http.sitedomain.com/dir,其中dir是wordpress目录。

我在主wordpress文件夹中创建了一个testconnection.php文件:

<?php
$link = mysql_connect('localhost', 'userName', 'password');
if (!$link) {
    die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';
mysql_close($link);
?>

userNamepassword又是新用户&amp;在cpanel中创建的密码已添加到具有所有权限的数据库中。

导航到sitedomain.com/dir/textconnection.php会显示以下内容:

Could not connect: Access denied for user 'userName'@'localhost' (using password: YES)

他们的其他配置细节我忘了改变吗?我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

您的数据库是否在localhost运行?

Localhost可能不在您的hosts文件中。将localhost更改为127.0.0.1(假设它位于您的localhost)或者更改为数据库计算机的IP。