mysql基础
文章目录
一、关系型数据库介绍
1.1数据结构模型
数据结构模型主要有:
- 层次模型
- 网状结构
- 关系结构
关系模型:
二层关系:row column
数据结构管理系统:DBMS
关系:Relational RD
BMS
1.2RDBMS专业名词
常见的关系型数据库管理系统:
-
MySQL:MySQL,MariaDB,Percona-Server
-
PostgreSQL:简称为pgsql
-
Oracle
-
MSSQL
SQL: Structure Query Language,结构化查询语言
约束: constraint,向数据表提供的数据要遵守的限制 -
主键约束:一个或多个字段的组合,填入的数据必须能在本表中唯一标识本行。且必须提供数据,不能为空(NOT NULL)。
-
惟一键约束:一个或多个字段的组合,填入的数据必须能在本表中唯一标识本行。允许为空(NULL)
-
外键约束:一个表中的某字段可填入数据取决于另一个表的主键已有的数据
-
检查性约束
索引: 将表中的一个或多个字段中的数据复制一份另存,并且这些数据需要按特定次序排序存储
1.3关系型数据库的常见组件
关系型数据库的常见组件有: -
数据库:database
-
表:table,由行(row)和列(column)组成
-
索引:index
-
视图:view
-
用户:user
-
权限:privilege
-
存储过程:procedure
-
存储函数:function
-
触发器:trigger
-
事件调度器:event scheduler
1.4SQL语句
SQL语句有三种类型: -
DDL:Data Defination Language,数据定义语言
-
DML:Data Manipulation Language,数据操纵语言
-
DCL:Data Control Language,数据控制语言
SQL语句类型 | 对应操作 |
---|---|
DDL | CREATE:创建DROP:删除ALTER:修改 |
-DML- | -INSERT:向表中插入数据DELETE:删除表中数据UPDATE:更新表中数据SELECT:查询表中数据- |
DCL | GRANT:授权 REVOKE:移除授权 |
二、mysql安装与配置
2.1mysql安装
- 源代码:编译安装
- 二进制格式的程序包:展开至特定路径,并经过简单配置后即可使用
- 程序包管理器管理的程序包
1.rpm:有两种
OS Vendor:操作系统发行商提供的
项目官方提供的
2.deb
准备工作
wget -O /usr/src/mysql57-community-release-el7-10.noarch.rpm \
http://dev.mysql.com/get/mysql57-community-release-el7-10.noarch.rpm
rpm -Uvh /usr/src/mysql57-community-release-el7-10.noarch.rpm
#安装mysql5.7
yum -y install mysql-community-server mysql-community-client \
mysql-community-common mysql-community-devel
[root@bogon ~]# wget http://repo.mysql.com/yum/mysql-5.7-community/el/7/x86_64/mysql-community-client-5.7.37-1.el7.x86_64.rpm
[root@bogon ~]# wget http://repo.mysql.com/yum/mysql-5.7-community/el/7/x86_64/mysql-community-common-5.7.37-1.el7.x86_64.rpm
[root@bogon ~]# wget http://repo.mysql.com/yum/mysql-5.7-community/el/7/x86_64/mysql-community-libs-5.7.37-1.el7.x86_64.rpm
[root@bogon ~]# wget http://repo.mysql.com/yum/mysql-5.7-community/el/7/x86_64/mysql-community-server-5.7.37-1.el7.x86_64.rpm
[root@bogon ~]# ls
anaconda-ks.cfg mysql-community-client-5.7.37-1.el7.x86_64.rpm mysql-community-devel-5.7.37-1.e
mysql57-community-release-el7-10.noarch.rpm mysql-community-common-5.7.37-1.el7.x86_64.rpm mysql-community-libs-5.7.37-1.e
[root@bogon ~]# dnf -y install *.rpm
2.2mysql配置
[root@bogon ~]# systemctl enable --now mysqld //设置开机自启
[root@bogon ~]# systemctl status mysqld.service //查看确保开启
● mysqld.service - MySQL Server
Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
Active: active (running) since Mon 2022-04-18 20:56:21 CST; 10s ago
Docs: man:mysqld(8)
http://dev.mysql.com/doc/refman/en/using-systemd.html
Process: 13651 ExecStart=/usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid $MYSQLD_OPTS (co>
Process: 13602 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)
Main PID: 13654 (mysqld)
Tasks: 27 (limit: 23790)
Memory: 302.1M
CGroup: /system.slice/mysqld.service
└─13654 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid
4月 18 20:56:17 bogon systemd[1]: Starting MySQL Server...
4月 18 20:56:21 bogon systemd[1]: Started MySQL Server.
[root@bogon ~]# ss -antl //查看端口3306
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 80 *:3306 *:*
LISTEN 0 128 [::]:22 [::]:*
登录mysql
[root@bogon ~]# grep "password" /var/log/mysqld.log //查看临时密码
2022-04-18T12:56:18.847517Z 1 [Note] A temporary password is generated for root@localhost: %<7A)1eaA9w?
[root@bogon ~]# mysql -uroot -p'%<7A)1eaA9w?' //利用临时密码登录mysql 注意中间无空格
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 5
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> //成功登录
mysql> set password = password('RunTime123!');
Query OK, 0 rows affected, 1 warning (0.00 sec)
修改策略
mysql> GRANT ALL ON *.* TO 'root'@'192.168.146.1' IDENTIFIED BY 'RunTime123!';
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
使用图形化工具连接数据库
mysql> create database aaa;
Query OK, 1 row affected (0.00 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| aaa |
| mysql |
| performance_schema |
| sys |
+--------------------+
5 rows in set (0.00 sec)
关闭防火墙
[root@bogon ~]# vi /etc/selinux/config
[root@bogon ~]# systemctl disable --now firewalld
[root@bogon ~]# setenforce 0
[root@bogon ~]# vi /etc/selinux/config
成功登录界面