MySql

MySQL基础

数据管理演变

  1. 人工档案管理
  2. 文件系统管理
  3. 数据库管理

数据库管理系统

  1. 层次数据库
  2. 网格型数据库
  3. 关系型数据库
  4. 对象型数据库
  5. 非关系型数据库
概述
数据库是一个长期存储在计算机内的、有组织的、有共享的、统一管理的数据集合.
在数据库中的数据我们可以用二维表格的形式理解,例如
表格中一列的标题我们称之为字段
表格中一行数据我们称之为一条记录
id account password nickname uuid
1 root root 超级管理员 2e6342dd-c475-410f-9948-e4cc1948ef0f
2 admin admin guanli e30e8060-9320-4b72-8722-10328348a272
3 phoenix 123456 李昊哲 3ddc2b0e-fdba-4f61-b0a8-2585ffdce940
数据库由3部分组成
数据库:用于存储数据
数据库管理系统:用户管理数据库的软件
数据库应用程序:为了提高数据库系统的管理能力使用的数据库管理的补充

在这里插入图片描述


对数据库进行查询、修改操作的语言我们称之为SQL
结构化查询语言(Structured Query Language)简称SQL(发音:/ˈes kjuː ˈel/ "S-Q-L"),是一种特殊目的的编程语言,是一种数据库查询和程序设计语言,用于存取数据以及查询、更新和管理关系数据库系统;同时也是数据库脚本文件的扩展名。
标准:SQL-92、SQL-99、SQL-2003方言
常用关系型数据库
Oracle、MySQL、SQLServer、...
对象型数据库
PostgreSQL、...
常用的非关系数据库(在中国)
Redis、MongoDB、HBase、...
按照SQL-92标准SQL包含4部分
1、数据库定义语言(DDL):create、drop、alter、...
2、数据库操作语言(DML):insert、update、delete、...
3、数据库查询语言(DQL):select
4、数据库控制语言(DCL):commit、rollback、grant、revoke、...

安装MySQL

华为欧拉openEuler在线安装MySQL8

升级系统

sudo yum -y update

安装依赖和常用工具

sudo yum -y install vim net-tools wget gcc make cmake lrzsz

将 MySQL Yum 存储库添加到系统的存储库列表中

sudo yum -y install https://repo.mysql.com//mysql80-community-release-el8-4.noarch.rpm

通过运行以下命令并检查其输出来验证是否已启用和禁用正确的子存储库

sudo yum repolist enabled | grep mysql

通过以下命令安装 MySQL

sudo yum -y install mysql-community-server

启动 MySQL 服务器

使用以下命令启动 MySQL 服务器:

sudo systemctl start mysqld

您可以使用以下命令检查 MySQL 服务器的状态:

sudo systemctl status mysqld

使用以下命令开启自启动 MySQL 服务器:

sudo systemctl enable mysqld

在服务器初始启动时,假设服务器的数据目录为空,会发生以下情况:

  • 服务器已初始化。

  • SSL 证书和密钥文件在数据目录中生成。

  • validate_password 已安装并启用。

  • 创建了一个超级用户帐户'root'@'localhost。超级用户的密码已设置并存储在错误日志文件中。要显示它,请使用以下命令:

    $> sudo grep 'temporary password' /var/log/mysqld.log
    

    通过使用生成的临时密码登录并为超级用户帐户设置自定义密码,尽快更改 root 密码:

    mysql -uroot -p
    

修改密码

ALTER USER 'root'@'localhost' IDENTIFIED BY '123456';
FLUSH PRIVILEGES;

笔记

validate_password 默认安装。实现的默认密码策略validate_password要求密码至少包含1个大写字母、1个小写字母、1个数字和1个特殊字符,密码总长度至少为8个字符。

validate_password检查语句中的明文密码。在要求密码长度至少为 8 个字符的默认密码策略下,密码很弱并且语句会产生错误:

ALTER USER 'root'@'localhost' IDENTIFIED BY 'Lihaozhe!!@@1122';

不检查指定为散列值的密码,因为原始密码值不可用于检查:

ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'Lihaozhe!!@@1122';
FLUSH PRIVILEGES;
update mysql.user set host = '%',plugin='mysql_native_password' where user='root';
FLUSH PRIVILEGES;
exit;
sudo systemctl restart mysqld
# 使用新密码连接
mysql -h 你自己的IP地址 -uroot -p

防火墙开放端口

–zone #作用域

–add-port=1935/tcp #添加端口,格式为:端口/通讯协议

–permanent #永久生效,没有此参数重启后失效

firewall-cmd --zone=public --add-port=3306/tcp --permanent

重启防火墙

firewall-cmd --reload 

Deepin UOS 离线安装MySQL8

升级系统

sudo apt update
sudo apt -y dist-upgrade 

