Mysql create database

Keywords: MySQL Database Oracle

Recently, I'm ready to start Mysql. I feel very interesting. I'm ready to study hard.

I just saw the creation of mysql database, which is very different from Oracle. I feel that mysql database is similar to Oracle's schema, but there is schema in mysql. Apart from the cluster environment, Oracle has multiple databases and instances. In general, it is to create an Oracle database, just one database. mysql can create multiple databases, and the creation method is also There are many kinds of files. You can use commands and create files directly in the background. After creating files, you can automatically recognize files and databases.

1. Create database with command 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)

At this time, query the database file in the background

[root@zxy bin]# cd /root/mysql/datadir/
//The test directory is the directory of the test library just created
[root@zxy 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
[root@zxy datadir]# cd test
--This library file
[root@zxy test]# ls -l
total 4
-rw-rw---- 1 mysql mysql 65 Sep 11 10:53 db.opt
[root@zxy test]# cat db.opt 
default-character-set=latin1
default-collation=latin1_swedish_ci

2. Create a database by creating files

 

[root@zxy datadir]# mkdir -p zxy
[root@zxy datadir]# cp test/db.opt zxy/
[root@zxy datadir]# cd zxy
[root@zxy zxy]# ls -l
total 4
-rw-r----- 1 root root 65 Sep 11 10:55 db.opt
//Modify the character set of the zxy library file to distinguish it from the test just now
[root@zxy zxy]# vi db.opt 
default-character-set=gbk
default-collation=latin1_swedish_ci
//Preservation
//Check in the database that the zxy database already exists
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
| test_db            |
| zxy                |
+--------------------+
6 rows in set (0.01 sec)

 

Supplement:

1. View currently used databases

mysql> select database();
+------------+
| database() |
+------------+
| zxy        |
+------------+
1 row in set (0.00 sec)

2. View all databases

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

 

Posted by hawkeyes on Fri, 03 Jan 2020 00:33:44 -0800