通过cmd命令导入和导出sql文件(以管理员身份运行cmd)
导出
mysqldump -u username -ppassword dbname > dbname.sql
1.cmd 进入到mysql的安装路径下的bin 目录(如:C:\Program Files\MySQL\MySQL Server 5.7\bin)
2.例:mysql账号为 root; 密码为:123456 ;要导出的数据库名为nodejs_db;dbsqlfile.sql为导出的sql脚本名字
mysqldump -u root -p123456 nodejs_db > dbsqlfile.sql (不能带 ;)
3.在当前目录下就会出现dbsqlfile.sql 文件
导入:(导入桌面上dbsqlfile.sql脚本数据)
source C:/Users/Administrator/Desktop/dbsqlfile.sql;
例:mysql账号为 root ;密码为:123456; 要导入的数据库名为nodejs_db;dbsqlfile.sql为导入的sql脚本名字
打开 MySQL 5.7 Command Line Client - Unicode
然后输入mysql密码
1.首先创建 数据库:nodejs_db;
create database nodejs_db;
2.切换到当前 数据库:
use nodejs_db;
3.执行脚本 (sql脚本放在桌面:C:/Users/Administrator/Desktop/dbsqlfile.sql;)
source C:/Users/Administrator/Desktop/dbsqlfile.sql;
4.数据库的表及表内的数据导入完成。
mysql命令行操作
-
打开 MySQL 5.7 Command Line Client - Unicode
-
展示所有数据库:
show databases;
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| flasker |
| mygame_db |
| mygamedb |
| mysql |
| performance_schema |
| python_study |
| sys |
| world |
| wyj_game_db |
+--------------------+
-
创建数据库(create database <数据库名>):
CREATE DATABASE mytest;
mysql> CREATE DATABASE mytest;
Query OK, 1 row affected (0.00 sec)
此时数据库存在该数据库:
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| flasker |
| mygame_db |
| mygamedb |
| mysql |
| mytest |
| performance_schema |
| python_study |
| sys |
| world |
| wyj_game_db |
+--------------------+
-
删除数据库(drop database <数据库名>) :
DROP DATABASE myTest;
mysql> DROP DATABASE myTest;
Query OK, 0 rows affected (0.00 sec)
此时:
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| flasker |
| mygame_db |
| mygamedb |
| mysql |
| performance_schema |
| python_study |
| sys |
| world |
| wyj_game_db |
+--------------------+
10 rows in set (0.00 sec)
-
创建数据库:
CREATE DATABASE mytest;
mysql> CREATE DATABASE mytest;
Query OK, 1 row affected (0.00 sec)
-
删除数据库:drop database <数据库名>
drop database mytest;
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| mytest |
| performance_schema |
| sakila |
| sys |
| world |
+--------------------+
7 rows in set (0.00 sec)
mysql> drop database mytest;
Query OK, 0 rows affected (0.00 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sakila |
| sys |
| world |
+--------------------+
6 rows in set (0.00 sec)
-
打开某一数据库:
use mytest;
mysql> use mytest;
Database changed
创建数据库表:
create table <表名> ( <字段名1> <类型1> [,..<字段名n> <类型n>])
CREATE TABLE user
(
id
int(11) NOT NULL AUTO_INCREMENT,
username
varchar(80) DEFAULT NULL,
email
varchar(120) DEFAULT NULL,
PRIMARY KEY (id
)
) ENGINE=InnoDB AUTO_INCREMENT=16 DEFAULT CHARSET=utf8;
mysql> CREATE TABLE `user` (
-> `id` int(11) NOT NULL AUTO_INCREMENT,
-> `username` varchar(80) DEFAULT NULL,
-> `email` varchar(120) DEFAULT NULL,
-> PRIMARY KEY (`id`)
-> ) ENGINE=InnoDB AUTO_INCREMENT=16 DEFAULT CHARSET=utf8;
Query OK, 0 rows affected (0.02 sec)
-
删除数据库中的表:
DROP TABLE user;
mysql> show tables;
+------------------+
| Tables_in_mytest |
+------------------+
| user |
+------------------+
1 row in set (0.00 sec)
mysql> use mytest;
Database changed
mysql> drop table user;
Query OK, 0 rows affected (0.01 sec)
mysql> show tables;
Empty set (0.00 sec)
-
展示数据库中所有的表:
show tables;
mysql> show tables;
+------------------+
| Tables_in_mytest |
+------------------+
| user |
+------------------+
-
展示数据库表结构:
desc user;
mysql> desc user;
+----------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| username | varchar(80) | YES | | NULL | |
| email | varchar(120) | YES | | NULL | |
+----------+--------------+------+-----+---------+----------------+
3 rows in set (0.01 sec)
-
插入一条数据:
insert into user(username,email) values('erming','erming@example.com');
(或者:insert into user values(3,'erming','erming@example.com');)
mysql> insert into user(username,email) values('erming','erming@example.com');
Query OK, 1 row affected (0.00 sec)
mysql> select * from user;
+----+----------+----------------------+
| id | username | email |
+----+----------+----------------------+
| 1 | admin | xiaoming@example.com |
| 16 | erming | erming@example.com |
+----+----------+----------------------+
-
删除一条数据:
delete from user where username='xiaoming';
mysql> delete from user where username='xiaoming';
Query OK, 1 row affected (0.00 sec)
-
查找表中某一条数据:
select * from user where username='admin';
mysql> select * from user where username='admin';
+----+----------+----------------------+
| id | username | email |
+----+----------+----------------------+
| 1 | admin | xiaoming@example.com |
+----+----------+----------------------+
1 row in set (0.00 sec)
-
查找表中所有数据:
select * from user;
在命令窗口按照每条数据展示:
select * from user \G;
mysql> select * from user;
+----+----------+----------------------+
| id | username | email |
+----+----------+----------------------+
| 1 | admin | admin@example.com |
| 2 | xiaoming | xiaoming@example.com |
+----+----------+----------------------+
mysql> select * from user;
+----+----------+-------------------+
| id | username | email |
+----+----------+-------------------+
| 1 | admin | admin@example.com |
+----+----------+-------------------+
-
更新一条数据:
update user set email='xiaoming@example.com' where username='admin';
mysql> update user set email='xiaoming@example.com' where username='admin';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select * from user;
+----+----------+----------------------+
| id | username | email |
+----+----------+----------------------+
| 1 | admin | xiaoming@example.com |
+----+----------+----------------------+
1 row in set (0.00 sec)
```javascript