Linux下Mysql数据库的基础操作
生活随笔
收集整理的這篇文章主要介紹了
Linux下Mysql数据库的基础操作
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Linux下Mysql數據庫的基礎操作
- 一、Mysql數據介紹
- 二、數據庫相關術語介紹
- 1.數據庫相關名詞
- 2.相關術語介紹
- 三、Mysql數據庫的管理
- 1.創建數據庫用戶
- 2.查詢用戶狀態
- 3.修改密碼及刪除用戶
- 四、創建數據庫及表
- 1.查看當前數據庫列表
- 2.顯示指定數據庫的表
- 3.顯示數據表的詳細信息
- 4.創建數據庫的表
- 五、查詢表其他相關信息
一、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 distributionCopyright (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 -ADatabase 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 | +---------------+---------------+------+-----+-------------------+-----------------------------------------------+ 5 rows in set (0.00 sec)②顯示mysql數據庫中所有表信息
mysql> SHOW TABLE STATUS FROM mysql; +---------------------------+--------+---------+------------+------+----------------+-------------+-----------------+--------------+-----------+----------------+---------------------+---------------------+------------+--------------------+----------+---------------------------------------+-----------------------------------------+ | Name | Engine | Version | Row_format | Rows | Avg_row_length | Data_length | Max_data_length | Index_length | Data_free | Auto_increment | Create_time | Update_time | Check_time | Collation | Checksum | Create_options | Comment | +---------------------------+--------+---------+------------+------+----------------+-------------+-----------------+--------------+-----------+----------------+---------------------+---------------------+------------+--------------------+----------+---------------------------------------+-----------------------------------------+ | columns_priv | InnoDB | 10 | Dynamic | 0 | 0 | 16384 | 0 | 0 | 4194304 | NULL | 2021-04-23 00:47:17 | NULL | NULL | utf8_bin | NULL | stats_persistent=0 | Column privileges | | component | InnoDB | 10 | Dynamic | 0 | 0 | 16384 | 0 | 0 | 4194304 | 1 | 2021-04-23 00:47:17 | NULL | NULL | utf8_general_ci | NULL | | Components | | db | InnoDB | 10 | Dynamic | 2 | 8192 | 16384 | 0 | 16384 | 4194304 | NULL | 2021-04-23 00:47:17 | NULL | NULL | utf8_bin | NULL | stats_persistent=0 | Database privileges | | default_roles | InnoDB | 10 | Dynamic | 0 | 0 | 16384 | 0 | 0 | 4194304 | NULL | 2021-04-23 00:47:17 | NULL | NULL | utf8_bin | NULL | stats_persistent=0 | Default roles | | engine_cost | InnoDB | 10 | Dynamic | 2 | 8192 | 16384 | 0 | 0 | 4194304 | NULL | 2021-04-23 00:47:17 | NULL | NULL | utf8_general_ci | NULL | stats_persistent=0 | | | func | InnoDB | 10 | Dynamic | 0 | 0 | 16384 | 0 | 0 | 4194304 | NULL | 2021-04-23 00:47:17 | NULL | NULL | utf8_bin | NULL | stats_persistent=0 | User defined functions | | general_log | CSV | 10 | Dynamic | 2 | 0 | 0 | 0 | 0 | 0 | NULL | 2021-04-23 00:47:17 | NULL | NULL | utf8_general_ci | NULL | | General log | | global_grants | InnoDB | 10 | Dynamic | 21 | 780 | 16384 | 0 | 0 | 4194304 | NULL | 2021-04-23 00:47:17 | 2021-05-07 11:42:36 | NULL | utf8_bin | NULL | stats_persistent=0 | Extended global grants | | gtid_executed | InnoDB | 10 | Dynamic | 0 | 0 | 16384 | 0 | 0 | 4194304 | NULL | 2021-04-23 00:47:17 | NULL | NULL | utf8mb4_0900_ai_ci | NULL | | | | help_category | InnoDB | 10 | Dynamic | 40 | 409 | 16384 | 0 | 16384 | 4194304 | NULL | 2021-04-23 00:47:17 | NULL | NULL | utf8_general_ci | NULL | stats_persistent=0 | help categories | | help_keyword | InnoDB | 10 | Dynamic | 658 | 149 | 98304 | 0 | 81920 | 4194304 | NULL | 2021-04-23 00:47:17 | NULL | NULL | utf8_general_ci | NULL | stats_persistent=0 | help keywords | | help_relation | InnoDB | 10 | Dynamic | 993 | 82 | 81920 | 0 | 0 | 4194304 | NULL | 2021-04-23 00:47:17 | NULL | NULL | utf8_general_ci | NULL | stats_persistent=0 | keyword-topic relation | | help_topic | InnoDB | 10 | Dynamic | 722 | 2201 | 1589248 | 0 | 81920 | 4194304 | NULL | 2021-04-23 00:47:17 | NULL | NULL | utf8_general_ci | NULL | stats_persistent=0 | help topics | | innodb_index_stats | InnoDB | 10 | Dynamic | 10 | 1638 | 16384 | 0 | 0 | 4194304 | NULL | 2021-04-22 16:47:16 | NULL | NULL | utf8_bin | NULL | row_format=DYNAMIC stats_persistent=0 | | | innodb_table_stats | InnoDB | 10 | Dynamic | 3 | 5461 | 16384 | 0 | 0 | 4194304 | NULL | 2021-04-22 16:47:16 | NULL | NULL | utf8_bin | NULL | row_format=DYNAMIC stats_persistent=0 | | | password_history | InnoDB | 10 | Dynamic | 0 | 0 | 16384 | 0 | 0 | 4194304 | NULL | 2021-04-23 00:47:17 | NULL | NULL | utf8_bin | NULL | stats_persistent=0 | Password history for user accounts | | plugin | InnoDB | 10 | Dynamic | 0 | 0 | 16384 | 0 | 0 | 4194304 | NULL | 2021-04-23 00:47:17 | NULL | NULL | utf8_general_ci | NULL | stats_persistent=0 | MySQL plugins | | procs_priv | InnoDB | 10 | Dynamic | 0 | 0 | 16384 | 0 | 16384 | 4194304 | NULL | 2021-04-23 00:47:17 | NULL | NULL | utf8_bin | NULL | stats_persistent=0 | Procedure privileges | | proxies_priv | InnoDB | 10 | Dynamic | 1 | 16384 | 16384 | 0 | 16384 | 4194304 | NULL | 2021-04-23 00:47:17 | NULL | NULL | utf8_bin | NULL | stats_persistent=0 | User proxy privileges | | role_edges | InnoDB | 10 | Dynamic | 0 | 0 | 16384 | 0 | 0 | 4194304 | NULL | 2021-04-23 00:47:17 | NULL | NULL | utf8_bin | NULL | stats_persistent=0 | Role hierarchy and role grants | | server_cost | InnoDB | 10 | Dynamic | 6 | 2730 | 16384 | 0 | 0 | 4194304 | NULL | 2021-04-23 00:47:17 | NULL | NULL | utf8_general_ci | NULL | stats_persistent=0 | | | servers | InnoDB | 10 | Dynamic | 0 | 0 | 16384 | 0 | 0 | 4194304 | NULL | 2021-04-23 00:47:17 | NULL | NULL | utf8_general_ci | NULL | stats_persistent=0 | MySQL Foreign Servers table | | slave_master_info | InnoDB | 10 | Dynamic | 0 | 0 | 16384 | 0 | 0 | 4194304 | NULL | 2021-04-23 00:47:17 | NULL | NULL | utf8_general_ci | NULL | stats_persistent=0 | Master Information | | slave_relay_log_info | InnoDB | 10 | Dynamic | 0 | 0 | 16384 | 0 | 0 | 4194304 | NULL | 2021-04-23 00:47:17 | NULL | NULL | utf8_general_ci | NULL | stats_persistent=0 | Relay Log Information | | slave_worker_info | InnoDB | 10 | Dynamic | 0 | 0 | 16384 | 0 | 0 | 4194304 | NULL | 2021-04-23 00:47:17 | NULL | NULL | utf8_general_ci | NULL | stats_persistent=0 | Worker Information | | slow_log | CSV | 10 | Dynamic | 2 | 0 | 0 | 0 | 0 | 0 | NULL | 2021-04-23 00:47:17 | NULL | NULL | utf8_general_ci | NULL | | Slow log | | tables_priv | InnoDB | 10 | Dynamic | 2 | 8192 | 16384 | 0 | 16384 | 4194304 | NULL | 2021-04-23 00:47:17 | NULL | NULL | utf8_bin | NULL | stats_persistent=0 | Table privileges | | time_zone | InnoDB | 10 | Dynamic | 0 | 0 | 16384 | 0 | 0 | 4194304 | 1 | 2021-04-23 00:47:17 | NULL | NULL | utf8_general_ci | NULL | stats_persistent=0 | Time zones | | time_zone_leap_second | InnoDB | 10 | Dynamic | 0 | 0 | 16384 | 0 | 0 | 4194304 | NULL | 2021-04-23 00:47:17 | NULL | NULL | utf8_general_ci | NULL | stats_persistent=0 | Leap seconds information for time zones | | time_zone_name | InnoDB | 10 | Dynamic | 0 | 0 | 16384 | 0 | 0 | 4194304 | NULL | 2021-04-23 00:47:17 | NULL | NULL | utf8_general_ci | NULL | stats_persistent=0 | Time zone names | | time_zone_transition | InnoDB | 10 | Dynamic | 0 | 0 | 16384 | 0 | 0 | 4194304 | NULL | 2021-04-23 00:47:17 | NULL | NULL | utf8_general_ci | NULL | stats_persistent=0 | Time zone transitions | | time_zone_transition_type | InnoDB | 10 | Dynamic | 0 | 0 | 16384 | 0 | 0 | 4194304 | NULL | 2021-04-23 00:47:17 | NULL | NULL | utf8_general_ci | NULL | stats_persistent=0 | Time zone transition types | | user | InnoDB | 10 | Dynamic | 4 | 4096 | 16384 | 0 | 0 | 4194304 | NULL | 2021-04-23 00:47:17 | 2021-05-07 11:42:36 | NULL | utf8_bin | NULL | stats_persistent=0 | Users and global privileges | +---------------------------+--------+---------+------------+------+----------------+-------------+-----------------+--------------+-----------+----------------+---------------------+---------------------+------------+--------------------+----------+---------------------------------------+-----------------------------------------+ 33 rows in set (0.01 sec)4.創建數據庫的表
①創建新數據庫
mysql> create DATABASE huawei; Query OK, 1 row affected (0.04 sec)mysql> show databases; +--------------------+ | Database | +--------------------+ | huawei | | information_schema | | mysql | | performance_schema | | sys | +--------------------+ 5 rows in set (0.01 sec)常用創建數據方式為默認編碼格式為utf8格式。
mysql> create database zabbix character set utf8 collate utf8_bin; Query OK, 1 row affected, 2 warnings (0.06 sec)②創建用戶表
mysql> create table student(id int auto_increment primary key,name varchar(16) not null,age int,sex char(1)); Query OK, 0 rows affected (0.07 sec)③查看當前使用數據庫
mysql> select database(); +------------+ | database() | +------------+ | huawei | +------------+ 1 row in set (0.00 sec)④查看當前數據庫的表
mysql> describe student; +-------+-------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-------+-------------+------+-----+---------+----------------+ | id | int(11) | NO | PRI | NULL | auto_increment | | name | varchar(16) | NO | | NULL | | | age | int(11) | YES | | NULL | | | sex | char(1) | YES | | NULL | | +-------+-------------+------+-----+---------+----------------+ 4 rows in set (0.00 sec)⑤查看字符集
mysql> show charset; +----------+---------------------------------+---------------------+--------+ | Charset | Description | Default collation | Maxlen | +----------+---------------------------------+---------------------+--------+ | armscii8 | ARMSCII-8 Armenian | armscii8_general_ci | 1 | | ascii | US ASCII | ascii_general_ci | 1 | | big5 | Big5 Traditional Chinese | big5_chinese_ci | 2 | | binary | Binary pseudo charset | binary | 1 | | cp1250 | Windows Central European | cp1250_general_ci | 1 | | cp1251 | Windows Cyrillic | cp1251_general_ci | 1 | | cp1256 | Windows Arabic | cp1256_general_ci | 1 | | cp1257 | Windows Baltic | cp1257_general_ci | 1 | | cp850 | DOS West European | cp850_general_ci | 1 | | cp852 | DOS Central European | cp852_general_ci | 1 | | cp866 | DOS Russian | cp866_general_ci | 1 | | cp932 | SJIS for Windows Japanese | cp932_japanese_ci | 2 | | dec8 | DEC West European | dec8_swedish_ci | 1 | | eucjpms | UJIS for Windows Japanese | eucjpms_japanese_ci | 3 | | euckr | EUC-KR Korean | euckr_korean_ci | 2 | | gb18030 | China National Standard GB18030 | gb18030_chinese_ci | 4 | | gb2312 | GB2312 Simplified Chinese | gb2312_chinese_ci | 2 | | gbk | GBK Simplified Chinese | gbk_chinese_ci | 2 | | geostd8 | GEOSTD8 Georgian | geostd8_general_ci | 1 | | greek | ISO 8859-7 Greek | greek_general_ci | 1 | | hebrew | ISO 8859-8 Hebrew | hebrew_general_ci | 1 | | hp8 | HP West European | hp8_english_ci | 1 | | keybcs2 | DOS Kamenicky Czech-Slovak | keybcs2_general_ci | 1 | | koi8r | KOI8-R Relcom Russian | koi8r_general_ci | 1 | | koi8u | KOI8-U Ukrainian | koi8u_general_ci | 1 | | latin1 | cp1252 West European | latin1_swedish_ci | 1 | | latin2 | ISO 8859-2 Central European | latin2_general_ci | 1 | | latin5 | ISO 8859-9 Turkish | latin5_turkish_ci | 1 | | latin7 | ISO 8859-13 Baltic | latin7_general_ci | 1 | | macce | Mac Central European | macce_general_ci | 1 | | macroman | Mac West European | macroman_general_ci | 1 | | sjis | Shift-JIS Japanese | sjis_japanese_ci | 2 | | swe7 | 7bit Swedish | swe7_swedish_ci | 1 | | tis620 | TIS620 Thai | tis620_thai_ci | 1 | | ucs2 | UCS-2 Unicode | ucs2_general_ci | 2 | | ujis | EUC-JP Japanese | ujis_japanese_ci | 3 | | utf16 | UTF-16 Unicode | utf16_general_ci | 4 | | utf16le | UTF-16LE Unicode | utf16le_general_ci | 4 | | utf32 | UTF-32 Unicode | utf32_general_ci | 4 | | utf8 | UTF-8 Unicode | utf8_general_ci | 3 | | utf8mb4 | UTF-8 Unicode | utf8mb4_0900_ai_ci | 4 | +----------+---------------------------------+---------------------+--------+ 41 rows in set (0.01 sec)五、查詢表其他相關信息
mysql> show create table student; +---------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | Table | Create Table | +---------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | student | CREATE TABLE `student` (`id` int(11) NOT NULL AUTO_INCREMENT,`name` varchar(16) NOT NULL,`age` int(11) DEFAULT NULL,`sex` char(1) DEFAULT NULL,PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci | +---------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ 1 row in set (0.03 sec)mysql> show index from student; +---------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+------------+ | Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment | Visible | Expression | +---------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+------------+ | student | 0 | PRIMARY | 1 | id | A | 0 | NULL | NULL | | BTREE | | | YES | NULL | +---------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+------------+ 1 row in set (0.09 sec)總結
以上是生活随笔為你收集整理的Linux下Mysql数据库的基础操作的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Linux脚本实战之检测网卡流量
- 下一篇: Linux下Mysql的查询用法