MySQL 常用命令示例詳解(使用mysql)
MySQL是一款常用的關系型數(shù)據(jù)庫管理系統(tǒng),廣泛應用于各個領域。在使用MySQL時,我們經(jīng)常需要編寫一些常用腳本來進行數(shù)據(jù)操作和管理。本文將介紹一些常用的MySQL腳本,幫助更好地使用MySQL。
一、啟停MySQL數(shù)據(jù)庫服務
- 啟動MySQL數(shù)據(jù)庫服務
systemctl start mysqld
- 停止MySQL數(shù)據(jù)庫服務
systemctl stop mysqld
- MySQL數(shù)據(jù)庫服務狀態(tài)查看
systemctl status mysqld
- MySQL數(shù)據(jù)庫服務開機自啟
systemctl enable mysqld
- MySQL數(shù)據(jù)庫服務禁止開機自啟
systemctl disable mysqld
二、連接MySQL數(shù)據(jù)庫
在使用MySQL之前,我們需要先連接到數(shù)據(jù)庫??梢允褂靡韵履_本進行連接:
- 使用用戶名和密碼連接MySQL數(shù)據(jù)庫
mysql -u username -p password 或者 mysql -u username -p
其中,username為數(shù)據(jù)庫用戶名,password為數(shù)據(jù)庫密碼。
- 使用主機名、用戶名和密碼連接MySQL數(shù)據(jù)庫
mysql -h 主機名 -P 端口號 -u 用戶名 -p 密碼 或者 mysql -h 主機名 -P 端口號 -u 用戶名 -p
- 使用數(shù)據(jù)庫
連接到 MySQL 數(shù)據(jù)庫后,可能有多個可以操作的數(shù)據(jù)庫,可使用use 命令選擇你要操作的數(shù)據(jù)庫。
use 數(shù)據(jù)庫名;
- 關閉MySQL數(shù)據(jù)庫連接的SQL語句
exit; 或者 quit;
三、創(chuàng)建和管理數(shù)據(jù)庫
- 創(chuàng)建數(shù)據(jù)庫:使用CREATE DATABASE語句可以創(chuàng)建新的數(shù)據(jù)庫(mydatabase),例如:
mysql> CREATE DATABASE mydatabase; Query OK, 1 row affected (0.00 sec) mysql>
- 刪除數(shù)據(jù)庫(慎用):使用DROP DATABASE語句可以刪除指定的數(shù)據(jù)庫(mydatabase),例如:
mysql> DROP DATABASE mydatabase; Query OK, 0 rows affected (0.01 sec) mysql>
- 查看數(shù)據(jù)庫:使用SHOW DATABASES語句可以列出當前所有的數(shù)據(jù)庫,例如:
mysql> SHOW DATABASES; +--------------------+ | Database | +--------------------+ | information_schema | | mydatabase | | mysql | | performance_schema | | sys | +--------------------+ 5 rows in set (0.00 sec) mysql>
- 查看當前使用的數(shù)據(jù)庫:
mysql> select database(); +------------+ | database() | +------------+ | mydatabase | +------------+ 1 row in set (0.00 sec) mysql>
- 查看當前數(shù)據(jù)庫包含的表信息
mysql> show tables; +----------------------+ | Tables_in_mydatabase | +----------------------+ | users | +----------------------+ 1 row in set (0.00 sec) mysql>
- 獲取表結構
mysql> desc users; +-------+-------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-------+-------------+------+-----+---------+----------------+ | id | int | NO | PRI | NULL | auto_increment | | name | varchar(50) | YES | | NULL | | | age | int | YES | | NULL | | | email | varchar(50) | YES | | NULL | | +-------+-------------+------+-----+---------+----------------+ 4 rows in set (0.00 sec) mysql>
- 查看數(shù)據(jù)庫版本
mysql> select version(); +-----------+ | version() | +-----------+ | 8.0.32 | +-----------+ 1 row in set (0.00 sec) mysql>
四、創(chuàng)建和管理數(shù)據(jù)表
- 創(chuàng)建數(shù)據(jù)表
數(shù)據(jù)表是數(shù)據(jù)庫中存儲數(shù)據(jù)的主要方式??梢允褂靡韵履_本創(chuàng)建一個名為users的數(shù)據(jù)表:
mysql> use mydatabase
Database changed
mysql>
mysql> CREATE TABLE users (
-> id INT AUTO_INCREMENT PRIMARY KEY,
-> name VARCHAR(50),
-> age INT,
-> email VARCHAR(50)
-> );
Query OK, 0 rows affected (0.01 sec)
mysql> - 插入數(shù)據(jù)
插入數(shù)據(jù)是將數(shù)據(jù)添加到數(shù)據(jù)庫中的關鍵操作??梢允褂靡韵履_本向users表中插入一條數(shù)據(jù):
mysql> INSERT INTO users (name, age, email) VALUES ('John', 25, 'john@example.com');
Query OK, 1 row affected (0.01 sec)
mysql>- 查詢數(shù)據(jù)
查詢數(shù)據(jù)是使用MySQL的常見操作之一。可以使用以下腳本查詢users表中的所有數(shù)據(jù):
mysql> SELECT * FROM users; +----+------+------+------------------+ | id | name | age | email | +----+------+------+------------------+ | 1 | John | 25 | john@example.com | +----+------+------+------------------+ 1 row in set (0.00 sec) mysql>
- 更新數(shù)據(jù)
更新數(shù)據(jù)是在數(shù)據(jù)庫中修改現(xiàn)有數(shù)據(jù)的操作??梢允褂靡韵履_本將users表中id為1的記錄的age字段更新為30:
mysql> UPDATE users SET age = 30 WHERE id = 1; Query OK, 1 row affected (0.01 sec) Rows matched: 1 Changed: 1 Warnings: 0 mysql> SELECT * FROM users; +----+------+------+------------------+ | id | name | age | email | +----+------+------+------------------+ | 1 | John | 30 | john@example.com | +----+------+------+------------------+ 1 row in set (0.00 sec) mysql>
- 刪除數(shù)據(jù)
刪除數(shù)據(jù)是從數(shù)據(jù)庫中刪除不再需要的數(shù)據(jù)的操作??梢允褂靡韵履_本刪除users表中id為1的記錄:
DELETE FROM users WHERE id = 1;
- 排序數(shù)據(jù):使用ORDER BY子句可以對查詢結果進行排序。以下是按照年齡從小到大對"users"表中的記錄進行排序的示例:
SELECT * FROM users ORDER BY age ASC;
- 連接表:使用JOIN語句可以將多個表連接起來進行查詢。以下是連接"users"表和"orders"表,并檢索用戶和訂單信息的示例:
SELECT users.name, orders.order_number FROM users JOIN orders ON users.id = orders.user_id;
- 創(chuàng)建索引:使用CREATE INDEX語句可以為表中的列創(chuàng)建索引,以提高查詢效率。以下是為"users"表中的name列創(chuàng)建索引的示例:
CREATE INDEX idx_name ON users (name);
- 刪除表和數(shù)據(jù)庫(慎用):使用DROP TABLE語句可以刪除表,使用DROP DATABASE語句可以刪除數(shù)據(jù)庫。以下是刪除"users"表和"mydatabase"數(shù)據(jù)庫的示例:
DROP TABLE users; DROP DATABASE mydatabase;
- 更改表名
rename table 原表名 to 新表名; mysql> rename table users to t_users; Query OK, 0 rows affected (0.01 sec) mysql> select * from t_users; +----+------+------+-------------------+ | id | name | age | email | +----+------+------+-------------------+ | 1 | John | 30 | john@example.com |2 | Tom | 25 | tom@example.com |3 | Zyl | 18 | zyl@example.com +----+------+------+-------------------+ 3 rows in set (0.00 sec) mysql>
- mysql的表中增加字段
###表t_users 中添加了一個字段address ,類型為 varchar(255)。 mysql> alter table t_users add column address varchar(255); Query OK, 0 rows affected (0.01 sec)
- 授權
MySQL8.0 以上將創(chuàng)建賬戶和賦予權限分開了,需要用兩個語句創(chuàng)建賬號,賦予遠程權限;
###1、創(chuàng)建test用戶 create user 'test'@'%' identified by 'Test##2023'; ###授權所有權限 grant all privileges on *.* to 'test'@'%' with grant option; ###部分權限(select, insert),這個用戶只能連接到數(shù)據(jù)庫,并擁有select, insert權限。 mysql> grant select, insert on mydatabase.* to 'test'@'%' with grant option; Query OK, 0 rows affected (0.01 sec) mysql> ###驗證test用戶權限 mysql> show grants for 'test'@'%'; mysql> delete from t_users; ERROR 1142 (42000): DELETE command denied to user 'test'@'localhost' for table 't_users' mysql> ###---->該用戶目前沒有DELETE 的權限
五、數(shù)據(jù)備份和恢復
備份數(shù)據(jù)庫:使用mysqldump命令可以備份整個數(shù)據(jù)庫,例如:
mysqldump -u username -p mydatabase > backup.sql
[root@db-server ~]# mysqldump -u root -p mydatabase > backup.sql Enter password: [root@db-server ~]# ll total 1964180 -rw-r--r--. 1 root root 2036 Jul 29 17:35 backup.sql
恢復數(shù)據(jù)庫:使用mysql命令可以從備份文件中恢復數(shù)據(jù)庫,例如:
mysql -u username -p mydatabase < backup.sql
###刪除表數(shù)據(jù) mysql> delete from users; Query OK, 3 rows affected (0.02 sec) ###恢復 [root@db-server ~]# mysql -u root -p mydatabase < backup.sql Enter password: [root@db-server ~]# ###驗證 mysql> select * from users; +----+------+------+-------------------+ | id | name | age | email | +----+------+------+-------------------+ | 1 | John | 30 | john@example.com |2 | Tom | 25 | tom@example.com |3 | Zyl | 18 | zyl@example.com +----+------+------+-------------------+ 3 rows in set (0.00 sec) mysql>
導出數(shù)據(jù)到csv文件:
###創(chuàng)建目錄、賦權 [root@db-server ~]# mkdir -p /path/to [root@db-server ~]# chmod -R 777 /path/to/ [root@db-server ~]# cd /path/ [root@db-server path]# ll total 0 drwxrwxrwx. 2 root root 6 Jul 29 17:20 to [root@db-server path]#
配置 secure_file_priv 變量,在/etc/my.cnf文件中加入
secure_file_priv=/path/to

