mysql基础
文章目录
1.关系型数据库介绍
1.1常见数据库:mysqlmariadb mssql oracle
(数据库管理系统)DBMS:database management system服务器
主机:
安装多个数据库服务
多个数据库
多个表
行,列
1.2数据模型主要有:
层次模型
网状结构
关系结构:二维关系(row,column)数据库管理系统(DBMS)
关系:Pelational,RDBMS
1.3事务:多个操作被当作一个整体对待就称为一个事务
一个关系型数据库是否支持事务,需要看其是否支持并满足ACID测试
ACID:是事务的一个基本标准
A:Automicity,原子性
C:Consistency,一致性
I:Isolation,隔离性
D:Durability,持久性
**SQL:**Structure Query Language,结构化查询语言
**约束:**constraint,向数据表提供的数据要遵守的限制
- 主键约束:一个或多个字段的组合,填入的数据必须能在本表中唯一标识本行。且必须提供数据,不能为空(NOT NULL)。
- 一个表只能存在一个
- 惟一键约束:一个或多个字段的组合,填入的数据必须能在本表中唯一标识本行。允许为空(NULL)
- 一个表可以存在多个
- 外键约束:一个表中的某字段可填入数据取决于另一个表的主键已有的数据
- 检查性约束
**索引:**将表中的一个或多个字段中的数据复制一份另存,并且这些数据需要按特定次序排序存储
1.4关系型数据库的常见组件有:
数据库:database
表:table由行(row)和列(column)组成
索引:index
视图:view
用户:user
权限:privilege
存储过程:procedure
存储函数:function
触发器:trigger
事件调度器:eventscheduler
1.5SQL语言的三种类型
- DDL:Data Defination Language,数据定义语言
- DML:Data Manipulation Language,数据操纵语言
- DCL:Data Control Language,数据控制语言
DDL:数据定义语言
创建:dcreate
删除:drop
修改:alter
DML:数据操纵语言(针对表)
向表中插入数据:insert
删除表中数据:delete
更新表中数据:update
查询表中数据:select
DCL:数据控制语言
授权:grant
移除授权:revoke
2. mysql安装与配置
2.1 mysql安装
mysql安装方式有三种:
- 源代码:编译安装
- 二进制格式的程序包:展开至特定路径,并经过简单配置后即可使用
- 程序包管理器管理的程序包:
-
rpm:有两种
- OS Vendor:操作系统发行商提供的
- 项目官方提供的
-
deb
-
//安装mysql的准备工作
wget http://dev.mysql.com/get/mysql57-community-release-el7-10.noarch.rpm //配置mysql的yum源
[root@lch ~]# rpm -ivh mysql57-community-release-el7-10.noarch.rpm
[root@lch ~]# yum clean all
[root@lch ~]# yum makecache
//安装mysql
[root@lch ~]# 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 \
http://repo.mysql.com/yum/mysql-5.7-community/el/7/x86_64/mysql-community-common-5.7.37-1.el7.x86_64.rpm \
http://repo.mysql.com/yum/mysql-5.7-community/el/7/x86_64/mysql-community-devel-5.7.37-1.el7.x86_64.rpm \
http://repo.mysql.com/yum/mysql-5.7-community/el/7/x86_64/mysql-community-libs-5.7.37-1.el7.x86_64.rpm \
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@lch ~]# rm -f mysql57-community-release-el7-10.noarch.rpm
[root@lch ~]# yum -y install *.rpm
//设置开机自启
[root@lch ~]# systemctl enable --now mysqld
[root@lch ~]# systemctl status mysqld
● mysqld.service - MySQL Server
Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disab>
Active: active (running) since Mon 2022-04-18 18:05:59 CST; 5s ago
Docs: man:mysqld(8)
http://dev.mysql.com/doc/refman/en/using-systemd.html
Process: 17285 ExecStart=/usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysql>
Process: 17236 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)
Main PID: 17288 (mysqld)
Tasks: 27 (limit: 11201)
Memory: 290.7M
CGroup: /system.slice/mysqld.service
└─17288 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid
Apr 18 18:05:54 lch systemd[1]: Starting MySQL Server...
Apr 18 18:05:59 lch systemd[1]: Started MySQL Server.
[root@lch ~]#
[root@lch ~]# ss -antl //查看端口号3306是mysql默认端口号
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 80 *:3306 *:*
LISTEN 0 128 [::]:22 [::]:*
//登录mysql
[root@lch ~]# grep "password" /var/log/mysqld.log //在日志文件中找出临时密码
2022-04-18T10:05:56.685157Z 1 [Note] A temporary password is generated for root@localhost: O.Ctpod)k6>o(临时密码)
[root@lch ~]# mysql -uroot -p'O.Ctpod)k6>o'
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 2
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 global validate_password_policy=0; //将设置密码的复杂性设为最低
Query OK, 0 rows affected (0.00 sec)
mysql> set global validate_password_length=1; //设置密码的长度最小为1
Query OK, 0 rows affected (0.00 sec)
mysql> set password = password('lch123!');
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> quit
//为避免mysql自动升级,这里需要卸载最开始安装的yum源
[root@lch ~]# rpm -e mysql57-community-release
//尝试用新密码登录
[root@lch ~]# mysql -uroot -p'lch123!'
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 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> //登录成功
//使用图形化工具连接数据库
//设置策略 让192.168.200.111这个ip可以连接到数据库
mysql> GRANT ALL ON *.* TO 'root'@'192.168.200.111' IDENTIFIED BY 'lch123!';
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> create database hui; //创建数据库,方便看效果
Query OK, 1 row affected (0.00 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| hui |
| mysql |
| performance_schema |
| sys |
+--------------------+
5 rows in set (0.00 sec)
[root@lch ~]# systemctl disable --now firewalld //关闭防火墙
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@lch ~]# setenforce 0 //临时关闭selinx
3. mysql的程序组成
- 客户端
- mysql:CLI交互式客户端程序
- mysql_secure_installation:安全初始化,强烈建议安装完以后执行此命令
- mysqldump:mysql备份工具
- mysqladmin
- 服务器端
- mysqld
3.1 mysql工具使用
-uUSERNAME //指定用户名,默认为root
-hHOST //指定服务器主机,默认为localhost,推荐使用ip地址
-pPASSWORD //指定用户的密码
-P# //指定数据库监听的端口,这里的#需用实际的端口号代替,如-P3307
-V //查看当前使用的mysql版本
-e //不登录mysql执行sql语句后退出,常用于脚本
[root@lch ~]# mysql -V
mysql Ver 14.14 Distrib 5.7.37, for Linux (x86_64) using EditLine wrapper
[root@lch ~]# mysql -uroot -p'lch123!' -e 'SHOW DATABASES;'
mysql: [Warning] Using a password on the command line interface can be insecure.
+--------------------+
| Database |
+--------------------+
| information_schema |
| hui |
| mysql |
| performance_schema |
| sys |
+--------------------+
[root@lch ~]# mysql -uroot -p -e 'SHOW DATABASES;'
Enter password:
+--------------------+
| Database |
+--------------------+
| information_schema |
| hui |
| mysql |
| performance_schema |
| sys |
+--------------------+