云计算第三阶段---DBA数据库管理

Day1

一.数据库概述

        计算机中数据存放的仓库,可以按数据类型划分数据库,又可以在数据库中划分更细节的分类存放到相应的表中。

抽象来说就是生活中的菜市场、五金店之类的场所,根据用途开设;划分广泛的还有百货商场、批发市场等。

生活中的数据包括 图片、文字、视频、音乐等各种各种渠道信息。

数据库常见格式:.sql               #方便后续学习过程中,数据库导入导出内容测试,

常见数据库系统有以下几种:

我上大学时候使用的是MySQLTomcat两种类型,同学们想要下载推荐都下个免费版,或者开源的免费版。因为在工作中有的数据库软件公司发现你用的是破解版,时机到了会找你索要赔偿。

 ̄□ ̄||

数据库类型分为:

#tomcat图形数据库,就需要建模,而建模需要换成收费版,有包括建模的更多功能#

二.部署MySQL服务

特点:

部署MySQL环境:

1 案例1:构建MySQL服务器

1.1 问题

  1. 在IP地址192.168.88.50主机和192.168.88.51主机上部署mysql服务
  2. 练习必备命令的使用

1.2 方案

准备2台虚拟机,要求如下:

表-1

#练习环境中,记得配置yum源、关闭selinux和防火墙

1.3 步骤

步骤一:安装软件

命令操作如下所示:
#装mysql-server自动补全依赖,以防万一,就一起下。

mysql-server 提供服务软件 

mysql 提供命令软件

//安装提供服务和命令软件
[root@mysql50 ~]# yum -y install mysql-server  mysql 
软件已安装
[root@mysql50 ~]# rpm -q mysql-server  mysql
mysql-server-8.0.26-1.module+el8.4.0+652+6de068a7.x86_64
mysql-8.0.26-1.module+el8.4.0+652+6de068a7.x86_64
[root@mysql50 ~]#
//查看软件信息
[root@mysql50 ~]# rpm -qi mysql-server 
Name        : mysql-server
Version     : 8.0.26
Release     : 1.module+el8.4.0+652+6de068a7
Architecture: x86_64
Install Date: 2023年03月13日 星期一 12时09分38秒
Group       : Unspecified
Size        : 126674945
License     : GPLv2 with exceptions and LGPLv2 and BSD
Signature   : RSA/SHA256, 2021年09月22日 星期三 07时27分14秒, Key ID 15af5dac6d745a60
Source RPM  : mysql-8.0.26-1.module+el8.4.0+652+6de068a7.src.rpm
Build Date  : 2021年09月22日 星期三 07时06分32秒
Build Host  : ord1-prod-x86build005.svc.aws.rockylinux.org
Relocations : (not relocatable)
Packager    : infrastructure@rockylinux.org
Vendor      : Rocky
URL         : http://www.mysql.com
Summary     : The MySQL server and related files
Description :
MySQL is a multi-user, multi-threaded SQL database server. MySQL is a
client/server implementation consisting of a server daemon (mysqld)
and many different client programs and libraries. This package contains
the MySQL server and some accompanying files and directories.
[root@mysql50 ~]# systemctl  start  mysqld  //启动服务
[root@mysql50 ~]# systemctl  enable  mysqld  //开机运行
         

步骤二:查看端口号和进程名

  1. [root@mysql50 ~]# ps -C mysqld //仅查看mysqld进程
  2. PID TTY TIME CMD
  3. 21912 ? 00:00:00 mysqld
  4. [root@mysql50 ~]#
  5. [root@mysql50 ~]# ss -utnlp | grep 3306 查看端口
  6. tcp LISTEN 0 70 *:33060 *:* users:(("mysqld",pid=21912,fd=22))
  7. tcp LISTEN 0 128 *:3306 *:* users:(("mysqld",pid=21912,fd=25))
  8. [root@mysql50 ~]#
  9. [root@mysql50 ~]# netstat -utnlp | grep mysqld //仅查看mysqld进程
  10. tcp6 0 0 :::33060 :::* LISTEN 21912/mysqld
  11. tcp6 0 0 :::3306 :::* LISTEN 21912/mysqld
  12. [root@mysql50 ~]#

#MySQL 8中的3306端口是MySQL服务默认使用的端口,主要用于建立客户端与MySQL服务器之间的连接。

支持协议: TCP。 

#两个配置文件都是可以tab补全的

主配置文件位置: /etc/my.cnf.d/mysql-server.conf   
错误日志文件: /var/log/mysql/mysqld.log

步骤三:连接服务。