重啟數(shù)據(jù)庫服務:
systemctl restart mysqld
導出數(shù)據(jù)是將數(shù)據(jù)庫中的數(shù)據(jù)導出到文件的操作??梢允褂靡韵履_本將users表中的數(shù)據(jù)導出到users.csv文件中:
mysql> SELECT * INTO OUTFILE '/path/to/users.csv' FIELDS TERMINATED BY ',' LINES TERMINATED BY '\n' FROM users; Query OK, 1 row affected (0.00 sec) mysql>

從csv文件導入數(shù)據(jù):

導入數(shù)據(jù)是將文件中的數(shù)據(jù)導入到數(shù)據(jù)庫中的操作。可以使用以下腳本將users.csv文件中的數(shù)據(jù)導入到users表中:
mysql> LOAD DATA INFILE '/path/to/users.csv' INTO TABLE users FIELDS TERMINATED BY ',' LINES TERMINATED BY '\n'; Query OK, 3 rows affected (0.01 sec) Records: 3 Deleted: 0 Skipped: 0 Warnings: 0 mysql>

導出數(shù)據(jù)到txt文件:
使用SELECT INTO OUTFILE語句可以將查詢結果導出為文件,例如:
mysql> SELECT * INTO OUTFILE '/path/to/result.txt' FROM users; Query OK, 3 rows affected (0.01 sec) mysql>
從txt文件導入導入數(shù)據(jù):
使用LOAD DATA INFILE語句可以將數(shù)據(jù)從文件導入到表中,例如:
LOAD DATA INFILE '/path/to/result.txt' INTO TABLE users;
導出一個表:
mysqldump -u 用戶名 -p 數(shù)據(jù)庫名 表名> 導出的文件名 [root@db-server ~]# mysqldump -u root -p mydatabase users > mydatabases_users.sql Enter password: [root@db-server ~]# ll total 1964184 -rw-r--r--. 1 root root 2036 Jul 29 18:07 mydatabases_users.sql
導出一個數(shù)據(jù)庫結構:
###參數(shù): -d 沒有數(shù)據(jù) --add-drop-table 在每個create語句之前增加一個drop table [root@db-server ~]# mysqldump -u root -p -d --add-drop-table mydatabase > mydatabase-tab.sql Enter password: [root@db-server ~]# ll total 1964196 -rw-r--r--. 1 root root 2036 Jul 29 17:35 backup.sql -rw-r--r--. 1 root root 2036 Jul 29 18:07 mydatabases_users.sql -rw-r--r--. 1 root root 1732 Jul 29 18:12 mydatabase-tab.sql
source 命令導入數(shù)據(jù):
mysql> delete from users; Query OK, 3 rows affected (0.00 sec) ####備份文件位置 mysql> source /root/backup.sql Query OK, 0 rows affected (0.00 sec) Query OK, 0 rows affected (0.00 sec) Query OK, 0 rows affected (0.00 sec) Query OK, 0 rows affected (0.00 sec) Query OK, 0 rows affected (0.00 sec) Query OK, 0 rows affected (0.00 sec) Query OK, 0 rows affected (0.00 sec) Query OK, 0 rows affected (0.00 sec) Query OK, 0 rows affected (0.00 sec) Query OK, 0 rows affected (0.00 sec) Query OK, 0 rows affected (0.01 sec) Query OK, 0 rows affected (0.00 sec) Query OK, 0 rows affected (0.00 sec) Query OK, 0 rows affected, 1 warning (0.01 sec) Query OK, 0 rows affected (0.00 sec) Query OK, 0 rows affected (0.00 sec) Query OK, 0 rows affected (0.00 sec) Query OK, 3 rows affected (0.01 sec) Records: 3 Duplicates: 0 Warnings: 0 Query OK, 0 rows affected (0.04 sec) Query OK, 0 rows affected (0.00 sec) Query OK, 0 rows affected (0.00 sec) Query OK, 0 rows affected (0.00 sec) Query OK, 0 rows affected (0.00 sec) Query OK, 0 rows affected (0.00 sec) Query OK, 0 rows affected (0.00 sec) Query OK, 0 rows affected (0.00 sec) Query OK, 0 rows affected (0.00 sec) Query OK, 0 rows affected (0.00 sec) mysql> ###驗證: mysql> select * from users; +----+------+------+-------------------+ | id | name | age | email | +----+------+------+-------------------+ | 1 | John | 30 | john@example.com |2 | Tom | 25 | tom@example.com |3 | Zyl | 18 | zyl@example.com +----+------+------+-------------------+ 3 rows in set (0.00 sec) mysql>
注意: mysqldump導出mysql數(shù)據(jù)庫中某個數(shù)據(jù)庫的數(shù)據(jù),用mysqldump只要把輸出符號換為輸入符號。那樣導入后,雖說不報錯,但是數(shù)據(jù)庫中是沒有任何數(shù)據(jù)的!需要直接通過mysql命令來實現(xiàn)。
恢復數(shù)據(jù)庫:使用mysql命令可以從備份文件中恢復數(shù)據(jù)庫,例如:
mysql -u username -p mydatabase < backup.sql
###刪除表數(shù)據(jù) mysql> delete from users; Query OK, 3 rows affected (0.02 sec) ###恢復 [root@db-server ~]# mysql -u root -p mydatabase < backup.sql Enter password: [root@db-server ~]# ###驗證 mysql> select * from users; +----+------+------+-------------------+ | id | name | age | email | +----+------+------+-------------------+ | 1 | John | 30 | john@example.com |2 | Tom | 25 | tom@example.com |3 | Zyl | 18 | zyl@example.com +----+------+------+-------------------+ 3 rows in set (0.00 sec) mysql>
六、查詢與優(yōu)化
查詢數(shù)據(jù):使用SELECT語句可以從表中查詢數(shù)據(jù),例如:
mysql> SELECT * FROM users WHERE id = 1; +----+------+------+-------------------+ | id | name | age | email | +----+------+------+-------------------+ | 1 | John | 30 | john@example.com +----+------+------+-------------------+ 1 row in set (0.00 sec) mysql>
優(yōu)化查詢:使用EXPLAIN語句可以分析查詢語句的執(zhí)行計劃,例如:
mysql> EXPLAIN SELECT * FROM users WHERE id = 1; +----+-------------+-------+------------+-------+---------------+---------+---------+-------+------+----------+-------+ | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra | +----+-------------+-------+------------+-------+---------------+---------+---------+-------+------+----------+-------+ | 1 | SIMPLE | users | NULL | const | PRIMARY | PRIMARY | 4 | const | 1 | 100.00 | NULL | +----+-------------+-------+------------+-------+---------------+---------+---------+-------+------+----------+-------+ 1 row in set, 1 warning (0.00 sec) mysql>
到此這篇關于MySQL 常用命令示例的文章就介紹到這了,更多相關mysql常用命令內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
利用MySQL系統(tǒng)數(shù)據(jù)庫做性能負載診斷的方法
這篇文章主要介紹了利用MySQL系統(tǒng)數(shù)據(jù)庫做性能負載診斷的方法,本文圖文并茂給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下2019-09-09
監(jiān)控MySQL主從狀態(tài)的shell腳本
這篇文章主要分享了監(jiān)控MySQL主從狀態(tài)的shell腳本,幫助大家更好的監(jiān)控mysql數(shù)據(jù)庫,保持穩(wěn)定性,感興趣的朋友可以了解下2020-12-12
win2008 R2 WEB環(huán)境配置之MYSQL 5.6.22安裝版安裝配置方法
這篇文章主要介紹了win2008 R2 WEB環(huán)境配置之MYSQL 5.6.22安裝版安裝配置方法,需要的朋友可以參考下2016-06-06
MySQL slow_log表無法修改成innodb引擎詳解
這篇文章主要給大家介紹了關于MySQL slow_log表無法修改成innodb引擎的相關資料,文中通過示例代碼介紹的非常詳細,對大家學習或者使用MySQL具有一定的參考學習價值,需要的朋友們下面來一起學習學習吧2019-04-04
Mysql 5.7.18 解壓版下載安裝及啟動mysql服務的圖文詳解
這篇文章主要介紹了Mysql 5.7.18 解壓版下載安裝及啟動mysql服務的圖文詳解,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2017-05-05

