一、连接数据库
root@zhangshu-virtual-machine:/# mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 608
Server version: 5.7.19 MySQL Community Server (GPL)
Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
mysql>exit(按回车)
二、操作数据库
mysql> create database demo_test;
Query OK, 1 row affected (0.01 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| demo_sfabric |
| demo_test |
| mysql |
| performance_schema |
| phpmyadmin |
| sys |
+--------------------+
7 rows in set (0.00 sec)
mysql>
mysql> drop database demo_test;
Query OK, 0 rows affected (0.00 sec)
mysql>
mysql> use demo_test;
Database changed
mysql>
查看当前使用的数据库:
mysql> select database();
+------------+
| database() |
+------------+
| demo_test |
+------------+
1 row in set (0.00 sec)
mysql>
查看mysql数据库的版本:
mysql> select version();
+-----------+
| version() |
+-----------+
| 5.7.19 |
+-----------+
1 row in set (0.00 sec)
mysql>
三、操作表
mysql> create table user(
-> id int(4) not null primary key auto_increment,
-> name char(20) not null,
-> sex int(4) not null default '0',
-> password char(10) not null);
Query OK, 0 rows affected (0.06 sec)
mysql>
mysql> show tables;
+---------------------+
| Tables_in_demo_test |
+---------------------+
| user |
+---------------------+
1 row in set (0.00 sec)
mysql>
mysql> drop table user;
Query OK, 0 rows affected (0.02 sec)
mysql> desc user;
+----------+----------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------+----------+------+-----+---------+----------------+
| id | int(4) | NO | PRI | NULL | auto_increment |
| name | char(20) | NO | | NULL | |
| sex | int(4) | NO | | 0 | |
| password | char(10) | NO | | NULL | |
+----------+----------+------+-----+---------+----------------+
4 rows in set (0.00 sec)
mysql>
mysql> insert into user(name,sex,password) values('zhangshu','1','123456');
Query OK, 1 row affected (0.00 sec)
mysql>
mysql> select * from user;
+----+----------+-----+----------+
| id | name | sex | password |
+----+----------+-----+----------+
| 1 | zhangshu | 1 | 123456 |
+----+----------+-----+----------+
1 row in set (0.00 sec)
mysql>
mysql> update user set password='666666' where name='zhangshu';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql>
mysql> delete from user where id=1;
Query OK, 1 row affected (0.01 sec)
mysql>
四、备份数据库
把数据库demo_sfabric备份到当前根目录:
root@zhangshu-virtual-machine:/# mysqldump -u root -p demo_sfabric > backup.sql
Enter password:
root@zhangshu-virtual-machine:/#
导出new_permissions 表到当前根目录:
root@zhangshu-virtual-machine:/# mysqldump -u root -p demo_sfabric new_permissions > backup_permission.sql
Enter password:
root@zhangshu-virtual-machine:/#
五、导入数据库
mysql> create database new_database; /*创建一个新的数据库*/
Query OK, 1 row affected (0.00 sec)
mysql> use new_database; /*连接新的数据库*/
Database changed
mysql> set foreign_key_checks=0; /*设置忽略外键*/
Query OK, 0 rows affected (0.00 sec)
mysql> source /backup.sql; /*导入根目录下的备份文件*/