解决WordPress连接数据库失败

前言 在WordPress搬站或服务器迁移过程中,会导致新建数据库无法与WordPress连接,错误提示“Error establishing a database connection”。一般导致这种问题的原因数据库本身服务挂掉了,再就是WordPress配置文件与迁移的数据库不一致。 解决方案 Ⅰ.如是本地数据库服务挂掉,重启MySQL服务即可解决。 Ⅱ.首先收到WordPress目录wp-config.php文件,打开进行编辑,修改以下的内容:
// ** MySQL settings - You can get this info from your web host ** //
/** The name of the database for WordPress */
define( 'DB_NAME', '数据库的名称' );

/** MySQL database username */
define( 'DB_USER', '数据库用户名' );

/** MySQL database password */
define( 'DB_PASSWORD', '数据库密码' );

/** MySQL hostname */
define( 'DB_HOST', 'MySQL主机名,如在本地填写:localhost' );

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

/** The Database Collate type. Don't change this if in doubt. */
define( 'DB_COLLATE', '' );
参数设置 根据以上的数据库配置修改,替换以上的汉字部分,数据库连接配置即可解决,刷新一下网站已经解决了。如有PHP缓存,请过了缓存机制再试。