语法:
create database ‘DBname‘;
[root@mysql-linuxview ~]# mysql -uroot -p
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MySQL connection id is 15
Server version: 5.7.18-log Source distribution
Copyright (c) 2000, 2017, Oracle, MariaDB Corporation Ab and others.
Type ‘help;‘ or ‘\h‘ for help. Type ‘\c‘ to clear the current input statement.
MySQL [(none)]> create database viewtest;
Query OK, 1 row affected (0.01 sec)
MySQL [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| linuxview |
| mysql |
| performance_schema |
| sys |
| viewtest |
+--------------------+
6 rows in set (0.00 sec)
MySQL [(none)]>
说明:如果是MySQL的普通用户,需要root赋予特定创建或者删除MySQL数据库权限。
例:[root@mysql-linuxview ~]# mysqladmin -uroot -p create linuxtest Enter password: [root@mysql-linuxview ~]# mysql -uroot -p Enter password: Welcome to the MariaDB monitor. Commands end with ; or \g. Your MySQL connection id is 17 Server version: 5.7.18-log Source distribution
Copyright (c) 2000, 2017, Oracle, MariaDB Corporation Ab and others.
Type ‘help;‘ or ‘\h‘ for help. Type ‘\c‘ to clear the current input statement.
MySQL [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| linuxtest |
| linuxview |
| mysql |
| performance_schema |
| sys |
| viewtest |
+--------------------+
7 rows in set (0.00 sec)
MySQL [(none)]>
三、PHP脚本建库
> > PHP使用mysqli_query函数来创建或者删除MySQL数据库
> 该函数有两个参数,在执行成功是返回true,否则返回false。
> **语法:**
> mysqli_query(connection,query,resultmode);
![](http://i2.51cto.com/images/blog/201808/28/d7b788972dd31eca4334d5c8c4a69229.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=)
**实例操作:**
[root@mysql-linuxview web]# cat index.php
<?
$dbhost = ‘localhost:3306‘; //MySQL的服务器地址和端口
$dbuser = ‘root‘; //登录MySQL数据库用户名
$dbpass = ‘000000‘; //登录MySQL数据库用户密码
$conn = mysqli_connect($dbhost,$dbuser,$dbpass);
if ( ! $conn)
{
die(‘Could not connect:‘. mysqli_error());
}
echo ‘connect success!!!<br />‘;
$vsql1 = ‘create database lvtest‘;
$retval = mysqli_query($conn,$vsql1);
if ( ! $retval)
{
die(‘创建数据库失败:‘ . mysqli_query($conn));
}
echo "创建数据库lvtest成功\n";
mysqli_close($conn);
?>
[root@mysql-linuxview web]#
查看结果
![](http://i2.51cto.com/images/blog/201808/28/d7ec5b8fecc7ba0d754df20e520ed402.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=)
![](http://i2.51cto.com/images/blog/201808/28/1df76d1853a0eaa9cfb418215a9affe0.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=)
输出结果:
connect success!!!
创建数据库lvtest成功
> 如果报错:
> connect success!!!
> 创建数据库失败:
原文:http://blog.51cto.com/leoheng/2165711