1. 程式人生 > >Mysql建立資料庫

Mysql建立資料庫

最近準備入手Mysql,感覺挺有意思的,準備好好學習學習。

剛看到建立mysql資料庫,這個建立資料庫跟Oracle有很大不同,我感覺mysql的資料庫跟Oracle的schema相似,但是mysql中又有schema,Oracle除了叢集環境是多個庫,多個例項,一般情況下都是建立一個Oracle資料庫,僅僅就一個庫,而mysql可以建立多個數據庫,並且建立方式還多種多樣,可以使用命令,還可以直接在後臺建立檔案,建立檔案之後,就自動識別到檔案,識別到資料庫。

1、使用命令建立資料庫create database

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test_db            |
+--------------------+
4 rows in set (0.00 sec)

mysql> 
mysql> create database test;
Query OK, 1 row affected (0.00 sec)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
| test_db            |
+--------------------+
5 rows in set (0.00 sec)

mysql> drop database if exists test;
Query OK, 0 rows affected (0.01 sec)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test_db            |
+--------------------+
4 rows in set (0.00 sec)

這個時候在後臺查詢這個資料庫檔案, 

[[email protected] bin]# cd /root/mysql/datadir/
這個test目錄就是剛剛建立的test庫的目錄
[[email protected] datadir]# ls -l
total 110680
-rw-rw---- 1 mysql mysql       56 Sep  4 13:58 auto.cnf
-rw-rw---- 1 mysql mysql 12582912 Sep 11 09:59 ibdata1
-rw-rw---- 1 mysql mysql 50331648 Sep 11 09:59 ib_logfile0
-rw-rw---- 1 mysql mysql 50331648 Sep  4 13:50 ib_logfile1
drwx------ 2 mysql mysql     4096 Sep  4 13:50 mysql
-rw------- 1 root  root       123 Sep 11 10:21 mysql_env.ini
drwx------ 2 root  root      4096 Sep  4 13:57 performance_schema
drwx------ 2 mysql mysql     4096 Sep 11 10:53 test
drwx------ 2 mysql mysql     4096 Sep  5 09:51 test_db
-rw-rw---- 1 mysql mysql    60221 Sep 11 09:59 zxy.err
-rw-rw---- 1 mysql mysql        7 Sep 11 09:59 zxy.pid
[
[email protected]
datadir]# cd test --這個庫檔案 [[email protected] test]# ls -l total 4 -rw-rw---- 1 mysql mysql 65 Sep 11 10:53 db.opt [[email protected] test]# cat db.opt default-character-set=latin1 default-collation=latin1_swedish_ci

2、使用建立檔案的方式建立資料庫

[[email protected] datadir]# mkdir -p zxy
[
[email protected]
datadir]# cp test/db.opt zxy/ [[email protected] datadir]# cd zxy [[email protected] zxy]# ls -l total 4 -rw-r----- 1 root root 65 Sep 11 10:55 db.opt 這個修改一下zxy庫檔案的字符集,跟剛才test作區分 [[email protected] zxy]# vi db.opt default-character-set=gbk default-collation=latin1_swedish_ci 儲存 在資料庫檢視zxy這個資料庫已經存在 mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | test | | test_db | | zxy | +--------------------+ 6 rows in set (0.01 sec)