Ubuntu操作mysql數據庫命令

一、連接數據庫
  • 連接本地數據庫
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;     /*導入根目錄下的備份文件*/
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容