1. 登录数据库
mysql -h localhost -u root -p
2. 数据库基本操作
2.1 查看数据库
mysql> SHOW DATABASES;
+--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | sakila | | sys | | world | +--------------------+ 6 rows in set (0.00 sec)
2.2 创建数据库
mysql> CREATE DATABASE db;
mysql> CREATE DATABASE IF NOT EXISTS db;
指定字符集:
mysql> CREATE DATABASE db CHARACTER SET 'utf8' COLLATE 'utf8_general_ci';
2.3 查看创建数据库定义
mysql> SHOW CREATE DATABASE db G;
*************************** 1. row *************************** Database: db Create Database: CREATE DATABASE `db` /*!40100 DEFAULT CHARACTER SET utf8 */ 1 row in set (0.00 sec)
2.4 使用数据库
mysql> USE db;
2.5 显示使用的数据库
mysql> SELECT DATABASE(); +------------+ | DATABASE() | +------------+ | db | +------------+
2.6 删除数据库
mysql> DROP DATABASE db;
3. 查看数据库系统支持的存储引擎类型
mysql> SHOW ENGINES; +--------------------+---------+----------------------------------------------------------------+--------------+------+------------+ | Engine | Support | Comment | Transactions | XA | Savepoints | +--------------------+---------+----------------------------------------------------------------+--------------+------+------------+ | InnoDB | DEFAULT | Supports transactions, row-level locking, and foreign keys | YES | YES | YES | | MRG_MYISAM | YES | Collection of identical MyISAM tables | NO | NO | NO | | MEMORY | YES | Hash based, stored in memory, useful for temporary tables | NO | NO | NO | | BLACKHOLE | YES | /dev/null storage engine (anything you write to it disappears) | NO | NO | NO | | MyISAM | YES | MyISAM storage engine | NO | NO | NO | | CSV | YES | CSV storage engine | NO | NO | NO | | ARCHIVE | YES | Archive storage engine | NO | NO | NO | | PERFORMANCE_SCHEMA | YES | Performance Schema | NO | NO | NO | | FEDERATED | NO | Federated MySQL storage engine | NULL | NULL | NULL | +--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
其中,Support中DEFAULT表示默认存储引擎。
3.1 InnoDB存储引擎
InnoDB事务型数据库的首选引擎,支持事务安全表(ACID),支持行锁和外键。MySQL5.5.5之后,InnoDB作为默认存储引擎。
InnoDB存储引擎中,创建的表的表结构存储在.frm文件中。数据和索引存储在innodb_data_home_dir和innodb_data_file_path定义的表空间中。
使用InnoDB时,MySQL将在数据目录下创建ibdata1的自动扩展数据文件,以及两个ib_logfile0和ib_logfile1的日志文件。
innodb_data_file_path:指定innodb tablespace文件。如果不在my.ini文件中指定innodb_data_home_dir和innodb_data_file_path,则默认会在datadir目录下创建ibdata1 作为innodb tablespace。
[mysqld] #Path to the database root datadir="C:/ProgramData/MySQL/MySQL Server 5.6/Data/"
查询innodb_data_home_dir和innodb_data_file_path参数设置:
mysql> SHOW VARIABLES LIKE 'innodb_data%';
+-----------------------+------------------------+ | Variable_name | Value | +-----------------------+------------------------+ | innodb_data_file_path | ibdata1:12M:autoextend | | innodb_data_home_dir | | +-----------------------+------------------------+
my.ini中设置innodb_data_home_dir和innodb_data_file_path参数:
[mysqld] innodb_data_home_dir = /data/3306 innodb_data_file_path = ibdata1:12M:autoextend:max:1G
3.2 MyISAM存储引擎
MyISAM具有较高的插入、查询速度,但不支持事务。MySQL5.5.5之前为默认存储引擎。
MyISAM存储引擎的表存储成3个文件,文件名称与表名相同,扩展名包括frm、MYD和MYI。其中,frm扩展名文件存储表的结构,MYD扩展名文件存储数据(MYData缩写),MYI扩展名文件存储索引(MYIndex缩写)。
MyISAM存储引擎的优点:占用空间小,处理速度快;缺点:不支持实务的完整性和并发性。
4. 查询默认存储引擎
mysql> show variables like 'storage_engine';
+----------------+--------+ | Variable_name | Value | +----------------+--------+ | storage_engine | InnoDB | +----------------+--------+ 1 row in set (0.00 sec)
可以在my.ini中修改默认存储引擎,将“default-storage-engine=INNODB”改为“default-storage-engine=MyISAM”,重启服务,修改生效。
5. 查看数据库字符集
mysql> SELECT DISTINCT table_schema,table_collation -> FROM information_schema. TABLES -> WHERE table_schema NOT IN ('performance_schema', 'sys', 'test', 'mysql', 'information_schema');
+--------------+-----------------+ | table_schema | table_collation | +--------------+-----------------+ | db | utf8_general_ci | +--------------+-----------------+