Bootstrap

数据库的安装与配置

数据库安装与配置

一. 关系型数据库

1. 数据库模型:
1.1 层次模型
1.2 网状结构
1.3 关系模型
  • 二维关系:行(row)和列(column)
1.4 数据库管理系统:DBMS(DataBase Management System
  • 数据库管理系统:一台安装的数据库软件的主机

二. RDBMS专业名词

1.1 关系型数据库管理系统:RDBMS(Relational DataBase Management System)
  • MySQL:MySQL(5.7),MariaDB(10.0之后),Percona-Server
  • PostgreSQL:简称为pgsql
  • Oracle
  • MSSQL:MicroSoft ,SQLServer
  • redis,memcache 缓存
  • mongdb
  • SQLite,数据库的文件
1.2 SQL (Structure Query Language)结构化查询语言

结构化数据:数据库存放的数据

半结构化数据:文本文件

非结构化数据:图片,视频

1.3 约束(constraint):向数据表提供的数据要遵守的限制

表由行和列组成 记录:(行) 条 字段:(列) 个

  • 主键约束:一个或多个字段的组合,填入的数据必须能在本表中唯一标识本行,提供数据,不能为空

    • 一个表只能存在一个
  • 惟一键约束:个或多个字段的组合,填入的数据必须能在本表中唯一标识本行,允许为空

    • 一个表可以存在多个
  • 外键约束:一个表中的某字段可填入数据取决于另一个表的主键已有的数据

  • 检查性约束

1.4 索引:(优化数据库)将表中的字段中的数据复制一份另存到虚拟表,并且进行特定的顺序排序存放,以便于快速查找
2. 关系型数据库的常用组件
  • 数据库:database
  • 表:table,由行(row)和列(column)组成
  • 用户:user
  • 权限:privilege
  • 索引:index
  • 视图:view
  • 存储过程:procedure
  • 存储函数:function
  • 触发器:trigger
  • 事件调度器:event scheduler
3. SQL语句
3.1 DDL:(Data Defination Language)数据定义语言

针对数据库,表,用户,索引,视图,存储过程,存储函数,触发器,事件调度器

  • CREATE:创建 create,创建表的结构
  • DROP:删除 drop
  • ALTER:修改 alter
3.2 DML:(Data Manipulation Language)数据操纵语言

针对表,增删改查

  • INSERT:向表中插入数据 insert
  • DELETE:删除表中数据 delete
  • UPDATE:更新表中数据 update
  • SELECT:查询表中数据 select
3.3 DCL:(Data Control Language)数据控制语言
  • GRANT:授权 grant
  • REVOKE:移除授权 revoke

三. MySQL的安装与配置

1. 数据库安装方式
  • 源码安装:编译安装
  • 二进制格式的程序包,展开至特定路径,并经过简单配置后即可使用
  • 程序包管理器的程序包(yum或者rpm)
2.下载并安装mysql
[root@SYL4 ~]# wget http://dev.mysql.com/get/mysql57-community-release-el7-10.noarch.rpm
[root@SYL4 ~]# ls
anaconda-ks.cfg  mysql57-community-release-el7-10.noarch.rpm
[root@SYL4 ~]# yum list all|grep mysql57-community
Failed to set locale, defaulting to C.UTF-8
[root@SYL4 ~]# rpm -ivh mysql57-community-release-el7-10.noarch.rpm 
warning: mysql57-community-release-el7-10.noarch.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY
Verifying...                          ################################# [100%]
Preparing...                          ################################# [100%]
Updating / installing...
   1:mysql57-community-release-el7-10 ################################# [100%]
[root@SYL4 ~]# 
2.1 MySQL源网址
2.2 下载MySQL源
[root@SYL4 ~]# wget https://repo.mysql.com/yum/mysql-5.7-community/el/7/x86_64/mysql-community-client-5.7.37-1.el7.x86_64.rpm https://repo.mysql.com/yum/mysql-5.7-community/el/7/x86_64/mysql-community-common-5.7.37-1.el7.x86_64.rpm https://repo.mysql.com/yum/mysql-5.7-community/el/7/x86_64/mysql-community-devel-5.7.37-1.el7.x86_64.rpm https://repo.mysql.com/yum/mysql-5.7-community/el/7/x86_64/mysql-community-libs-5.7.37-1.el7.x86_64.rpm https://repo.mysql.com/yum/mysql-5.7-community/el/7/x86_64/mysql-community-server-5.7.37-1.el7.x86_64.rpm
2.3 安装mysql源
[root@SYL4 ~]# ls
anaconda-ks.cfg
mysql-community-client-5.7.37-1.el7.x86_64.rpm
mysql-community-common-5.7.37-1.el7.x86_64.rpm
mysql-community-devel-5.7.37-1.el7.x86_64.rpm
mysql-community-libs-5.7.37-1.el7.x86_64.rpm
mysql-community-server-5.7.37-1.el7.x86_64.rpm
[root@SYL4 ~]# yum -y install *.rpm

3. 配置MySQL

3.1 启动并设置开机自动启动
[root@SYL4 ~]# systemctl enable --now mysqld
[root@SYL4 ~]# systemctl status mysqld
● mysqld.service - MySQL Server
   Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enab>
   Active: active (running) since Mon 2022-04-18 17:31:04 CST; >
     Docs: man:mysqld(8)
           http://dev.mysql.com/doc/refman/en/using-systemd.html
  Process: 135835 ExecStart=/usr/sbin/mysqld --daemonize --pid->
  Process: 135442 ExecStartPre=/usr/bin/mysqld_pre_systemd (cod>
 Main PID: 135837 (mysqld)
    Tasks: 27 (limit: 11216)
   Memory: 301.1M
   CGroup: /system.slice/mysqld.service
           └─135837 /usr/sbin/mysqld --daemonize --pid-file=/va>

Apr 18 17:30:58 SYL4 systemd[1]: Starting MySQL Server...
Apr 18 17:31:04 SYL4 systemd[1]: Started MySQL Server.
3.2 确定MySQL端口是否监听
[root@SYL4 ~]# ss -antl
State  Recv-Q Send-Q Local Address:Port Peer Address:PortProcess
LISTEN 0      128          0.0.0.0:22        0.0.0.0:*          
LISTEN 0      128             [::]:22           [::]:*          
LISTEN 0      80                 *:3306            *:*          
[root@SYL4 ~]# 
3.3 设置密码
3.3.1 找出临时密码
[root@SYL4 ~]# grep 'password' /var/log/mysqld.log 
2022-04-18T09:31:00.145440Z 1 [Note] A temporary password is generated for root@localhost: 3W(phUO)_a0z
3.3.2 设置密码
[root@SYL4 ~]# mysql -uroot -p'3W(phUO)_a0z'
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.37

Copyright (c) 2000, 2022, Oracle and/or its affiliates.

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> set password = password('Run123456!');
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> quit
3.3.3 重新登录
[root@SYL4 ~]# mysql -uroot -p'Run123456!';
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.37 MySQL Community Server (GPL)

Copyright (c) 2000, 2022, Oracle and/or its affiliates.

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> quit
Bye
[root@SYL4 ~]# 
  • 为避免mysql自动升级,这里需要卸载最开始安装的yum源

  • [root@SYL4 ~]# rpm -e mysql57-community-release
    

4. mysql的程序组成

  • 客户端
    mysql:CLI交互式客户端程序
    mysql_secure_installation:安全初始化,强烈建议安装完以后执行此命令
    mysqldump:mysql备份工具
    mysqladmin:管理
  • 服务器端
    mysqld

5. 配置MySqL的权限

虚拟机的权限

主机ip服务器ip客户机ip
192.168.232.1192.168.232.132192.168.232.129
5.1 设置权限,与Navicat的连接
mysql> GRANT all ON *.* TO 'root'@'192.168.232.1' IDENTIFIED B
Y 'Run123456!';
Query OK, 0 rows affected, 1 warning (0.13 sec)

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

  • 关闭防火墙
[root@SYL4 ~]# systemctl disable --now firewalld
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@SYL4 ~]# setenforce 0
[root@SYL4 ~]# vim /etc/selinux/config 
[root@SYL4 ~]# cat /etc/selinux/config |grep SELINUX
# SELINUX= can take one of these three values:
SELINUX=disabled
# SELINUXTYPE= can take one of these three values:
SELINUXTYPE=targeted
[root@SYL4 ~]# 

  • 与Navicat的连接
    在这里插入图片描述

在这里插入图片描述

5.2 与客户机的连接
  • 设置权限

  • mysql> GRANT all ON *.* TO 'root'@'192.168.232.129' IDENTIFIED BY 'Run123456!';
    Query OK, 0 rows affected, 1 warning (0.18 sec)
    
    mysql> flush privileges;
    Query OK, 0 rows affected (0.18 sec)
    
    
  • 与客户机的连接

  • [root@SYL2 ~]# ip a
    2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
        link/ether 00:0c:29:ce:9c:21 brd ff:ff:ff:ff:ff:ff
        inet 192.168.232.129/24 brd 192.168.232.255 scope global noprefixroute ens33
           valid_lft forever preferred_lft forever
        inet6 fe80::20c:29ff:fece:9c21/64 scope link 
           valid_lft forever preferred_lft forever
    
    
    //客户机第二台虚拟机
    [root@SYL2 ~]# mysql -uroot -p'Run123456!' -h192.168.232.132
    mysql: [Warning] Using a password on the command line interface can be insecure.
    Welcome to the MySQL monitor.  Commands end with ; or \g.
    Your MySQL connection id is 16
    Server version: 5.7.37 MySQL Community Server (GPL)
    
    Copyright (c) 2000, 2022, Oracle and/or its affiliates.
    
    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> quit
    

6. mysql工具的使用

-uUSERNAME      //指定用户名,默认为root
-hHOST          //指定服务器主机,默认为localhost,推荐使用ip地址
-pPASSWORD      //指定用户的密码
-P#             //指定数据库监听的端口,这里的#需用实际的端口号代替,如-P3307
-V              //查看当前使用的mysql版本
-e              //不登录mysql执行sql语句后退出,常用于脚本
    
  • 查看当前使用的mysql版本
[root@SYL4 ~]# mysql -V
mysql  Ver 14.14 Distrib 5.7.37, for Linux (x86_64) using  EditLine wrapper
[root@SYL4 ~]# 
  • 显示数据库
mysql> SHOW DATABASES;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.30 sec)

mysql> quit
Bye
[root@SYL4 ~]# 
  • 不登录mysql执行sql语句后退出
[root@SYL4 ~]# mysql -uroot -p'Run123456!' -e 'SHOW DATABASES';
mysql: [Warning] Using a password on the command line interface can be insecure.
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
[root@SYL4 ~]# 
  • 不显示输入的密码-p -e
[root@SYL4 ~]# mysql -uroot -p -e 'SHOW DATABASES';
Enter password: 
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
[root@SYL4 ~]# 
;