MySQL 如何检查mysql数据库是否存在

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/838978/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 13:14:53  来源:igfitidea点击:

How to check if mysql database exists

mysqldatabaseexists

提问by Ankur

Is it possible to check if a (MySQL) database exists after having made a connection.

建立连接后是否可以检查(MySQL)数据库是否存在。

I know how to check if a table exists in a DB, but I need to check if the DB exists. If not I have to call another piece of code to create it and populate it.

我知道如何检查数据库中是否存在表,但我需要检查数据库是否存在。如果不是,我必须调用另一段代码来创建它并填充它。

I know this all sounds somewhat inelegant - this is a quick and dirty app.

我知道这一切听起来有些不雅 - 这是一个快速而肮脏的应用程序。

回答by Kirtan

SELECT SCHEMA_NAME
  FROM INFORMATION_SCHEMA.SCHEMATA
 WHERE SCHEMA_NAME = 'DBName'

If you just need to know if a db exists so you won't get an error when you try to create it, simply use (From here):

如果您只需要知道一个 db 是否存在以便在尝试创建它时不会出错,只需使用 (From here):

CREATE DATABASE IF NOT EXISTS DBName;

回答by Ruben Konig

A simple way to check if a database exists is:

检查数据库是否存在的简单方法是:

SHOW DATABASES LIKE 'dbname';

If database with the name 'dbname' doesn't exist, you get an empty set. If it does exist, you get one row.

如果名称为“dbname”的数据库不存在,则会得到一个空集。如果确实存在,您将获得一行。

回答by AskApache Webmaster

From the shell like bash

从像 bash 这样的 shell

if [[ ! -z "`mysql -qfsBe "SELECT SCHEMA_NAME FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME='db'" 2>&1`" ]];
then
  echo "DATABASE ALREADY EXISTS"
else
  echo "DATABASE DOES NOT EXIST"
fi

回答by TopPot

If you are looking for a php script see below.

如果您正在寻找 php 脚本,请参见下文。

$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
if (!$link) {
  die('Not connected : ' . mysql_error());
}

// make foo the current db
$db_selected = mysql_select_db('foo', $link);
if (!$db_selected) {
  die ('Cannot use foo : ' . mysql_error());
}

回答by docwhat

Here is a bash function for checking if a database exists:

这是一个用于检查数据库是否存在的 bash 函数:

function does_db_exist {
  local db=""

  local output=$(mysql -s -N -e "SELECT schema_name FROM information_schema.schemata WHERE schema_name = '${db}'" information_schema)
  if [[ -z "${output}" ]]; then
    return 1 # does not exist
  else
    return 0 # exists
  fi
}           

Another alternative is to just try to use the database. Note that this checks permission as well:

另一种选择是尝试使用数据库。请注意,这也会检查权限:

if mysql "${db}" >/dev/null 2>&1 </dev/null
then
  echo "${db} exists (and I have permission to access it)"
else
  echo "${db} does not exist (or I do not have permission to access it)"
fi

回答by Junaid Saleem

A great way to check if a database exists in PHP is:

检查 PHP 中是否存在数据库的好方法是:

$mysql = mysql_connect("<your host>", "root", "");

if (mysql_select_db($mysql, '<your db name>')) {
    echo "Database exists";
} else {
    echo "Database does not exist";
}

That is the method that I always use.

这是我一直使用的方法。

回答by andiba

A very simple BASH-one-liner:

一个非常简单的 BASH-one-liner:

mysqlshow | grep dbname

回答by jprism

CREATE SCHEMA IF NOT EXISTS `demodb` DEFAULT CHARACTER SET utf8 ;

回答by inemanja

Using bash:

使用 bash:

if [ "`mysql -u'USER' -p'PASSWORD' -se'USE $DATABASE_NAME;' 2>&1`" == "" ]; then
    echo $DATABASE_NAME exist
else
    echo $DATABASE_NAME doesn't exist
fi

回答by Thomas Williams

For those who use php with mysqli then this is my solution. I know the answer has already been answered, but I thought it would be helpful to have the answer as a mysqli prepared statement too.

对于那些将 php 与 mysqli 一起使用的人,这就是我的解决方案。我知道答案已经得到解答,但我认为将答案作为 mysqli 准备好的语句也会有所帮助。

$db = new mysqli('localhost',username,password);
$database="somedatabase";
$query="SELECT SCHEMA_NAME FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME=?";
$stmt = $db->prepare($query);
$stmt->bind_param('s',$database);
$stmt->execute();
$stmt->bind_result($data);
if($stmt->fetch())
{
    echo "Database exists.";
}
else
{
    echo"Database does not exist!!!";
}
$stmt->close();