mysql基础

2022/7/25 2:22:54

本文主要是介绍mysql基础,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

目录
  • mysql基础
    • 数据库
      • 关系型数据库
      • nosql
      • sqlite
    • 1关系型数据库介绍
      • 1.1 数据结构模型
    • 1.2 RDBMS专业名词
    • 1.3 关系型数据库的常见组件
    • 1.4 SQL语句
  • 2. mysql安装与配置
    • 2.1 mysql安装
      • 2.2 mysql配置
    • 3.mariadb安装与配置
      • 3.1 mariadb安装

mysql基础

数据库

关系型数据库

:存储在硬盘

  • mysql:5.7版本前是开源软件
  • oracle: 商业软件
  • mariadb: 开源软件,由mysql原班人马开发
  • sqlserver 商业软件
  • MSSQL: MicroSoft 微软sql

nosql

:存储在内存,变量的形式key=value

  • redis
  • memcache
  • mongodb
  • pgsql

sqlite

:数据是存放在“文件(数据文件)”中

1关系型数据库介绍

1.1 数据结构模型

数据结构模型主要有:

  • 层次模型
  • 网状结构
  • 关系模型

关系模型:
二维关系:row,column

数据库管理系统:DBMS
关系:Relational,RDBMS

1.2 RDBMS专业名词

常见的关系型数据库管理系统:

  • 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.4 SQL语句

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:移除授权;|

2. mysql安装与配置

2.1 mysql安装

mysql安装方式有三种:

  • 源代码:编译安装
  • 二进制格式的程序包:展开至特定路径,并经过简单配置后即可使用
  • 程序包管理器管理的程序包:
  • rpm:有两种
    • OS Vendor:操作系统发行商提供的
    • 项目官方提供的
  • deb

配置mysql的yum源
1.下载mysql的yum repository

[root@localhost ~]# wget http://dev.mysql.com/get/mysql57-community-release-el7-11.noarch.rpm

2.更新下载好的yum repository

[root@localhost ~]#  rpm -Uvh mysql57-community-release-el7-11.noarch.rpm 

3.查看/etc/yum.repos.d/下多了两个mysql的repo

[root@localhost ~]# ls /etc/yum.repos.d/
myrepo.repo  mysql-community.repo  mysql-community-source.repo

4.开始安装mysql,安装前先禁用默认的mysql模块

[root@localhost ~]# yum module disable mysql
[root@localhost ~]# yum -y install mysql-community-server mysql-community-client mysql-community-common mysql-community-devel --nogpgcheck

2.2 mysql配置

启动mysql并设置开机自动启动

[root@localhost ~]# systemctl enable --now mysqld

查看3306端口已经监听

[root@localhost ~]# systemctl status mysqld
● mysqld.service - MySQL Server
   Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
   Active: active (running) since Sun 2022-07-24 13:57:18 CST; 3min 9s ago
     Docs: man:mysqld(8)
           http://dev.mysql.com/doc/refman/en/using-systemd.html
  Process: 2729 ExecStart=/usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid $MYSQLD_OPTS (code=exited, status=0/SUCCESS)
  Process: 2679 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)
 Main PID: 2731 (mysqld)
    Tasks: 27 (limit: 11201)
   Memory: 316.4M
   CGroup: /system.slice/mysqld.service
           └─2731 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid

Jul 24 13:57:14 localhost.localdomain systemd[1]: Starting MySQL Server...
Jul 24 13:57:18 localhost.localdomain systemd[1]: Started MySQL Server.

[root@localhost ~]# ss -antl
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                                 [::]:*   

在日志文件中找出临时密码

[root@localhost ~]# grep "password" /var/log/mysqld.log 
2022-07-24T05:57:15.560498Z 1 [Note] A temporary password is generated for root@localhost: g>u%=yugt6gW

使用获取到的临时密码登录mysql,密码为 g>u%=yugt6gW

[root@localhost ~]# mysql -uroot -p
Enter password: 
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
[root@localhost ~]# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.38

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登录密码

mysql> alter user 'root'@'localhost' identified by 'Luojialong123!';
Query OK, 0 rows affected (0.00 sec)

使用新密码登录mysql

[root@localhost ~]# mysql -uroot -pLuojialong123!
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.38 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> 

避免mysql自动升级,卸载最开始安装的yum源

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

3.mariadb安装与配置

3.1 mariadb安装

[root@localhost ~]# yum -y install mariadb*
[root@localhost ~]# systemctl enable --now mariadb
[root@localhost ~]# systemctl  status mariadb
● mariadb.service - MariaDB 10.3 database server
   Loaded: loaded (/usr/lib/systemd/system/mariadb.service; enabled; vendor preset: d>
   Active: active (running) since Sun 2022-07-24 02:21:37 EDT; 19s ago
     Docs: man:mysqld(8)
           https://mariadb.com/kb/en/library/systemd/
  Process: 7967 ExecStartPost=/usr/libexec/mysql-check-upgrade (code=exited, status=0>
  Process: 7831 ExecStartPre=/usr/libexec/mysql-prepare-db-dir mariadb.service (code=>
  Process: 7806 ExecStartPre=/usr/libexec/mysql-check-socket (code=exited, status=0/S>
 Main PID: 7936 (mysqld)
   Status: "Taking your SQL requests now..."
    Tasks: 30 (limit: 4615)
   Memory: 103.9M
   CGroup: /system.slice/mariadb.service
           └─7936 /usr/libexec/mysqld --basedir=/usr


设置密码

[root@localhost ~]# mysql
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 8
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)]> set password = password('123456');
Query OK, 0 rows affected (0.000 sec)

MariaDB [(none)]> \q
Bye

测试登录

[root@localhost ~]# mysql -uroot -p123456
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 9
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)]> 



这篇关于mysql基础的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程