Linux下Mysql数据库的基础操作

Posted jks212454

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Linux下Mysql数据库的基础操作相关的知识,希望对你有一定的参考价值。

一、Mysql数据介绍

Mysql数据库是一种关系型数据库管理系统,具有的优点有体积小、速度快、总体成本低,开源,可移植性(跨平台,在不同系统中使用),可以和开发语结合,属于轻量级数据库。

二、数据库相关术语介绍

1.数据库相关名词

数据库: database  
表: table  
行: row  
列: column  
索引: index  
视图: view  
用户: user  
权限: privilege  
存储过程: procedure  
存储函数: function  
触发器: trigger  
事件调度器: event scheduler,任务计划

2.相关术语介绍

数据库中的表:表是一种结构化的文件,可用于存储特定类型的数据,表中的每一行,也称为一条记录。
数据库中的列:表中的一个字段,所有表都是由一个或多个列组成的。表中的每一列,称为属性,字段。
数据库中的索引: 将表中的一个或多个字段中的数据复制一份另存,并且按特定次序排序存储。
关系型数据库:关系数据库系统建立了关系模型,并用它来处理数据。关系模型在表中将信息与字段关联起来(也就是schemas),存储在数据表的行和列中。数据表可以彼此关联协作存储,也很容易提取数据。
非关系型数据库:非关系型数据不适合存储在数据表的行和列中,而是大块组合在一起。非关系型数据通常存储在数据集中,就像文档、键值对或者图结构。你的数据及其特性是选择数据存储和提取方式的首要影响因素。

三、Mysql数据库的管理

1.创建数据库用户

①创建用户

mysql> create user test@localhost identified by '123456';
Query OK, 0 rows affected (0.08 sec)

②授予用户权限

mysql> grant all privileges on *.* to test@localhost;
Query OK, 0 rows affected (0.10 sec)

③刷新权限缓存

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

④查看新建用户及登录

mysql> select user,host from mysql.user;
+------------------+-----------+
| user             | host      |
+------------------+-----------+
| mysql.infoschema | localhost |
| mysql.session    | localhost |
| mysql.sys        | localhost |
| root             | localhost |
| test             | localhost |
+------------------+-----------+
5 rows in set (0.01 sec)

[root@control ~]# mysql -utest -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \\g.
Your MySQL connection id is 12
Server version: 8.0.13 Source distribution

Copyright (c) 2000, 2018, 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> 

2.查询用户状态

①查看单个用户权限

mysql> show grants for 'test'@'localhost';
+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Grants for test@localhost                                                                                                                                                                                                                                                                                                                                                              |
+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, RELOAD, SHUTDOWN, PROCESS, FILE, REFERENCES, INDEX, ALTER, SHOW DATABASES, SUPER, CREATE TEMPORARY TABLES, LOCK TABLES, EXECUTE, REPLICATION SLAVE, REPLICATION CLIENT, CREATE VIEW, SHOW VIEW, CREATE ROUTINE, ALTER ROUTINE, CREATE USER, EVENT, TRIGGER, CREATE TABLESPACE, CREATE ROLE, DROP ROLE ON *.* TO `test`@`localhost` |
| GRANT BACKUP_ADMIN,BINLOG_ADMIN,CONNECTION_ADMIN,ENCRYPTION_KEY_ADMIN,GROUP_REPLICATION_ADMIN,PERSIST_RO_VARIABLES_ADMIN,REPLICATION_SLAVE_ADMIN,RESOURCE_GROUP_ADMIN,RESOURCE_GROUP_USER,ROLE_ADMIN,SET_USER_ID,SYSTEM_VARIABLES_ADMIN,XA_RECOVER_ADMIN ON *.* TO `test`@`localhost`                                                                                                  |
+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
2 rows in set (0.00 sec)


②查看单个用户是否存在

mysql> select user,host from mysql.user where user='test';
+------+-----------+
| user | host      |
+------+-----------+
| test | localhost |
+------+-----------+
1 row in set (0.00 sec)


③查询当前用户

mysql> select current_user;
+----------------+
| current_user   |
+----------------+
| root@localhost |
+----------------+
1 row in set (0.00 sec)

3.修改密码及删除用户

①修改密码

mysql> alter user 'test'@'localhost' identified by 'admin';
Query OK, 0 rows affected (0.04 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

②删除数据库用户

mysql> 
mysql> drop user test@localhost;
Query OK, 0 rows affected (0.07 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

mysql> select user,host from mysql.user;
+------------------+-----------+
| user             | host      |
+------------------+-----------+
| mysql.infoschema | localhost |
| mysql.session    | localhost |
| mysql.sys        | localhost |
| root             | localhost |
+------------------+-----------+
4 rows in set (0.00 sec)

四、创建数据库及表

1.查看当前数据库列表

mysql> use mysql;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.00 sec)

2.显示指定数据库的表

mysql> use mysql;
Database changed
mysql> show tables;
+---------------------------+
| Tables_in_mysql           |
+---------------------------+
| columns_priv              |
| component                 |
| db                        |
| default_roles             |
| engine_cost               |
| func                      |
| general_log               |
| global_grants             |
| gtid_executed             |
| help_category             |
| help_keyword              |
| help_relation             |
| help_topic                |
| innodb_index_stats        |
| innodb_table_stats        |
| password_history          |
| plugin                    |
| procs_priv                |
| proxies_priv              |
| role_edges                |
| server_cost               |
| servers                   |
| slave_master_info         |
| slave_relay_log_info      |
| slave_worker_info         |
| slow_log                  |
| tables_priv               |
| time_zone                 |
| time_zone_leap_second     |
| time_zone_name            |
| time_zone_transition      |
| time_zone_transition_type |
| user                      |
+---------------------------+
33 rows in set (0.01 sec)

3.显示数据表的详细信息

①显示mysql数据库中某个表信息

mysql> show  columns from server_cost;
+---------------+---------------+------+-----+-------------------+-----------------------------------------------+
| Field         | Type          | Null | Key | Default           | Extra                                         |
+---------------+---------------+------+-----+-------------------+-----------------------------------------------+
| cost_name     | varchar(64)   | NO   | PRI | NULL              |                                               |
| cost_value    | float         | YES  |     | NULL              |                                               |
| last_update   | timestamp     | NO   |     | CURRENT_TIMESTAMP | DEFAULT_GENERATED on update CURRENT_TIMESTAMP |
| comment       | varchar(1024) | YES  |     | NULL              |                                               |
| default_value | float         | YES  |     | NULL              | VIRTUAL GENERATED                             |
+---------------+---------------+------+-----+-------------------+----------------------------------------

以上是关于Linux下Mysql数据库的基础操作的主要内容,如果未能解决你的问题,请参考以下文章

Mysql的二进制安装和基础入门操作

mysql基础 仅供参考(基于linux下安装的mysql)

Linux系统下自行编译安装MySQL及基础配置全过程解析

求大神告知 linux系统下,JAVA如何操作mysql备份和还原。求源代码~~~~测试通过的加分。

Linux系统——MySQL基础

Linux mysql 基础操作