安装依赖和常用工具

sudo apt -y install vim net-tools wget gcc make cmake lrzsz
sudo apt -y install libmecab2 libjson-perl 

下载离线安装包

wget https://cdn.mysql.com//Downloads/MySQL-8.0/mysql-server_8.0.29-1debian10_amd64.deb-bundle.tar

解压缩安装包

mkdir mysql & tar -xvf mysql-server_8.0.29-1debian10_amd64.deb-bundle.tar -C mysql cd mysql

安装MySQL8

cd mysql 
sudo dpkg -i mysql-{
   common,community-client,community-client-core,community-client-plugins,client,community-server,community-server-core,server}_*.deb

修改密码和可访问主机地址

select host , user , plugin ,authentication_string from mysql.user;

ALTER USER 'root'@'localhost' IDENTIFIED BY '123456';
flush privileges;

ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '123456';
flush privileges;

update mysql.user set host = '%',plugin='mysql_native_password' where user='root';
flush privileges;

quit;

sudo systemctl restart mysql

新建用户和授权

-- 创建密码为 Lihaozhe!!@@6633 的用户 lhz 可以通过 本机访问 
create user 'lhz'@'localhost' identified by 'Lihaozhe!!@@6633';
-- 授权 lhz 用户 可以通过本机 访问 所有数据中所有表 
grant all privileges on *.* to 'lhz'@'localhost' with grant option;
-- 刷新权限
flush privileges;
-- 创建密码为 Lihaozhe!!@@6633 的用户 lhz 可以通过 任意地址访问 密码策略为 mysql_native_password
create user 'lhz'@'%' identified with mysql_native_password by 'Lihaozhe!!@@6633';
-- 授权 lhz 用户 可以通过本机 访问 所有数据中所有表 
grant all privileges on *.* to 'lhz'@'%' with grant option;
-- 刷新权限
flush privileges;
-- 如果遇到 ERROR 1227 (42000): Access denied; you need (at least one of) the SYSTEM_USER privilege(s) for this operation
-- 原因是由于root用户没有SYSTEM_USER权限,把权限加入后即可解决:
grant system_user on *.* to 'root';

MySQL忘记密码

1、编辑MySQL配置文件

# openEuler
sudo vim /etc/my.cnf

# Deepin UOS 
sudo vim /etc/mysql/mysql.conf.d/mysqld.cnf

2、在配置文件中的[mysqld]下加入skip-grant-tables

openEuler配置如下:

# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/8.0/en/server-configuration-defaults.html

[mysqld]
#
# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M
#
# Remove the leading "# " to disable binary logging
# Binary logging captures changes between backups and is enabled by
# default. It's default setting is log_bin=binlog
# disable_log_bin
#
# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M
#
# Remove leading # to revert to previous value for default_authentication_plugin,
# this will increase compatibility with older clients. For background, see:
# https://dev.mysql.com/doc/refman/8.0/en/server-system-variables.html#sysvar_default_authentication_plugin
# default-authentication-plugin=mysql_native_password

datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock

log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid

skip-grant-tables

Deepin UOS 配置如下:

# Copyright (c) 2014, 2021, Oracle and/or its affiliates.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License, version 2.0,
# as published by the Free Software Foundation.
#
# This program is also distributed with certain software (including
# but not limited to OpenSSL) that is licensed under separate terms,
# as designated in a particular file or component or in included license
# documentation.  The authors of MySQL hereby grant you an additional
# permission to link the program and your derivative works with the
# separately licensed software that they have included with MySQL.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License, version 2.0, for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA

#
# The MySQL  Server configuration file.
#
# For explanations see
# http://dev.mysql.com/doc/mysql/en/server-system-variables.html

[mysqld]
pid-file	= /var/run/mysqld/mysqld.pid
socket		= /var/run/mysqld/mysqld.sock
datadir		= /var/lib/mysql
log-error	= /var/log/mysql/error.log

skip-grant-tables

3、重启MySQL服务

# openEuler
sudo systemctl restart mysqld

# Deepin UOS
sudo systemctl restart mysql

4、进入MySQL

sudo mysql

5、刷新权限

FLUSH PRIVILEGES;

6、修改密码

select user,host from mysql.user where user = 'root';

-- 如果查询结果host的值为 localhost 执行
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'Lihaozhe!!@@6633';

-- 如果查询结果host的值为 % 执行
ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY 'Lihaozhe!!@@6633';

-- 如果遇到 ERROR 1290 (HY000): The MySQL server is running with the --skip-grant-tables option so it cannot execute this statement
-- 先执行 FLUSH PRIVILEGES; 再修改密码

-- 如果遇到 ERROR 1227 (42000): Access denied; you need (at least one of) the SYSTEM_USER privilege(s) for this operation
-- 原因是由于root用户没有SYSTEM_USER权限,把权限加入后即可解决:
grant system_user on *.* to 'root';

