Mysql:常用基本操作命令集
- 客戶端使用幫助
$ mysql --help
……
-p, --password[=name]
Password to use when connecting to server. If password is
not given it's asked from the tty.
-P, --port=# Port number to use for connection or 0 for default to, in
order of preference, my.cnf, $MYSQL_TCP_PORT,
/etc/services, built-in default (3306).
-h, --host=name Connect to host.
-u, --user=name User for login if not current user.
- 客戶端連接服務端
服務地址 | 服務端口 | 用戶 | 密碼 |
---|---|---|---|
127.0.0.1 | 3306 | test1280 | 1qaz2wsx |
- 指定用戶、密碼
mysql -utest1280 -p1qaz@WSX
- 指定用戶、密碼、地址、端口
mysql -utest1280 -p1qaz@WSX -h 127.0.0.1 -P 3306
或者:
mysql --user=test1280 --password=1qaz@WSX --host=127.0.0.1 --port=3306
- 查詢服務端版本號
select version();
- database的創建、刪除、選擇
創建:mysql> create database test1280;
刪除:mysql> drop database test1280;
選擇:mysql> use test1280;
- show的使用
顯示 database:
mysql> show databases;
+-------------------------------+
| Database |
+-------------------------------+
| information_schema |
| mysql |
| mysql_innodb_cluster_metadata |
| performance_schema |
| sys |
| test1280 |
+-------------------------------+
12 rows in set (0.00 sec)
顯示 table:
mysql> show tables;
+--------------------+
| Tables_in_test1280 |
+--------------------+
| userinfo |
+--------------------+
1 row in set (0.00 sec)
mysql> show tables from test1280;
+--------------------+
| Tables_in_test1280 |
+--------------------+
| userinfo |
+--------------------+
1 row in set (0.00 sec)
使用like模糊匹配:
mysql> show tables from test1280 like "%user%";
+-----------------------------+
| Tables_in_test1280 (%user%) |
+-----------------------------+
| userinfo |
+-----------------------------+
1 row in set (0.00 sec)
顯示系統變量及值:
mysql> show variables like "%version_comment%";
+-----------------+------------------------------+
| Variable_name | Value |
+-----------------+------------------------------+
| version_comment | MySQL Community Server - GPL |
+-----------------+------------------------------+
1 row in set (0.01 sec)
- 顯示錶信息(結構)
mysql> describe userinfo;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| name | varchar(16) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
1 row in set (0.00 sec)
- 組複製節點狀態查詢
mysql> select * from performance_schema.replication_group_members;
+---------------------------+--------------------------------------+-------------+-------------+--------------+-------------+----------------+
| CHANNEL_NAME | MEMBER_ID | MEMBER_HOST | MEMBER_PORT | MEMBER_STATE | MEMBER_ROLE | MEMBER_VERSION |
+---------------------------+--------------------------------------+-------------+-------------+--------------+-------------+----------------+
| group_replication_applier | 5d4b9ccf-86b6-11e9-bb97-1c4024c099f1 | test1280 | 3306 | ONLINE | PRIMARY | 8.0.15 |
| group_replication_applier | 896e52f4-86b2-11e9-9214-1c4024c099fe | test1281 | 3306 | ONLINE | SECONDARY | 8.0.15 |
| group_replication_applier | a46e46ef-86b0-11e9-b58e-1c4024c0997c | test1282 | 3306 | ONLINE | SECONDARY | 8.0.15 |
+---------------------------+--------------------------------------+-------------+-------------+--------------+-------------+----------------+
3 rows in set (0.00 sec)
- 數據導入導出
導出
導出表結構和表數據:
mysqldump -uuser -ppassword -h host -P port database table > output.sql
例如:
mysqldump -uroot -p1qaz@WSX -h 127.0.0.1 -P 3306 test1280 userinfo > table_withdata.sql
導出表結構不帶數據:
mysqldump -uuser -ppassword -h host -P port -d database table > output.sql
例如:
mysqldump -uroot -p1qaz@WSX -h 127.0.0.1 -P 3306 -d test1280 userinfo > table_withoutdata.sql
導出數據庫所有表以及數據:
mysqldump -uuser -ppassword -h host -P port database > output.sql
例如:
mysqldump -uroot -p1qaz@WSX -h 127.0.0.1 -P 3306 test1280 > database_withdata.sql
導出數據庫所有表(結構)不含表數據:
mysqldump -uuser -ppassword -h host -P port -d database > output.sql
例如:
mysqldump -uroot -p1qaz@WSX -h 127.0.0.1 -P 3306 -d test1280 > database_withoutdata.sql
導出數據庫是多次執行導出表的結果集合。
導出數據庫約等價於遍歷數據庫的每個表,執行表導出操作。
導入
通過source命令執行導出生成的sql結果:
mysql> source ./output.sql
例如:
mysql> source ./table_withdata.sql;
mysql> source ./table_withoutdata.sql;
mysql> source ./database_withdata.sql;
mysql> source ./database_withoutdata.sql;
未完待續…
創建mysql用戶新增
授權mysql用戶權限
修改mysql用戶密碼