数据库
首页 > 数据库> > mysql基础

mysql基础

作者:互联网

目录

mysql基础

数据库

关系型数据库

:存储在硬盘

nosql

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

sqlite

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

1关系型数据库介绍

1.1 数据结构模型

数据结构模型主要有:

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

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

1.2 RDBMS专业名词

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

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

索引:将表中的一个或多个字段中的数据复制一份另存,并且这些数据需要按特定次序排序存储

1.3 关系型数据库的常见组件

关系型数据库的常见组件有:

1.4 SQL语句

SQL语句有三种类型:

|SQL语句类型 |对应操作|
|--- |: -----|
|DDL |CREATE:创建; DROP:删除;ALTER:修改;|
|DML |INSERT:向表中插入数据;DELETE:删除表中数据;UPDATE:更新表中数据;SELECT:查询表中数据;|
|DCL |GRANT:授权;REVOKE:移除授权;|

2. mysql安装与配置

2.1 mysql安装

mysql安装方式有三种:

配置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)]> 

标签:mariadb,基础,mysqld,mysql,MariaDB,root,localhost
来源: https://www.cnblogs.com/loronoa/p/16514472.html