说明: 数据库管理员本机登陆默认没有密码

  1. [root@mysql50 ~]# mysql //连接服务
  2. Welcome to the MySQL monitor. Commands end with ; or \g.
  3. Your MySQL connection id is 8
  4. Server version: 8.0.26 Source distribution
  5. Copyright (c) 2000, 2021, Oracle and/or its affiliates.
  6. Oracle is a registered trademark of Oracle Corporation and/or its
  7. affiliates. Other names may be trademarks of their respective
  8. owners.
  9. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  10. mysql> 登陆后的提示符
  11. mysql> exit //断开连接
  12. Bye
  13. [root@mysql50 ~]#

步骤四:配置第2台数据库服务器MySQL51。

命令操作如下所示:

  1. [root@mysql51 ~]# yum -y install mysql-server mysql
  2. [root@mysql51 ~]# systemctl start mysqld
  3. [root@mysql51 ~]# systemctl enable mysqld
  4. [root@mysql51 ~]# mysql
  5. mysql> exit
  6. Bye
  7. [root@mysql51 ~]#

三.基础查询语句

MySQL语句规范:

-P: 数据服务器端口   -h:IP地址  。 exit:退出数据库  ctrl+D也可以退出。

1.不区分大小写。   2. 语句以 “ ;” ,  “\g”,  “\G” 结尾。 

3.注释 : 

