DBA:多方式建立數據庫
語法:
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)]>
二、mysqladmin建庫
說明:如果是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 |
| 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!!!
> 創建數據庫失敗:
DBA:多方式建立數據庫