本文主要是介绍Mysql:常用基本操作命令集,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
Mysql:常用基本操作命令集
- 客户端使用帮助
$ mysql --help
……-p, --password[=name] Password to use when connecting to server. If password isnot given it's asked from the tty.-P, --port=# Port number to use for connection or 0 for default to, inorder 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();
更多:Mysql:查看Mysql版本号
- 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用户密码
这篇关于Mysql:常用基本操作命令集的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!