单行注释 :“ -- ”           多行注释:  /*  内容   /*

SQL语句分类

DQL查询语句DML 操作语言DDL 定义语言 TCL 事务控制语言DCL 控制语言
【不修改本身数据】selectinsert插入数据、update更新数据、delete删除数据。create、alter、 drop。 针对:表库索引

commit提交 ,rollback:回滚savepoint:设置保存点。

set transaction 设置事务特性

grant:授予权限。

revoke:撤销权限

查看MySQL版本: select version();  
查看系统时间: select now();
查看当前用户: select  user();

脚本案例:

  1. [root@mysql50 ~]# mysql //连接服务
  2. mysql> select version() ; //查看数据库软件版本
  3. +-----------+
  4. | version() |
  5. +-----------+
  6. | 8.0.26 |
  7. +-----------+
  8. 1 row in set (0.00 sec)
  9. mysql> select user() ; //查看登陆的用户和客户端地址
  10. +----------------+
  11. | user() |
  12. +----------------+
  13. | root@localhost | 管理员root本机登陆
  14. +----------------+
  15. 1 row in set (0.00 sec)
  16. mysql> show databases; //查看已有的库
  17. +--------------------+
  18. | Database |
  19. +--------------------+
  20. | information_schema |
  21. | mysql |
  22. | performance_schema |
  23. | sys |
  24. +--------------------+
  25. 4 rows in set (0.00 sec)
  26. mysql>

以下是MySQL自带数据库介绍

默认4个库 不可以删除,存储的是 服务运行时加载的不同功能的程序和数据。

information_schema:是MySQL数据库提供的一个虚拟的数据库,存储了MySQL数据库中的相关信息,比如数据库、表、列、索引、权限、角色等信息。它并不存储实际的数据,而是提供了一些视图和存储过程,用于查询和管理数据库的元数据信息。

mysql:存储了MySQL服务器的系统配置、用户、账号和权限信息等。它是MySQL数据库最基本的库,存储了MySQL服务器的核心信息。


#MySQL数据库中有user表,里面有一个user表,想要在其他软件连接MySQL数据库,就需要设置该表的user=root 位置,host的localhost:本地  设置为 % . 意思:所有机器都可以登录#


performance_schema:存储了MySQL数据库的性能指标、事件和统计信息等数据,可以用于性能分析和优化。

sys:是MySQL 8.0引入的一个新库,它基于information_schema和performance_schema视图,提供了更方便、更直观的方式来查询和管理MySQL数据库的元数据和性能数据。

  1. mysql> select database(); //查看当前在那个库里 null 表示没有在任何库里
  2. +------------+
  3. | database() |
  4. +------------+
  5. | NULL |
  6. +------------+
  7. 1 row in set (0.00 sec)
  8. mysql> use mysql ; //切换到mysql库
  9. mysql> select database(); // 再次显示所在的库
  10. +------------+
  11. | database() |
  12. +------------+
  13. | mysql |
  14. +------------+
  15. 1 row in set (0.00 sec)
  16. mysql> show tables; //显示库里已有的表
  17. +------------------------------------------------------+
  18. | Tables_in_mysql |
  19. +------------------------------------------------------+
  20. | columns_priv |
  21. | component |
  22. | db |
  23. | default_roles |
  24. | engine_cost |
  25. | func |
  26. | general_log |
  27. | global_grants |
  28. | gtid_executed |
  29. | help_category |
  30. | help_keyword |
  31. | help_relation |
  32. | help_topic |
  33. | innodb_index_stats |
  34. | innodb_table_stats |
  35. | password_history |
  36. | plugin |
  37. | procs_priv |
  38. | proxies_priv |
  39. | replication_asynchronous_connection_failover |
  40. | replication_asynchronous_connection_failover_managed |
  41. | replication_group_configuration_version |
  42. | replication_group_member_actions |
  43. | role_edges |
  44. | server_cost |
  45. | servers |
  46. | slave_master_info |
  47. | slave_relay_log_info |
  48. | slave_worker_info |
  49. | slow_log |
  50. | tables_priv |
  51. | time_zone |
  52. | time_zone_leap_second |
  53. | time_zone_name |
  54. | time_zone_transition |
  55. | time_zone_transition_type |
  56. | user |
  57. +------------------------------------------------------+
  58. 37 rows in set (0.00 sec)
  59. mysql> exit ; 断开连接
  60. Bye
  61. [root@mysql50 ~]#

设置数据库登录账号密码

案例2:密码管理

2.1 问题

1) 在192.168.88.50主机做如下练习:

  1. 设置root密码为123
  2. 修改root密码为 456
  3. 破解root密码为 123456

设置root密码为123

  1. [root@mysql50 ~]# mysqladmin -uroot -p password "123"
  2. Enter password: //敲回车
  3. mysqladmin: [Warning] Using a password on the command line interface can be insecure.
  4. Warning: Since password will be sent to server in plain text, use ssl connection to ensure password safety
  5. #设置完成后,不能直接用MySQL 进入数据库。需要使用用户密码。

修改root密码为 456

  1. [root@mysql50 ~]# mysqladmin -uroot -p123 password "456"
  2. mysqladmin: [Warning] Using a password on the command line interface can be insecure.
  3. Warning: Since password will be sent to server in plain text, use ssl connection to ensure password safety.

#修改新密码后,旧密码作废,需要设置新密码登录。


破解root密码

在mysql50主机做此练习

修改主配置文件

  1. [root@mysql50 ~]# vim /etc/my.cnf.d/mysql-server.cnf
  2. [mysqld]
  3. skip-grant-tables //手动添加此行 作用登陆时不验证密码
  4. ……
  5. ……
  6. :wq
  7. [root@mysql50 ~]# systemctl restart mysqld //重启服务 让服务以新配置运行

############配置了新东西,都需要重新启动服务才能看到结果。

连接服务

  1. [root@mysql50 ~]# mysql
  2. Welcome to the MySQL monitor. Commands end with ; or \g.
  3. Your MySQL connection id is 7
  4. Server version: 8.0.26 Source distribution
  5. Copyright (c) 2000, 2021, Oracle and/or its affiliates.
  6. Oracle is a registered trademark of Oracle Corporation and/or its
  7. affiliates. Other names may be trademarks of their respective
  8. owners.
  9. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

查看存放密码的表头名

  1. Mysql> desc mysql.user ;

删除不知道的密码

  1. mysql> update mysql.user set authentication_string="" where user="root" and host="localhost";

删除后的效果

  1. mysql> select host , user , authentication_string from mysql.user where user="root";
  2. +-----------+------+-------------------------------------------+
  3. | host | user | authentication_string |
  4. +-----------+------+-------------------------------------------+
  5. | localhost | root |                                          |
  6. +-----------+------+-------------------------------------------+
  7. 1 row in set (0.01 sec)
  8. mysql> exit; 断开连接

编辑配置文件 

  1. [root@mysql50 ~]# vim /etc/my.cnf.d/mysql-server.cnf
  2. [mysqld]
  3. #skip-grant-tables //破完密码需要添加注释。
  4. :wq
  5. [root@mysql50 ~]# systemctl restart mysqld //重启服务 作用让注释生效

无密码登陆

  1. [root@localhost ~]# mysql
  2. Welcome to the MySQL monitor. Commands end with ; or \g.
  3. Your MySQL connection id is 8
  4. Server version: 8.0.26 Source distribution
  5. Copyright (c) 2000, 2021, Oracle and/or its affiliates.
  6. Oracle is a registered trademark of Oracle Corporation and/or its
  7. affiliates. Other names may be trademarks of their respective
  8. owners.
  9. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

设置root用户本机登陆密码

  1. mysql> alter user root@"localhost" identified by "123456";
  2. Query OK, 0 rows affected (0.00 sec)
  3. mysql> exit 断开连接

不输密码无法登陆

  1. [root@localhost ~]# mysql
  2. ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)

使用破解的密码登陆

  1. [root@localhost ~]# mysql -uroot -p123456
  2. mysql: [Warning] Using a password on the command line interface can be insecure.
  3. Welcome to the MySQL monitor. Commands end with ; or \g.
  4. Your MySQL connection id is 10
  5. Server version: 8.0.26 Source distribution
  6. Copyright (c) 2000, 2021, Oracle and/or its affiliates.
  7. Oracle is a registered trademark of Oracle Corporation and/or its
  8. affiliates. Other names may be trademarks of their respective
  9. owners.
  10. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  11. mysql>
  12. mysql> 登陆成功
  13. mysql> show databases; 查看已有的库
  14. +--------------------+
  15. | Database |
  16. +--------------------+
  17. | information_schema |
  18. | mysql |
  19. | performance_schema |
  20. | sys |
  21. +--------------------+
  22. 4 rows in set (0.01 sec)
  1. [root@localhost ~]# mysql
  2. ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)

 案例3:筛选条件            

#没有文件的话,可以看看。或者网上下几个来测试。知识点是想通的#

3.1 问题

  1. 准备练习环境
  2. 练习数值比较
  3. 练习范围匹配
  4. 练习模糊匹配
  5. 练习正则匹配
  6. 练习逻辑比较
  7. 练习字符比较/空/非空
  8. 练习别名/去重/合并

3.2 方案

拷贝tarena.sql文件到mysql50主机里,然后使用tarena.sql创建练习使用的数据。

3.3 步骤

实现此案例需要按照如下步骤进行。#这是我们上课的教学环境。#

步骤一:准备练---+

步骤八:练习别名/去重/合并

定义别名使用 as 或 空格

 
  1. mysql> select name , homedir from tarena.user;
  2. mysql> select name as 用户名 , homedir 家目录 from tarena.user;

拼接 concat()

 
  1. mysqlcat(name,"-",uid) as 用户信息 from tarena.user where uid <= 5;
  2. +--------------+
  3. | bin-1 |
  4. | daemon-2 |
  5. | adm-3 |
  6. | lp-4 |
  7. | sync-5 |
  8. +--------------+
  9. 6 rows in set (0.00 sec)
  10. //拼接
  11. mysql> select concat(name , "-" , uid) as 用户信息 from tarena.user where uid <= 5;

多列拼接

 
  1. mysql> select concat(name , "-" , uid , "-" , gid) as 用户信息 from tarena.user where uid <= 5;
  2. +--------------+
  3. | 用户信息 |
  4. +--------------+
  5. | root-0-0 |
  6. | bin-1-1 |
  7. | daemon-2-2 |
  8. | adm-3-4 |
  9. | lp-4-7 |
  10. | sync-5-0 |
  11. +--------------+

去重显示 distinct 字段名列表

 
  1. //去重前输出
  2. mysql> select shell from tarena.user where shell in ("/bin/bash","/sbin/nologin") ;
  3. +---------------+
  4. | shell |
  5. +---------------+
  6. | /bin/bash |
  7. | /sbin/nologin |
  8. | /sbin/nologin |
  9. | /sbin/nologin |
  10. | /sbin/nologin |
  11. | /sbin/nologin |
  12. | /sbin/nologin |
  13. | /sbin/nologin |
  14. | /sbin/nologin |
  15. | /sbin/nologin |
  16. | /sbin/nologin |
  17. | /sbin/nologin |
  18. | /sbin/nologin |
  19. | /sbin/nologin |
  20. | /sbin/nologin |
  21. | /sbin/nologin |
  22. | /sbin/nologin |
  23. | /sbin/nologin |
  24. | /sbin/nologin |
  25. | /sbin/nologin |
  26. | /bin/bash |
  27. | /sbin/nologin |
  28. +---------------+
  29. 22 rows in set (0.00 sec)
  30. //去重后查看
  31. mysql> select distinct shell from tarena.user where shell in ("/bin/bash","/sbin/nologin") ;
  32. +---------------+
  33. | shell |
  34. +---------------+
  35. | /bin/bash |
  36. | /sbin/nologin |
  37. +---------------+
  38. 2 rows in set (0.01 sec)
  39. mysql>


4 案例4:安装图形软件

#可以在网络服务器上设置 数据库的增删改查

4.1 问题

  • 在IP地址192.168.88.50主机安装phpmyadmin软件
  • 客户端通过访问phpmyadmin软件管理数据库

4.2 方案

把用到的软件拷贝的虚拟机mysql50里

在mysql50主机,首先配置运行环境LNP,然后安装phpmyadmin软件,最后打开真机的浏览器输入phpmyadmin的网址访问。

4.3 步骤

实现此案例需要按照如下步骤进行。

步骤一:准备运行环境

命令操作如下所示:

  1. gcc 源码包编译工具
  2. unzip 提供解压.zip 压缩包命令
  3. make 源码软件编译命令
  4. pcre-devel 支持正则表达式
  5. zlib-devel 提供数据压缩命令
  6. [root@mysql50 ~]# yum -y install gcc unzip make pcre-devel zlib-devel //安装依赖
  7. [root@mysql50 ~]# tar -xf nginx-1.22.1.tar.gz //解压源码
  8. [root@mysql50 ~]# cd nginx-1.22.1 //进源码目录
  9. [root@mysql50 nginx-1.22.1]# ./configure //配置
  10. [root@mysql50 nginx-1.22.1]# make && make install //编译并安装
  11. [root@mysql50 nginx-1.22.1]# ls /usr/local/nginx/ //查看安装目录
  12. conf html logs sbin
  13. //修改主配置文件
  14. [root@mysql50 nginx-1.22.1]# vim /usr/local/nginx/conf/nginx.conf
  15. 43 location / {
  16. 44 root html;
  17. 45 index index.php index.html index.htm; //添加首页名
  18. 46 }
  19. 65 location ~ \.php$ {
  20. 66 root html;
  21. 67 fastcgi_pass 127.0.0.1:9000; //访问.php的请求转给本机的9000端口
  22. 68 fastcgi_index index.php;
  23. 69 #fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
  24. 70 include fastcgi.conf; //保存nginx变量文件
  25. 71 }
  26. :wq
  27. [root@mysql50 nginx-1.22.1]# /usr/local/nginx/sbin/nginx //启动服务
  28. [root@mysql50 nginx-1.22.1]# netstat -utnlp | grep 80 //查看端口
  29. tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 42182/nginx: master
  30. [root@mysql50 nginx-1.22.1]#

软件功能说明

  1. php 解释php代码
  2. php-devel php扩展包
  3. php-mysqlnd 连接mysql命令包
  4. php-json 支持json代码
  5. php-fpm 提供fpm服务

安装软件

  1. [root@mysql50 ~]# yum -y install php php-devel php-mysqlnd php-json php-fpm

修改主配置文件

  1. [root@mysql50 ~]# vim +38 /etc/php-fpm.d/www.conf
  2. listen = 127.0.0.1:9000  #监听端口设置,默认是80
  3. :wq
  4. [root@mysql50 ~]# systemctl start php-fpm //启动服务

查看端口  #mysqladmin   

 
  1. [root@mysql50 ~]# netstat -utnlp | grep 9000
  2. tcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN 67251/php-fpm: mast
  3. [root@mysql50 ~]#

编写php脚本

  1. [root@mysql50 ~]# vim /usr/local/nginx/html/test.php
  2. <?php
  3. $name = "qq" ;
  4. echo $name ;
  5. echo "\n" ;
  6. ?>

步骤二:安装phpmyadmin软件

解压软件

  1. [root@mysql50 ~]# which unzip || yum -y install unzip
  2. [root@mysql50 ~]# unzip phpMyAdmin-5.2.1-all-languages.zip //解压

移动并改名

  1. [root@mysql50 ~]# mv phpMyAdmin-5.2.1-all-languages /usr/local/nginx/html/phpmyadmin

创建主配置文件

  1. [root@mysql50 ~]# cd /usr/local/nginx/html/phpmyadmin/
  2. [root@mysql50 phpmyadmin]# cp config.sample.inc.php config.inc.php

步骤三:客户端访问

打开浏览器输入此网址 效果如图-1所示   

http://192.168.88.50/phpmyadmin

说明:输入数据库管理员root 和 密码 成功后如图-2所示

在数据库服务器里创建普通用户

  1. 创建库
  2. mysql> create database gamedb;
  3. Query OK, 1 row affected (0.01 sec)
  4. 创建用户
  5. mysql> create user dc@"localhost" identified by "123456";
  6. Query OK, 0 rows affected (0.01 sec)
  7. 授权权限
  8. mysql> grant all on gamedb.* to dc@"localhost" ;
  9. Query OK, 0 rows affected (0.00 sec)
  10. mysql>

客户端以上用户密码 登陆 mysqladmin 

可以看到


     第一天涉及到的内容太多了,软件依赖下载,配置  mysqladmin,mysql 和 具体语法的介绍,还有密码用户的授权管理。

 后续内容我放到下几篇文章,不然眼睛看不过来了。O(∩_∩)O

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值