7、刷新权限

FLUSH PRIVILEGES;

8、退出MySQL

exit;

9、编辑MySQL配置文件

sudo vim /etc/my.cnf

10、在配置文件中注释 skip-grant-tables

# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/8.0/en/server-configuration-defaults.html

[mysqld]
#
# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M
#
# Remove the leading "# " to disable binary logging
# Binary logging captures changes between backups and is enabled by
# default. It's default setting is log_bin=binlog
# disable_log_bin
#
# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M
#
# Remove leading # to revert to previous value for default_authentication_plugin,
# this will increase compatibility with older clients. For background, see:
# https://dev.mysql.com/doc/refman/8.0/en/server-system-variables.html#sysvar_default_authentication_plugin
# default-authentication-plugin=mysql_native_password

datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock

log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid

# skip-grant-tables

11、重启MySQL服务

sudo systemctl restart mysqld

12、使用新密码访问数据库

sudo mysql -uroot -p

数据库常用操作

登录数据库

mysql -uroot -p

提示信息如下:

Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.29 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.





**deppin mysql** 

sudo apt-get -y install libmecab2 libjson-perl 
sudo dpkg -i mysql-{common,community-client,community-client-core,community-client-plugins,client,community-server,community-server-core,server}_*.deb

mysql -h localhost -uroot -p

use mysql;

select host , user , plugin ,authentication_string from user;

ALTER USER 'root'@'localhost' IDENTIFIED BY '123456';
flush privileges;

ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '123456';
flush privileges;

update mysql.user set host = '%',plugin='mysql_native_password' where user='root';
flush privileges;

quit;

sudo systemctl restart mysql

查看所有数据库

show databases;

提示信息如下:

+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.00 sec)

创建数据库

create database `day01`;

提示信息如下:

Query OK, 1 row affected (0.00 sec)
show databases;
+--------------------+
| Database           |
+--------------------+
| day01              |
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.01 sec)

查看数据库创建详细过程

show create database `day01`;

提示信息如下:
在这里插入图片描述

或者:

show create database `day01`\G

提示信息如下:
在这里插入图片描述

创建数据库同时指定字符集

CREATE DATABASE `day011` DEFAULT CHARACTER SET gbk;
SHOW CREATE DATABASE `day011`;

提示信息如下:
在这里插入图片描述

删除数据库

drop database `day011`;

提示信息如下:

Query OK, 0 rows affected (0.00 sec)

查看当前使用的数据库

select database();

提示信息如下:

+------------+
| database() |
+------------+
| NULL       |
+------------+
1 row in set (0.00 sec)

切换数据库

use `day01`;

提示信息如下:

Database changed
select database();
+------------+
| database() |
+------------+
| day01      |
+------------+
1 row in set (0.00 sec)

数据表操作

创建数据表

use `day01`;
create table `user` (
    `id` int(3) comment '用户编号',
    `account` varchar(11) comment '用户账号',
    `password` varchar(64) comment '用户密码',
    `nickname` varchar(50) comment '用户姓名'
);

提示信息如下:

Query OK, 0 rows affected, 1 warning (0.01 sec)

查看当前数据库中的所有表

show tables;

提示信息如下:

+-----------------+
| Tables_in_day01 |
+-----------------+
| user            |
+-----------------+
1 row in set (0.00 sec)

查看数据标表创建详细过程

show create table `user`;

在这里插入图片描述

show create table `user`\G

在这里插入图片描述

查询MySQL数据库支持的数据库引擎

show engines;

在这里插入图片描述

删除数据表

drop table `user`;

提示信息如下:

Query OK, 0 rows affected (0.01 sec)

创建指定引擎和字符集的数据表

create table `user` (
    `id` int(3) comment '用户编号',
    `account` varchar(11) comment '用户账号',
    `password` varchar(64) comment '用户密码',
    `nickname` varchar(50) comment '用户姓名'
) engine=myisam default charset=gbk;

查看数据表结构

describe `user`;

提示信息如下:

+----------+-------------+------+-----+---------+-------+
| Field    | Type        | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| id       | int         | YES  |     | NULL    |       |
| account  | varchar(11) | YES  |     | NULL    |       |
| password | varchar(64) | YES  |     | NULL    |       |
| nickname | varchar(50) | YES  |     | NULL    |       |
+----------+-------------+------+-----+---------+-------+
4 rows in set (0.01 sec)

或者

describe `user`\G
*************************** 1. row ***************************
  Field: id
   Type: int
   Null: YES
    Key: 
Default: NULL
  Extra: 
*************************** 2. row ***************************
  Field: account
   Type: varchar(11)
   Null: YES
    Key: 
Default: NULL
  Extra: 
**

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值