net start mysql 启动服务
net stop mysql 停止服务
执行此命令前,需要安装服务:
mysqld -install
服务的名称往往不是mysql,而是mysql加上版本号,例如mysql57
登陆使用mysql命令,该命令有以下参数:
参数 | 作用 |
---|---|
-D | 打开指定数据库 |
–delimiter=name | 指定分隔符 |
-h(–host=name) | 服务器名称(如果是默认的localhost,可以不写该选项) |
-p(小写)(–password[=password]) | 输入密码 |
-P(大写)(–port) | 端口号(如果是默认3306端口可以不写该选项) |
-u(–user=name) | 用户名 |
-V(大写)(–version) | 获得版本 |
退出:
三个命令作用相同:
- exit
- quit
- \q
可以在登陆时用–prompt指定提示符
C:\Users\twilight>mysql -uroot -p123456 --prompt \u@\h-\d
用prompt命令
常用的参数:
- \D 完整的日期
- \d 当前数据库
- \h 服务器名称
- \u 当前用户
因为当前没有选择数据库,所以\d 显示none
mysql>prompt \u@\h \d
root@localhost (none)
root@localhost (none)use student
Database changed
root@localhost student
root@localhost student prompt \u@\h-\d \D:
PROMPT set to 'prompt \u@\h-\d \D:'
prompt root@localhost-student Fri Jun 30 10:42:56 2017:
CREATE {DATABASE | SHEMAS} [IF NOT EXISTS] db_name [DEFAULT] CHARACTER SET [=] charset_set_name
直接创建
mysql> CREATE DATABASE test;
Query OK, 1 row affected (0.00 sec)
创建时指定编码方式:
mysql> CREATE DATABASE test2 CHARACTER SET GBK;
Query OK, 1 row affected (0.00 sec)
使用IF NOT EXISTS判断数据库是否已存在:
mysql> CREATE DATABASE IF NOT EXISTS test;
Query OK, 1 row affected, 1 warning (0.00 sec)
SHOW {DATABASES | SCHEMAS} [LIKE ‘pattern’ | WHERE expr]
mysql> SHOW DATABASES;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sakila |
| student |
| sys |
| test |
| test2 |
| world |
+--------------------+
9 rows in set (0.00 sec)
SHOW WARNINGS
产生WARNINGS时查看警告的具体信息:
mysql> SHOW WARNINGS;
+-------+------+-----------------------------------------------+
| Level | Code | Message |
+-------+------+-----------------------------------------------+
| Note | 1007 | Can't create database 'test'; database exists |
+-------+------+-----------------------------------------------+
1 row in set (0.00 sec)
SHOW CREATE DATABASE NAEM
用该命令可以查看数据库的信息,比如,该数据库的编码方式是gbk
mysql> SHOW CREATE DATABASE test2;
+----------+---------------------------------------------------------------+
| Database | Create Database |
+----------+---------------------------------------------------------------+
| test2 | CREATE DATABASE `test2` /*!40100 DEFAULT CHARACTER SET gbk */ |
+----------+---------------------------------------------------------------+
1 row in set (0.00 sec)
ALTER {DATABASE | SCHEMA}[db_name] [DEFAULT] CHARACTER SET [=] charset_name
使用alter修改数据库的默认编码方式:
mysql> ALTER DATABASE test2 CHARACTER SET UTF8;
Query OK, 1 row affected (0.00 sec)
查看修改后的编码,已经变成了utf-8:
mysql> SHOW CREATE DATABASE test2;
+----------+----------------------------------------------------------------+
| Database | Create Database |
+----------+----------------------------------------------------------------+
| test2 | CREATE DATABASE `test2` /*!40100 DEFAULT CHARACTER SET utf8 */ |
+----------+----------------------------------------------------------------+
1 row in set (0.00 sec)
DROP {DATABASE | SCHEMA} [IF EXISTS] db_name 删除数据库
mysql> DROP DATABASE test2;
Query OK, 0 rows affected (0.01 sec)
mysql> SHOW DATABASES;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sakila |
| student |
| sys |
| test |
| world |
+--------------------+
8 rows in set (0.00 sec)