数据库存在,但返回错误消息"Unknown Database". [英] Database exists but returns an error saying "Unknown Database"

查看:708
本文介绍了数据库存在,但返回错误消息"Unknown Database".的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

几个小时前,我在 Windows 10 64位计算机上安装了 WAMP服务器.我使用 phymyadmin 创建了一个名为" testdb "的数据库,并尝试使用php文件连接到该数据库.我确定我创建了数据库,但是它返回此错误:

I installed WAMP server few hours ago into my Windows 10 64-bit computer. I used phymyadmin to create a database named 'testdb' and tried to connect to it with a php file. I am sure that I created the database, but it returns this error:

"Warning: mysqli_connect(): (HY000/1049): Unknown database 'testdb' in C:\wamp64\www\projects\index.php on line 7"

这是php文件.

<?php
define('DB_SERVER', 'localhost');
define('DB_USERNAME', 'root');
define('DB_PASSWORD', '');
define('DB_NAME', 'testdb');

$link = mysqli_connect(DB_SERVER, DB_USERNAME, DB_PASSWORD, DB_NAME);
?>

我的问题与以下问题有点相似(但不完全相同):

My problem is kind of similar (but not exactly same) to the following question.:

数据库已创建,但作为未知数据库返回错误

(但是,在这种情况下,数据库不是由原始张贴者创建的.在这种情况下,我很确定自己已经创建了数据库.)

(However in that case, the database was not created by the original poster. In this case, I am pretty sure I have created the database.)

  1. 所有wamp服务正在运行.
  2. "root"用户具有访问数据库的特权.(证明)
  3. MySQL控制台确认数据库的存在. (证明)
  4. Wamp Server版本3.2.2.2
  1. All wamp services are running.
  2. 'Root' user has privileges to access the database.(proof)
  3. MySQL console confirms the existence of the database. (proof)
  4. Wamp Server version 3.2.2.2

推荐答案

在较新版本的Wampserver中,MySQL的端口已从3306更改为3308(您可以在第一个屏幕截图中看到它).您将需要更新连接以指定该端口.否则,您将遇到安装了WAMP的MariaDB,该数据库中包含该数据库.

In the newer versions of Wampserver, the port for MySQL has changed from 3306 to 3308 (you can see it in your first screenshot). You will need to update your connection to specify that port. Otherwise you will be hitting the MariaDB installed with WAMP which does not have that database within it.

define('DB_SERVER', 'localhost');
define('DB_USERNAME', 'root');
define('DB_PASSWORD', '');
define('DB_NAME', 'testdb');
define('DB_PORT', 3308);

$link = mysqli_connect(DB_SERVER, DB_USERNAME, DB_PASSWORD, DB_NAME, DB_PORT);

正如评论中指出的那样,还可以将MySQL设置为默认数据库,这也可以解决您的问题.您可以从 DBA Stack Exchange网站获取说明.

As pointed out in the comments, it is also possible to make MySQL your default database which would also solve your problem. You can get the instructions from the DBA Stack Exchange site.

这篇关于数据库存在,但返回错误消息"Unknown Database".的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