IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 系统运维 -> Linux的数据库学习 -> 正文阅读

[系统运维]Linux的数据库学习

  • 数据库?????

????????数据库是按照数据结构来组织、存储和管理数据的,是建立在计算机存储设备上的仓库

  • 数据库包安装

//下载mariadb数据库包
[root@localhost ~]# yum install mariadb-server -y -y

//启动数据库服务
[root@localhost ~]# systemctl restart mariadb

//初始化数据库
[root@localhost ~]# mysql_secure_installation 

NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!

In order to log into MariaDB to secure it, we'll need the current
password for the root user.  If you've just installed MariaDB, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.

Enter current password for root (enter for none):         
        #此时需要进行配置确认

//比较碍事,直接关闭防火墙
[root@localhost ~]# systemctl stop firewalld

    #!!!数据库的结尾需要加上";" !!!#
  • 增删改查等基本操作

//此时登录数据库,使用刚才设置的密码
u            连接mysql服务器的用户名
h            mysql服务器的ip地址/主机名
p            连接mysql服务器的密码

[root@localhost ~]# mysql -uroot -p123456
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 24
Server version: 10.3.28-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> 

//显示当前存在多少数据库
MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
+--------------------+
3 rows in set (0.001 sec)

//退出数据库系统
MariaDB [(none)]> quit
MariaDB [(none)]> exit
?
//切换到其他数据库下
MariaDB [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

//显示查看数据库的表
MariaDB [mysql]> show tables ;
+---------------------------+
| Tables_in_mysql           |
+---------------------------+
| column_stats              |
| columns_priv              |
| db                        |
| event                     |
| func                      |
| general_log               |
| gtid_slave_pos            |
| help_category             |
| help_keyword              |
| help_relation             |
| help_topic                |
| host                      |
| index_stats               |
| innodb_index_stats        |
| innodb_table_stats        |
| plugin                    |
| proc                      |
| procs_priv                |
| proxies_priv              |
| roles_mapping             |
| servers                   |
| slow_log                  |
| table_stats               |
| tables_priv               |
| time_zone                 |
| time_zone_leap_second     |
| time_zone_name            |
| time_zone_transition      |
| time_zone_transition_type |
| transaction_registry      |
| user                      |
+---------------------------+
31 rows in set (0.000 sec)

//查看数据表的表结构
MariaDB [mysql]> desc user;
+------------------------+-----------------------------------+------+-----+----------+-------+
| Field                  | Type                              | Null | Key | Default  | Extra |
+------------------------+-----------------------------------+------+-----+----------+-------+
| Host                   | char(60)                          | NO   | PRI |          |       |
| User                   | char(80)                          | NO   | PRI |          |       |
| Password               | char(41)                          | NO   |     |          |       |
| Select_priv            | enum('N','Y')                     | NO   |     | N        |       |
| Insert_priv            | enum('N','Y')                     | NO   |     | N        |       |
| Update_priv            | enum('N','Y')                     | NO   |     | N        |       |
| Delete_priv            | enum('N','Y')                     | NO   |     | N        |       |
| Create_priv            | enum('N','Y')                     | NO   |     | N        |       |
| Drop_priv              | enum('N','Y')                     | NO   |     | N        |       |
| Reload_priv            | enum('N','Y')                     | NO   |     | N        |       |
| Shutdown_priv          | enum('N','Y')                     | NO   |     | N        |       |
| Process_priv           | enum('N','Y')                     | NO   |     | N        |       |
| File_priv              | enum('N','Y')                     | NO   |     | N        |       |
| Grant_priv             | enum('N','Y')                     | NO   |     | N        |       |
| References_priv        | enum('N','Y')                     | NO   |     | N        |       |
| Index_priv             | enum('N','Y')                     | NO   |     | N        |       |
| Alter_priv             | enum('N','Y')                     | NO   |     | N        |       |
| Show_db_priv           | enum('N','Y')                     | NO   |     | N        |       |
| Super_priv             | enum('N','Y')                     | NO   |     | N        |       |
| Create_tmp_table_priv  | enum('N','Y')                     | NO   |     | N        |       |
| Lock_tables_priv       | enum('N','Y')                     | NO   |     | N        |       |
| Execute_priv           | enum('N','Y')                     | NO   |     | N        |       |
| Repl_slave_priv        | enum('N','Y')                     | NO   |     | N        |       |
| Repl_client_priv       | enum('N','Y')                     | NO   |     | N        |       |
| Create_view_priv       | enum('N','Y')                     | NO   |     | N        |       |
| Show_view_priv         | enum('N','Y')                     | NO   |     | N        |       |
| Create_routine_priv    | enum('N','Y')                     | NO   |     | N        |       |
| Alter_routine_priv     | enum('N','Y')                     | NO   |     | N        |       |
| Create_user_priv       | enum('N','Y')                     | NO   |     | N        |       |
| Event_priv             | enum('N','Y')                     | NO   |     | N        |       |
| Trigger_priv           | enum('N','Y')                     | NO   |     | N        |       |
| Create_tablespace_priv | enum('N','Y')                     | NO   |     | N        |       |
| Delete_history_priv    | enum('N','Y')                     | NO   |     | N        |       |
| ssl_type               | enum('','ANY','X509','SPECIFIED') | NO   |     |          |       |
| ssl_cipher             | blob                              | NO   |     | NULL     |       |
| x509_issuer            | blob                              | NO   |     | NULL     |       |
| x509_subject           | blob                              | NO   |     | NULL     |       |
| max_questions          | int(11) unsigned                  | NO   |     | 0        |       |
| max_updates            | int(11) unsigned                  | NO   |     | 0        |       |
| max_connections        | int(11) unsigned                  | NO   |     | 0        |       |
| max_user_connections   | int(11)                           | NO   |     | 0        |       |
| plugin                 | char(64)                          | NO   |     |          |       |
| authentication_string  | text                              | NO   |     | NULL     |       |
| password_expired       | enum('N','Y')                     | NO   |     | N        |       |
| is_role                | enum('N','Y')                     | NO   |     | N        |       |
| default_role           | char(80)                          | NO   |     |          |       |
| max_statement_time     | decimal(12,6)                     | NO   |     | 0.000000 |       |
+------------------------+-----------------------------------+------+-----+----------+-------+
47 rows in set (0.001 sec)

//查询host用户在mysql表中
MariaDB [mysql]> select host from user ;
+-----------------------+
| host                  |
+-----------------------+
| 127.0.0.1             |
| ::1                   |
| localhost             |
| localhost.localdomain |
+-----------------------+
4 rows in set (0.001 sec)

//创建一个学生表(序号,姓名,性别,生日)
MariaDB [XXX]> create table student(number int(5),name varchar(10),sex varchar(5),birthday date);
Query OK, 0 rows affected (0.042 sec)

//插入几条数据
MariaDB [XXX]> insert into student (number,name,sex,birthday) values (1,"lxb","boy",20000916);
Query OK, 1 row affected (0.001 sec)

MariaDB [XXX]> insert into student (number,name,sex,birthday) values (2,"lsp","boy",20000917);
Query OK, 1 row affected (0.001 sec)

MariaDB [XXX]> insert into student (number,name,sex,birthday) values (3,"lxb","girl",20000917);
Query OK, 1 row affected (0.001 sec)

//搜索数据
MariaDB [XXX]> select * from student ;
+--------+------+------+------------+
| number | name | sex  | birthday   |
+--------+------+------+------------+
|      1 | lxb  | boy  | 2000-09-16 |
|      2 | lsp  | boy  | 2000-09-17 |
|      3 | lxb  | girl | 2000-09-17 |
+--------+------+------+------------+
3 rows in set (0.000 sec)

//搜索sex为girl的
MariaDB [XXX]> select * from student  where sex="girl";
+--------+------+------+------------+
| number | name | sex  | birthday   |
+--------+------+------+------------+
|      3 | lxb  | girl | 2000-09-17 |
+--------+------+------+------------+
1 row in set (0.000 sec)

//搜索name是lxb的
MariaDB [XXX]> select * from student  where name="lxb";
+--------+------+------+------------+
| number | name | sex  | birthday   |
+--------+------+------+------------+
|      1 | lxb  | boy  | 2000-09-16 |
|      3 | lxb  | girl | 2000-09-17 |
+--------+------+------+------------+
2 rows in set (0.000 sec)

//搜索出name是lxb,sex是girl的
MariaDB [XXX]> select * from student  where name="lxb" and sex="girl";
+--------+------+------+------------+
| number | name | sex  | birthday   |
+--------+------+------+------------+
|      3 | lxb  | girl | 2000-09-17 |
+--------+------+------+------------+
1 row in set (0.000 sec)

//修改number为1的学生的name为lb
MariaDB [XXX]> update student set name="lb" where number="1";
Query OK, 1 row affected (1.623 sec)
Rows matched: 1  Changed: 1  Warnings: 0

//查看数据库,已经修改
MariaDB [XXX]> select * from student 
    -> ;
+--------+------+------+------------+
| number | name | sex  | birthday   |
+--------+------+------+------------+
|      1 | lb   | boy  | 2000-09-16 |
|      2 | lsp  | boy  | 2000-09-17 |
|      3 | lxb  | girl | 2000-09-17 |
+--------+------+------+------------+
3 rows in set (0.000 sec)

//删除表中name为lb的数据
MariaDB [XXX]> delete from student where name="lb" ;
Query OK, 1 row affected (0.001 sec)

MariaDB [XXX]> select * from student ;
+--------+------+------+------------+
| number | name | sex  | birthday   |
+--------+------+------+------------+
|      2 | lsp  | boy  | 2000-09-17 |
|      3 | lxb  | girl | 2000-09-17 |
+--------+------+------+------------+
2 rows in set (0.000 sec)
  • 用户的管理和访问权限的控制

//创建数据库登陆用户
MariaDB [mysql]> create user lsp@localhost identified by '123456';
Query OK, 0 rows affected (0.000 sec)
MariaDB [mysql]> create user lxb@localhost identified by "123456";
Query OK, 0 rows affected (0.000 sec)

//查看
MariaDB [mysql]>  select host,user,password from user;
+-----------------------+------+-------------------------------------------+
| host                  | user | password                                  |
+-----------------------+------+-------------------------------------------+
| localhost             | root | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
| localhost.localdomain | root | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
| 127.0.0.1             | root | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
| ::1                   | root | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
| localhost             | lsp  | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
| localhost             | lxb  | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
+-----------------------+------+-------------------------------------------+
6 rows in set (0.000 sec)

?
//查看当前使用用户
MariaDB [(none)]> select user();
+----------------+
| user()         |
+----------------+
| root@localhost |
+----------------+
1 row in set (0.000 sec)

//查看当前用户的数据库
MariaDB [(none)]>  select database();
+------------+
| database() |
+------------+
| mysql      |
+------------+
1 row in set (0.000 sec)
?
//重新登陆
[root@localhost ~]# mysql -ulxb -p 123456

//查看可以访问的数据库
MariaDB [(none)]> show databases;

//给lxb一张表的权限
MariaDB [(none)]> grant select,update,insert,delete on mysql.person to lxb@localhost;?

//退出,重新登陆lxb
[root@localhost ~]# mysql -ulxb -p123456 

//查询的权限
MariaDB [(none)]> 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

//增,删,改,查的权限
MariaDB [mysql]> select * from person;
MariaDB [mysql]> insert person (number,name,birthday) value (3,"xiaoming",20180303);
MariaDB [mysql]> update person set name="haha" where number=1
MariaDB [mysql]> delete from person where number=1;
?
//使用root用户登录,移除lxb删除的权限
MariaDB [(none)]> revoke delete on mysql.person from lxb@localhost;
//权限被拒绝
MariaDB [mysql]> delete from person where number=3 ;
  • 备份和还原

//备份数据库的所有表
[root@localhost ~]# mysqldump -u root -p123456 > /mysql_202212.dump

//使用root用户登录数据库
[root@localhost ~]# mysqldump -u root -p123456
//查询person表
MariaDB [mysql]> select * from person;
    ... ...
//删除person表
MariaDB [mysql]> drop table person;


//此时退出系统,进行还原操作
[root@localhost ~]# mysql -u root -p123456  < /mysql_202212.dum
    #或source读入表信息
    [root@localhost ~]# source  /mysql_202212.dum

//重新登陆数据库
[root@localhost ~]# mysql -u root -p123456
//查看person表
MariaDB [mysql]> select * from person;
    ... ...
  系统运维 最新文章
配置小型公司网络WLAN基本业务(AC通过三层
如何在交付运维过程中建立风险底线意识,提
快速传输大文件,怎么通过网络传大文件给对
从游戏服务端角度分析移动同步(状态同步)
MySQL使用MyCat实现分库分表
如何用DWDM射频光纤技术实现200公里外的站点
国内顺畅下载k8s.gcr.io的镜像
自动化测试appium
ctfshow ssrf
Linux操作系统学习之实用指令(Centos7/8均
上一篇文章      下一篇文章      查看所有文章
加:2022-01-03 16:30:34  更:2022-01-03 16:30:59 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年11日历 -2024/11/16 6:33:09-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码