文章目录
数据库
连接服务器
mysql -h 127.0.0.1 -P 3306 -u root -p
-h:指明登陆部署了mysql服务的主机
-P:指明我们要 访问的端口号
-u:指明登陆用户
-p:指明需要输入密码
连接的是本地的MySQL服务器,那么连接时只需要指明用户名和用户密码
root@iZwz9fjj2ssnshikw14avaZ helloworld]# mysql -uroot -p
停止服务器
[root@iZwz9fjj2ssnshikw14avaZ helloworld]# systemctl stop mysqld
创建数据库 create database db_name,本质就是再/var/lib/mysql创建一个目录
create database helloworld;
创建数据库 create database if not exists db_name
mysql> create database if not exists database1;
Query OK, 1 row affected (0.00 sec)
[root@iZwz9fjj2ssnshikw14avaZ ~]# cd /var/lib/mysql
进入/var/lib/mysql这个目录下,发现出现了helloworld
查看数据库
use helloworld; 选择数据库
show database
SQL可以查看系统中所有的数据库
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| helloworld |
| mysql |
| performance_schema |
| sys |
+--------------------+
5 rows in set (0.00 sec)
mysql> use helloworld;
Database changed
创建数据库
建立一张表,在数据库内建立表,本质就是在Linux下创建对应的文件
mysql> create table student(
-> name varchar(32) ,
-> age int ,
-> gender varchar(2)
-> );
Query OK, 0 rows affected (0.01 sec)
如果没有对MySQL的配置文件进行过修改,则默认的编码格式是utf8,默认的校验规则是utf8_general_ci
插入数据
mysql> insert into student (name , age, gender) values ('zhangsan' ,20 ,'n');
Query OK, 1 row affected (0.00 sec)
显示数据
mysql> select * from student;
+----------+------+--------+
| name | age | gender |
+----------+------+--------+
| zhangsan | 20 | n |
+----------+------+--------+
1 row in set (0.00 sec)
查存储引擎
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 |
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
9 rows in set (0.00 sec)
删除数据库,drop database db_name
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| database1 |
| helloworld |
| mysql |
| performance_schema |
| sys |
+--------------------+
6 rows in set (0.00 sec)
mysql> drop database database1;
Query OK, 0 rows affected (0.00 sec)
show charset;查看数据库支持的字符集
mysql> show charset;
创建数据库时通过charset指明数据库的编码格式
SQL中的charset=utf8,也可以写成character set=utf8或character set utf8 ,这两种写法都一样
mysql> create database d2 charset=utf8;
Query OK, 1 row affected (0.00 sec)
mysql> create database d3 character set utf8 ;
Query OK, 1 row affected (0.00 sec)
修改数据库
指定utf8编码格式和utf8_general_ci校验规则创建数据库
mysql> create database d4 charset=utf8 collate utf8_general_ci;
Query OK, 1 row affected (0.00 sec)
将数据库的字符集改为gbk,并将数据库的校验规则改为gbk_bin
mysql> create database d5 charset=gbk collate gbk_chinese_ci ;
Query OK, 1 row affected (0.00 sec)
插入数据,并查表
mysql> show tables;
+-----------------+
| Tables_in_test1 |
+-----------------+
| person |
+-----------------+
1 row in set (0.00 sec)
mysql> desc person;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| name | varchar(20) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
1 row in set (0.00 sec)
mysql> insert into person (name) values ('a');
Query OK, 1 row affected (0.00 sec)
mysql> insert into person (name) values ('b');
Query OK, 1 row affected (0.00 sec)
mysql> insert into person (name) values ('c');
Query OK, 1 row affected (0.00 sec)
mysql> select * from person;
+------+
| name |
+------+
| a |
| b |
| c |
+------+
3 rows in set (0.00 sec)
mysql> select * from person;
+------+
| name |
+------+
| a |
| b |
| c |
+------+
3 rows in set (0.00 sec)
mysql> select * from person where name ='a';
+------+
| name |
+------+
| a |
+------+
1 row in set (0.00 sec)
排序
mysql> select * from person order by name ;
+------+
| name |
+------+
| A |
| B |
| a |
| b |
+------+
4 rows in set (0.01 sec)
确认当前在那个数据库
mysql> select database();
+------------+
| database() |
+------------+
| test2 |
+------------+
1 row in set (0.00 sec)
修改
mysql> alter database test2 charset=gbk collate gbk_chinese_ci;
Query OK, 1 row affected (0.00 sec)
显示创建语句
mysql> show create database test2 ;
+----------+---------------------------------------------------------------+
| Database | Create Database |
+----------+---------------------------------------------------------------+
| test2 | CREATE DATABASE `test2` /*!40100 DEFAULT CHARACTER SET utf8 */|
+----------+---------------------------------------------------------------+
1 row in set (0.00 sec)
- MySQL建议SQL中的关键字使用大写,但不是必须的。
- 数据库的名字加上反引号,是为了防止使用的数据库名与关键字冲突。
/*!40100 DEFAULT CHARACTER SET utf8 */
不是注释,它表示当前MySQL版本如果大于4.10,则执行后面的SQL语句。
数据库的备份和恢复
数据库备份
mysqldump -P 端口号 -u 用户名 -p 密码 -B 数据库名1 数据库名2 ... > 数据库备份存储的文件路径
数据库恢复
source 数据库备份存储的文件路径
恢复成功
查看连接情况
show processlist ; 查看当前连接MySQL的用户
mysql> show processlist ;
+----+------+-----------------+-------+---------+------+----------+------------------+
| Id | User | Host | db | Command | Time | State | Info |
+----+------+-----------------+-------+---------+------+----------+------------------+
| 7 | root | localhost:49368 | test1 | Query | 0 | starting | show processlist |
+----+------+-----------------+-------+---------+------+----------+------------------+
1 row in set (0.00 sec)
- Id列:一个标识,可以在MySQL中通过kill id杀死指定id的线程。
- User列:显示当前用户,如果不是root,这个命令就只显示你权限范围内的SQL语句。
- Host列:显示这个语句是从哪个IP的哪个端口上发出的,可用来追踪出问题语句的用户。
- db列:当前执行的命令是在哪一个数据库上,如果没有指定数据库,则该值为NULL。
- Command列:显示当前连接执行的命令,一般就是休眠(Sleep)、查询(Query)和连接(Connect)。
- Time列:表示该线程处于当前状态的时间,单位是秒。
- State列:显示使用当前连接的SQL语句的状态。
- Info列:一般记录的是线程执行的语句,默认只显示前100个字符,如果要看全部信息,需要使用show full processlist
字符集和校验规则
查看系统默认的字符集
mysql> show variables like 'character_set_database';
+------------------------+--------+
| Variable_name | Value |
+------------------------+--------+
| character_set_database | latin1 |
+------------------------+--------+
1 row in set (0.00 sec)
注意:如果是在指定数据库下使用该SQL,则查看的是该数据库对应的字符集
查看系统默认的字符集校验规则,查看MySQL系统变量variables中的collation_database,可以得知系统默认的字符集校验规则
mysql> show variables like 'collation_database';
+--------------------+-------------------+
| Variable_name | Value |
+--------------------+-------------------+
| collation_database | latin1_swedish_ci |
+--------------------+-------------------+
1 row in set (0.00 sec)
只显示变量名称中包含 collation_
字符串的变量。
mysql> show variables like 'collation_%';
+----------------------+-------------------+
| Variable_name | Value |
+----------------------+-------------------+
| collation_connection | utf8_general_ci |
| collation_database | latin1_swedish_ci |
| collation_server | latin1_swedish_ci |
+----------------------+-------------------+
3 rows in set (0.00 sec)