mysql的卸载与安装及重置root密码

目录

一、卸载mysql

二、yum方式安装(安装mysql5.7)

①教程:

②实际安装过程:

三、yum方式安装(安装mysql8.0)

四、rpm方式安装

五、MySQL相关配置修改

1.修改初始密码(若想改为弱密码)

说明:

2.设置远程连接(前提:关闭防火墙或开放3306端口)

3.my.cnf配置文件简介

 六、重置root密码


一、卸载mysql

参考文章:/var/log/mysqld.log 但是执行命令后发现没有要找的密码,是个空的文件_mysqld.log为空_fy_1852003327的博客-CSDN博客

[root@zabbix-proxy lib]# rpm -qf `which mysqld`      #/usr/sbin/mysqld属于哪个软件包
mysql-community-server-5.7.42-1.el7.x86_64
[root@zabbix-proxy lib]# rpm -qa |grep mysql         #检查是否存在与mysql相关的软件包
zabbix-server-mysql-5.0.34-1.el7.x86_64
mysql-community-libs-compat-5.7.42-1.el7.x86_64
mysql-community-server-5.7.42-1.el7.x86_64
zabbix-proxy-mysql-5.0.34-1.el7.x86_64
mysql57-community-release-el7-10.noarch
mysql-community-libs-5.7.42-1.el7.x86_64
mysql-community-client-5.7.42-1.el7.x86_64
php72w-mysql-7.2.34-1.w7.x86_64
mysql-community-common-5.7.42-1.el7.x86_64
[root@zabbix-proxy lib]# rpm -e --nodeps $(rpm -qa|grep mysql)    #删除与mysql相关软件包
warning: /etc/zabbix/zabbix_proxy.conf saved as /etc/zabbix/zabbix_proxy.conf.rpmsave
warning: /etc/zabbix/zabbix_server.conf saved as /etc/zabbix/zabbix_server.conf.rpmsave
[root@zabbix-proxy lib]# rpm -qa|grep mysql
[root@zabbix-proxy lib]# find / -name mysql               
/root/.cpan/build/DBD-mysql-4.050-yrHYt0/lib/DBD/mysql
/root/.cpan/build/DBD-mysql-4.050-rWErXc/lib/DBD/mysql
/root/.cpan/build/DBD-mysql-4.050-CF03yr/lib/DBD/mysql
/root/.cpan/build/DBD-mysql-4.050-OrRuhs/lib/DBD/mysql
/root/.cpan/build/DBD-mysql-4.050-Znb2nQ/lib/DBD/mysql
/var/lib/mysql
/var/lib/mysql/mysql
/usr/lib64/perl5/vendor_perl/auto/DBD/mysql
/usr/lib64/perl5/vendor_perl/DBD/mysql
/usr/share/mysql
[root@zabbix-proxy lib]# rm -rf $(find / -name mysql) && rm -rf `find / -user mysql`  #查找之前老版本mysql的目录、并且删除老版本mysql的文件和库
[root@zabbix-proxy lib]# find / -name mysql && find / -user mysql
[root@zabbix-proxy lib]#wget -i -c http://dev.mysql.com/get/mysql57-community-release-el7-10.noarch.rpm  #下载rpm包到当前目录
[root@zabbix-proxy ~]# rpm -ivh mysql57-community-release-el7-10.noarch.rpm   #安装本地rpm包,在/etc/yum.repos.d/目录增加了可供下载mysql的源仓库文件
Preparing...                          ################################# [100%]
Updating / installing...
   1:mysql57-community-release-el7-10 ################################# [100%]
[root@zabbix-proxy ~]# rpm --import https://repo.mysql.com/RPM-GPG-KEY-mysql-2022  #根据需要
[root@zabbix-proxy ~]# yum -y install mysql-community-server     #安装mysql软件包   
Loaded plugins: fastestmirror, priorities
Loading mirror speeds from cached hostfile
 * base: mirrors.aliyun.com
 * extras: mirrors.aliyun.com
 * updates: mirrors.aliyun.com
Resolving Dependencies
--> Running transaction check
---> Package mysql-community-server.x86_64 0:5.7.42-1.el7 will be installed
--> Processing Dependency: mysql-community-common(x86-64) = 5.7.42-1.el7 for package: mysql-community-server-5.7.42-1.el7.x86_64
--> Processing Dependency: mysql-community-client(x86-64) >= 5.7.9 for package: mysql-community-server-5.7.42-1.el7.x86_64
--> Running transaction check
---> Package mysql-community-client.x86_64 0:5.7.42-1.el7 will be installed
--> Processing Dependency: mysql-community-libs(x86-64) >= 5.7.9 for package: mysql-community-client-5.7.42-1.el7.x86_64
---> Package mysql-community-common.x86_64 0:5.7.42-1.el7 will be installed
--> Running transaction check
---> Package mysql-community-libs.x86_64 0:5.7.42-1.el7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

=============================================================================================================================================================================
 Package                                          Arch                             Version                                 Repository                                   Size
=============================================================================================================================================================================
Installing:
 mysql-community-server                           x86_64                           5.7.42-1.el7                            mysql57-community                           179 M
Installing for dependencies:
 mysql-community-client                           x86_64                           5.7.42-1.el7                            mysql57-community                            28 M
 mysql-community-common                           x86_64                           5.7.42-1.el7                            mysql57-community                           311 k
 mysql-community-libs                             x86_64                           5.7.42-1.el7                            mysql57-community                           2.6 M

Transaction Summary
=============================================================================================================================================================================
Install  1 Package (+3 Dependent packages)

Total download size: 210 M
Installed size: 895 M
Downloading packages:
(1/4): mysql-community-common-5.7.42-1.el7.x86_64.rpm                                                                                                 | 311 kB  00:00:01     
(2/4): mysql-community-libs-5.7.42-1.el7.x86_64.rpm                                                                                                   | 2.6 MB  00:00:04     
(3/4): mysql-community-client-5.7.42-1.el7.x86_64.rpm                                                                                                 |  28 MB  00:00:56     
(4/4): mysql-community-server-5.7.42-1.el7.x86_64.rpm                                                                                                 | 179 MB  00:03:04     
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Total                                                                                                                                        1.1 MB/s | 210 MB  00:03:09     
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Warning: RPMDB altered outside of yum.
** Found 4 pre-existing rpmdb problem(s), 'yum check' output follows:
perl-DBD-MySQL-4.023-6.el7.x86_64 has missing requires of libmysqlclient.so.18()(64bit)
perl-DBD-MySQL-4.023-6.el7.x86_64 has missing requires of libmysqlclient.so.18(libmysqlclient_18)(64bit)
2:postfix-2.10.1-9.el7.x86_64 has missing requires of libmysqlclient.so.18()(64bit)
2:postfix-2.10.1-9.el7.x86_64 has missing requires of libmysqlclient.so.18(libmysqlclient_18)(64bit)
  Installing : mysql-community-common-5.7.42-1.el7.x86_64                                                                                                                1/4 
  Installing : mysql-community-libs-5.7.42-1.el7.x86_64                                                                                                                  2/4 
  Installing : mysql-community-client-5.7.42-1.el7.x86_64                                                                                                                3/4 
  Installing : mysql-community-server-5.7.42-1.el7.x86_64                                                                                                                4/4 
  Verifying  : mysql-community-libs-5.7.42-1.el7.x86_64                                                                                                                  1/4 
  Verifying  : mysql-community-common-5.7.42-1.el7.x86_64                                                                                                                2/4 
  Verifying  : mysql-community-client-5.7.42-1.el7.x86_64                                                                                                                3/4 
  Verifying  : mysql-community-server-5.7.42-1.el7.x86_64                                                                                                                4/4 

Installed:
  mysql-community-server.x86_64 0:5.7.42-1.el7                                                                                                                               

Dependency Installed:
  mysql-community-client.x86_64 0:5.7.42-1.el7              mysql-community-common.x86_64 0:5.7.42-1.el7              mysql-community-libs.x86_64 0:5.7.42-1.el7             

Complete!
[root@zabbix-proxy ~]# systemctl status mysqld
● mysqld.service - MySQL Server
   Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
   Active: inactive (dead)
     Docs: man:mysqld(8)
           http://dev.mysql.com/doc/refman/en/using-systemd.html

May 08 06:47:32 zabbix-proxy systemd[1]: Starting MySQL Server...
May 08 06:47:40 zabbix-proxy systemd[1]: Started MySQL Server.
May 08 07:06:13 zabbix-proxy systemd[1]: Stopping MySQL Server...
May 08 07:06:15 zabbix-proxy systemd[1]: Stopped MySQL Server.
[root@zabbix-proxy ~]# systemctl start  mysqld
[root@zabbix-proxy ~]# systemctl status mysqld
● mysqld.service - MySQL Server
   Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
   Active: active (running) since Mon 2023-05-08 07:16:46 EDT; 1s ago
     Docs: man:mysqld(8)
           http://dev.mysql.com/doc/refman/en/using-systemd.html
  Process: 3532 ExecStart=/usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid $MYSQLD_OPTS (code=exited, status=0/SUCCESS)
  Process: 3480 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)
 Main PID: 3535 (mysqld)
   CGroup: /system.slice/mysqld.service
           └─3535 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid

May 08 07:16:43 zabbix-proxy systemd[1]: Starting MySQL Server...
May 08 07:16:46 zabbix-proxy systemd[1]: Started MySQL Server.
[root@zabbix-proxy ~]# grep "password" /var/log/mysqld.log
................................
2023-05-08T11:16:44.318958Z 1 [Note] A temporary password is generated for root@localhost: ?&Kv9=w)1zgk
[root@zabbix-proxy ~]# mysql -uroot -p?&Kv9=w)1zgk
-bash: syntax error near unexpected token `)'    #因为此密码内含")"所以需要手动输入密码
[root@zabbix-proxy ~]# mysql -uroot -p        #或者使用mysql -uroot -p"?&Kv9=w)1zgk"登录,密码需要加双引号
Enter password:               ##手动输入?&Kv9=w)1zgk       
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.42

Copyright (c) 2000, 2023, 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> \s
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.
mysql> alter user 'root'@'localhost' identified by '123456';
ERROR 1819 (HY000): Your password does not satisfy the current policy requirements
mysql>  set global validate_password_policy=0;
Query OK, 0 rows affected (0.00 sec)

mysql> set global validate_password_length=1;
Query OK, 0 rows affected (0.00 sec)

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

二、yum方式安装(安装mysql5.7)

①教程:

在CentOS中默认安装有MariaDB,这个是MySQL的分支,但为了需要,还是要在系统中安装MySQL,而且安装完成之后可以直接覆盖掉MariaDB。

1. 下载并安装MySQL官方的 Yum Repository

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

[root@localhost ~]# ls
[root@localhost ~]# 
[root@localhost ~]# pwd
/root
[root@localhost ~]# wget -i -c http://dev.mysql.com/get/mysql57-community-release-el7-10.noarch.rpm
--2022-09-15 23:49:53--  http://dev.mysql.com/get/mysql57-community-release-el7-10.noarch.rpm
Resolving dev.mysql.com (dev.mysql.com)... 23.2.135.207, 2600:140b:400:180::2e31, 2600:140b:400:19f::2e31
Connecting to dev.mysql.com (dev.mysql.com)|23.2.135.207|:80... connected.
HTTP request sent, awaiting response... 301 Moved Permanently
Location: https://dev.mysql.com/get/mysql57-community-release-el7-10.noarch.rpm [following]
--2022-09-15 23:49:54--  https://dev.mysql.com/get/mysql57-community-release-el7-10.noarch.rpm
Connecting to dev.mysql.com (dev.mysql.com)|23.2.135.207|:443... connected.
HTTP request sent, awaiting response... 302 Moved Temporarily
Location: https://repo.mysql.com//mysql57-community-release-el7-10.noarch.rpm [following]
--2022-09-15 23:49:54--  https://repo.mysql.com//mysql57-community-release-el7-10.noarch.rpm
Resolving repo.mysql.com (repo.mysql.com)... 104.73.0.223
Connecting to repo.mysql.com (repo.mysql.com)|104.73.0.223|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 25548 (25K) [application/x-redhat-package-manager]
Saving to: ‘mysql57-community-release-el7-10.noarch.rpm’

100%[================================================================================================================>] 25,548       126KB/s   in 0.2s   

2022-09-15 23:49:55 (126 KB/s) - ‘mysql57-community-release-el7-10.noarch.rpm’ saved [25548/25548]

-c: No such file or directory
No URLs found in -c.
FINISHED --2022-09-15 23:49:55--
Total wall clock time: 1.9s
Downloaded: 1 files, 25K in 0.2s (126 KB/s)
[root@localhost ~]# ls
mysql57-community-release-el7-10.noarch.rpm

使用上面的命令就直接下载了数据库安装用的Yum Repository的rpm包,大概25KB的样子。

[root@localhost ~]# yum install -y mysql57-community-release-el7-10.noarch.rpm
使用上面的命令安装数据库用的Yum Repository。

[root@localhost ~]# yum repolist       ##查看yum源的软件仓库
Loaded plugins: fastestmirror, priorities
Repodata is over 2 weeks old. Install yum-cron? Or run: yum makecache fast
Determining fastest mirrors
 * base: mirrors.aliyun.com
 * extras: mirrors.aliyun.com
 * updates: mirrors.aliyun.com
1 packages excluded due to repository priority protections
repo id                                                      repo name                                                                            status
!base/7/x86_64                                               CentOS-7 - Base - mirrors.aliyun.com                                                   10,072
!epel/x86_64                                                 Extra Packages for Enterprise Linux 7 - x86_64                                       13,756+1
!extras/7/x86_64                                             CentOS-7 - Extras - mirrors.aliyun.com                                                    512
!nginx-stable/7/x86_64                                       nginx stable repo                                                                         280
!updates/7/x86_64                                            CentOS-7 - Updates - mirrors.aliyun.com                                                 4,135
!webtatic/x86_64                                             Webtatic Repository EL7 - x86_64                                                          789
repolist: 29,544
[root@localhost etc]# cd yum.repos.d/
[root@localhost yum.repos.d]# ls     ##查看yum源的源文件
CentOS-Base.repo  epel.repo  nginx.repo  webtatic-archive.repo  webtatic.repo  webtatic-testing.repo
[root@localhost ~]# yum -y install mysql57-community-release-el7-10.noarch.rpm  #利用rpm包安装数据库的yum源与仓库
Loaded plugins: fastestmirror, priorities
Examining mysql57-community-release-el7-10.noarch.rpm: mysql57-community-release-el7-10.noarch
Marking mysql57-community-release-el7-10.noarch.rpm to be installed
Resolving Dependencies
--> Running transaction check
---> Package mysql57-community-release.noarch 0:el7-10 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

==========================================================================================================================================================
 Package                                   Arch                   Version                  Repository                                                Size
==========================================================================================================================================================
Installing:
 mysql57-community-release                 noarch                 el7-10                   /mysql57-community-release-el7-10.noarch                  30 k

Transaction Summary
==========================================================================================================================================================
Install  1 Package

Total size: 30 k
Installed size: 30 k
Downloading packages:
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : mysql57-community-release-el7-10.noarch                                                                                                1/1 
  Verifying  : mysql57-community-release-el7-10.noarch                                                                                                1/1 

Installed:
  mysql57-community-release.noarch 0:el7-10                                                                                                               

Complete!
[root@localhost ~]#
[root@localhost ~]# yum repolist
Loaded plugins: fastestmirror, priorities
Loading mirror speeds from cached hostfile
 * base: mirrors.aliyun.com
 * extras: mirrors.aliyun.com
 * updates: mirrors.aliyun.com
mysql-connectors-community                                                                                                         | 2.6 kB  00:00:00     
mysql-tools-community                                                                                                              | 2.6 kB  00:00:00     
mysql57-community                                                                                                                  | 2.6 kB  00:00:00     
(1/3): mysql-tools-community/x86_64/primary_db                                                                                     |  87 kB  00:00:00     
(2/3): mysql-connectors-community/x86_64/primary_db                                                                                |  90 kB  00:00:01     
(3/3): mysql57-community/x86_64/primary_db                                                                                         | 315 kB  00:00:01     
1 packages excluded due to repository priority protections
repo id                                                           repo name                                                                       status
base/7/x86_64                                                     CentOS-7 - Base - mirrors.aliyun.com                                              10,072
epel/x86_64                                                       Extra Packages for Enterprise Linux 7 - x86_64                                  13,755+1
extras/7/x86_64                                                   CentOS-7 - Extras - mirrors.aliyun.com                                               516
mysql-connectors-community/x86_64                                 MySQL Connectors Community                                                           199
mysql-tools-community/x86_64                                      MySQL Tools Community                                                                 92
mysql57-community/x86_64                                          MySQL 5.7 Community Server                                                           604
nginx-stable/7/x86_64                                             nginx stable repo                                                                    282
updates/7/x86_64                                                  CentOS-7 - Updates - mirrors.aliyun.com                                            4,156
webtatic/x86_64                                                   Webtatic Repository EL7 - x86_64                                                     789
repolist: 30,465
You have new mail in /var/spool/mail/root
[root@localhost ~]# ls /etc/yum.repos.d/
CentOS-Base.repo  epel.repo  mysql-community.repo  mysql-community-source.repo  nginx.repo  webtatic-archive.repo  webtatic.repo  webtatic-testing.repo

之后就开始安装MySQL服务器。

[root@localhost ~]# yum -y install mysql-community-server
这步可能会花些时间,安装完成后就会覆盖掉之前的mariadb。至此MySQL就安装完成了,然后是对MySQL的一些设置。

安装过程中可能遇到如下报错无法安装,原因是Mysql的GPG升级了,需要重新获取

执行以下命令再安装即可

rpm --import https://repo.mysql.com/RPM-GPG-KEY-mysql-2022

2 .MySQL数据库设置
首先启动MySQL

[root@localhost ~]# systemctl start  mysqld.service


查看MySQL运行状态:

[root@localhost ~]# systemctl status mysqld.service


有小圆球则表示此时MySQL已经开始正常运行,不过要想进入MySQL还得先找出此时root用户的密码,通过如下命令可以在日志文件中找出密码:

[root@localhost ~]# grep "password" /var/log/mysqld.log
1
A temporary password is generated for root@localhost: 这段后边的字段就是root的密码,直接mysql -uroot -p输入上边的密码即可进入MySQL。

TIPS:
1.进入系统后 ALTER USER 'root'@'localhost' IDENTIFIED BY '新密码';即可修改密码。
2.安装mysql提示 GBK验证失败
报错原文
Public key for mysql-community-server-5.7.37-1.el7.x86_64.rpm is not installed
Failing package is: mysql-community-server-5.7.37-1.el7.x86_64
GPG Keys are configured as: file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
Mysql安装失败
操作系统:CentOS 7.6
Mysql版本:mysql5.7
CentOS7.6 安装mysql5.7的时候报错,提示某一个包安装不成功。
失败原因
GPG对于包的源key的验证没有通过
解决办法
在yum install 版本后面加上 --nogpgcheck,即可绕过GPG验证成功安装。比如yum install mysql-community-server --nogpgcheck

②实际安装过程:

centos7.6通过yum方式安装mysql5.7
在CentOS中默认安装有MariaDB,这个是MySQL的分支,但为了需要,还是要在系统中安装MySQL,而且安装完成之后可以直接覆盖掉MariaDB。

1. 下载并安装MySQL官方的 Yum Repository

[root@localhost ~]# wget -i -c http://dev.mysql.com/get/mysql57-community-release-el7-10.noarch.rpm
使用上面的命令就直接下载了安装用的Yum Repository,大概25KB的样子,然后就可以直接yum安装了。

[root@localhost ~]# yum -y install mysql57-community-release-el7-10.noarch.rpm
之后就开始安装MySQL服务器。

[root@localhost ~]# yum -y install mysql-community-server

############################################################################
Mysql安装失败
操作系统:CentOS 7.6
Mysql版本:mysql5.7
CentOS7.6 安装mysql5.7的时候报错,提示某一个包安装不成功。
失败原因
GPG对于包的源key的验证没有通过导致提示安装mysql提示 GBK验证失败
报错原文
Public key for mysql-community-server-5.7.37-1.el7.x86_64.rpm is not installed
Failing package is: mysql-community-server-5.7.37-1.el7.x86_64
GPG Keys are configured as: file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
解决办法
在yum install 版本后面加上 --nogpgcheck,即可绕过GPG验证成功安装。比如
yum install mysql-community-server --nogpgcheck
############################################################################这步可能会花些时间,安装完成后就会覆盖掉之前的mariadb。至此MySQL就安装完成了,然后是对MySQL的一些设置。

2 .MySQL数据库设置
首先启动MySQL
[root@localhost ~]# systemctl start  mysqld.service

查看MySQL运行状态:
[root@localhost ~]# systemctl status mysqld.service

有小圆球则表示此时MySQL已经开始正常运行,不过要想进入MySQL还得先找出此时root用户的密码,通过如下命令可以在日志文件中找出密码:

[root@localhost ~]# grep "password" /var/log/mysqld.log
A temporary password is generated for root@localhost: 这段后边的字段就是root的密码,直接mysql -uroot -p输入上边的密码即可进入MySQL。
TIPS:
1.进入系统后 ALTER USER 'root'@'localhost' IDENTIFIED BY '新密码';即可修改密码。

############################################################################

mysql5.7初始化密码报错ERROR1820(HY000):
mysql初始化密码常见报错问题
1,mysql5.6是密码为空直接进⼊数据库的,但是mysql5.7就需要初始密码
cat /var/log/mysqld.log | grep password

2,然后执⾏ mysql -uroot -p ,输⼊上⾯的到的密码进⼊,⽤该密码登录后,必须马上修改新的密码,不然会报如下错误:
mysql> use mysql;
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.

3,如果你想要设置⼀个简单的测试密码的话,⽐如设置为123456,会提⽰这个错误,报错的意思就是你的密码不符合要求
mysql> alter user 'root'@'localhost' identified by '123456';
ERROR 1819 (HY000): Your password does not satisfy the current policy requirements
这个其实与validate_password_policy的值有关。
validate_password_policy有以下取值:
默认是1,即MEDIUM,所以刚开始设置的密码必须符合长度,且必须含有数字,⼩写或⼤写字母,特殊字符。
有时候,只是为了⾃⼰测试,不想密码设置得那么复杂,譬如说,我只想设置root的密码为123456。

必须修改两个全局参数:
⾸先,修改validate_password_policy参数的值
mysql> set global validate_password_policy=0;
Query OK, 0 rows affected (0.00 sec)
validate_password_length(密码长度)参数默认为8,我们修改为1
mysql> set global validate_password_length=1;
Query OK, 0 rows affected (0.00 sec)

4,完成之后再次执⾏修改密码语句即可成功
mysql> alter user 'root'@'localhost' identified by '123456';
Query OK, 0 rows affected (0.00 sec)
############################################################################

 centOS7安装MySQL教程

rpm安装方式地址:
原文链接:https://blog.csdn.net/qq_41437542/article/details/123369271

三、yum方式安装(安装mysql8.0)

1.由于centOS7中默认安装了MariaDB,需要先进行卸载

rpm -qa | grep -i mariadb
rpm -e --nodeps mariadb-libs-5.5.64-1.el7.x86_64

新增:经实践在使用yum方式安装MySQL时不用卸载也可以,会被自动替代    通过rpm方式安装需要卸载,否则会出现依赖问题

查询下本机mysql是否卸载干净

rpm -qa|grep mysql       ###若有残留也需要卸载

2.下载MySQL仓库并安装

wget https://repo.mysql.com//mysql80-community-release-el7-3.noarch.rpm

yum -y install mysql80-community-release-el7-3.noarch.rpm

2022年5月2日更新:

安装过程中可能遇到如下报错无法安装,原因是Mysql的GPG升级了,需要重新获取

执行以下命令再安装即可

rpm --import https://repo.mysql.com/RPM-GPG-KEY-mysql-2022

 3.默认安装MySQL8.0,如果需要安装MySQL5.7的话需要修改/etc/yum.repos.d/mysql-community.repo配置文件

将mysql80中enabled属性改为0,并添加图中红框内代码(安装MySQL8.0跳过该步骤)

4.安装MySQL数据库
yum -y install mysql-community-server
5.开启mysql服务
systemctl start mysqld.service
6.查看mysql默认密码并登陆
cat /var/log/mysqld.log | grep password

mysql -uroot -p 

四、rpm方式安装

首先去官网下载完整rpm包

下载mysql5.7的话点击这里跳转至下载页面

 解压tar包

 依次安装所需要的rpm包即可

rpm -ivh mysql-community-common-8.0.23-1.el7.x86_64.rpm
rpm -ivh mysql-community-libs-8.0.23-1.el7.x86_64.rpm --force --nodeps
rpm -ivh mysql-community-libs-compat-8.0.23-1.el7.x86_64.rpm
rpm -ivh mysql-community-client-plugins-8.0.23-1.el7.x86_64.rpm
rpm -ivh mysql-community-client-8.0.23-1.el7.x86_64.rpm
rpm -ivh mysql-community-server-8.0.23-1.el7.x86_64.rpm

安装完成后可以查看下已安装的组件

rpm -qa | grep mysql

 之后mysql的配置同上所述

相关软件包(根据需要安装即可)

mysql-community-client-5.7.18-1.el7.x86_64.rpm 客户端

mysql-community-devel-5.7.18-1.el7.x86_64.rpm 开发库

mysql-community-embedded-5.7.18-1.el7.x86_64.rpm 嵌入式

mysql-community-server-5.7.18-1.el7.x86_64.rpm 服务端

mysql-community-libs-5.7.18-1.el7.x86_64.rpm 共享库

mysql-community-test-5.7.18-1.el7.x86_64.rpm 测试组件

五、MySQL相关配置修改

1.修改初始密码(若想改为弱密码)

SHOW variables LIKE 'validate_password%';

注:有可能遇到如下图情况,此时先修改密码为强密码,便可以继续进行修改密码验证策略操作

将密码验证策略改为LOW,密码长度4位以上

1 set global validate_password.policy=0;    #有的MySQL版本为validate_password_policy,此处请以上一步查询到的字段名称为准
2 set global validate_password.length=4; #重启MySQL后失效

 此时再进行修改密码操作,可以修改为弱密码了

ALTER USER 'root'@'localhost' IDENTIFIED BY 'your password';

以后便可以使用你自己设置的密码登陆

说明:

mysql5.7初始化密码报错ERROR1820(HY000):

mysql初始化密码常见报错问题
1,mysql5.6是密码为空直接进⼊数据库的,但是mysql5.7就需要初始密码
cat /var/log/mysqld.log | grep password

2,然后执⾏ mysql -uroot -p ,输⼊上⾯的到的密码进⼊,⽤该密码登录后,必须马上修改新的密码,不然会报如下错误:
mysql> use mysql;
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.

3,如果你想要设置⼀个简单的测试密码的话,⽐如设置为123456,会提⽰这个错误,报错的意思就是你的密码不符合要求
mysql> alter user 'root'@'localhost' identified by '123456';
ERROR 1819 (HY000): Your password does not satisfy the current policy requirements

这个其实与validate_password_policy的值有关。
validate_password_policy有以下取值:
默认是1,即MEDIUM,所以刚开始设置的密码必须符合长度,且必须含有数字,⼩写或⼤写字母,特殊字符。
有时候,只是为了⾃⼰测试,不想密码设置得那么复杂,譬如说,我只想设置root的密码为123456。

必须修改两个全局参数:
⾸先,修改validate_password_policy参数的值
mysql> set global validate_password_policy=0;
Query OK, 0 rows affected (0.00 sec)

validate_password_length(密码长度)参数默认为8,我们修改为1
mysql> set global validate_password_length=1;
Query OK, 0 rows affected (0.00 sec)

4,完成之后再次执⾏修改密码语句即可成功
mysql> alter user 'root'@'localhost' identified by '123456';
Query OK, 0 rows affected (0.00 sec)
--------------------------------------------------------
作者:岱宗得原2021
链接:https://wenku.baidu.com/view/ee5d65e4d6bbfd0a79563c1ec5da50e2524dd192.html
来源:百度文库
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

2.设置远程连接(前提:关闭防火墙或开放3306端口)

在实际工作中,经常会用到诸如DBeaver等的数据库管理工具进行远程连接mysql数据库,需要设置允许远程连接。

在mysql数据库的user表中查看host,默认只允许localhost访问

 只需将localhost改为%允许任意地址访问即可

update user set host = '%' where user = 'root';
flush privileges;             # 刷新权限 权限更新后刷新才会起作用

 注:如果使用客户端连接提示了plugin caching_sha2_password错误,这是因为MySQL8.0的密码策略默认为caching_sha2_password(MySQL5.7无此问题)

update user set plugin = 'mysql_native_password' where user = 'root';
flush privileges;             # 刷新权限 权限更新后刷新才会起作用

 如果使用DBeaver建立MySQL 8+连接,无需修改默认密码策略,但可能会提示Public Key Retrieval is not allowed错误

 这是因为MySQL8.0的密码策略默认为caching_sha2_password认证,密码在传输过程中必须使用 SSL 协议保护,但是如果 RSA 公钥不可用,可以使用服务器提供的公钥;需要修改AllowPublicKeyRetrieval=True参数以允许客户端从服务器获取公钥,在Dbeaver的修改如下:

 修改完成后可以正确建立远程连接

3.my.cnf配置文件简介

前言:若使用rpm方式安装,可能无/etc/my.cnf配置文件,需将/usr/share/mysql目录下配置文件复制至/etc目录下

cp /usr/share/mysql/my-large.cnf /etc/my.cnf

[client]

port = 3306

socket = /var/run/mysql/mysql.sock

[mysqldump]

quick

max_allowed_packet = 16M

以上参数会被 MySQL 客户端应用读取,参数说明如下:

  • port:MySQL 客户端连接服务器端时使用的端口号,默认为 3306
  • socket:套接字文件所在目录
  • quick:支持较大的数据库转储,导出非常巨大的表时需要此项。
  • max_allowed_packet:服务所能处理的请求包的最大大小以及服务所能处理的最大的请求大小(当与大的BLOB字段一起工作时相当必要),每个连接独立的大小,大小动态增加。

[mysqld]

user = mysql

basedir = /usr/local/mysql

datadir = /mydata/mysql/data

port = 3306

server-id = 1

socket = /var/run/mysql/mysql.sock

上述参数说明如下:

  • user:mysqld 程序在启动后将在给定 UNIX/Linux 账户下执行。mysqld 必须从 root 账户启动才能在启动后切换到另一个账户下执行。mysqld_safe 脚本将默认使用 user=mysql 选项来启动 mysqld 程序。
  • basedir:指定 MySQL 安装的绝对路径;
  • datadir:指定 MySQL 数据存放的绝对路径;
  • port:服务端口号,默认为 3306
  • server-id:MySQL 服务的唯一编号,每个 MySQL 服务的 id 需唯一。
  • socket:socket 文件所在目录

character-set-server = utf8mb4

collation-server = utf8mb4_general_ci

init_connect = 'SET NAMES utf8mb4'

lower_case_table_names = 1

key_buffer_size = 16M

max_allowed_packet = 8M

no-auto-rehash

sql_mode=TRADITIONAL

skip-grant-tables

  • character-set-server:数据库默认字符集
  • collation-server:数据库字符集对应一些排序等规则,注意要和character-set-server对应
  • init_connect:设置client连接mysql时的字符集,防止乱码
  • lower_case_table_names:是否对sql语句大小写敏感,1表示不敏感(MySQL8.0+需要初始化之前设置)
  • key_buffer_size:用于指定索引缓冲区的大小
  • max_allowed_packet:设置一次消息传输的最大值
  • no-auto-rehash:仅仅允许使用键值的UPDATES和DELETES
  • sql_mode:表示 SQL 模式的参数,通过这个参数可以设置检验 SQL 语句的严格程度
  • skip-grant-tables:跳过密码验证 (平时不要开启,忘记root密码时再使用)

忘记root密码时:

重置root密码为空(开启skip-grant-tables)

UPDATE mysql.user SET authentication_string = '' WHERE user = 'root';

推荐一个不错的Mysql教程网站:http://c.biancheng.net/mysql/

数据库(Database)指长期存储在计算机内的、有组织的、可共享的数据集合。数据库实际上就是一个文件集合,是一个存储数据的仓库,本质就是一个文件系统,按照特定的格式把数据存储起来。

数据库管理系统(DBMS)是数据库系统的核心软件之一,是位于用户与操作系统之间的数据管理软件,用于建立,使用和维护数据库。它的主要功能包括数据定义、数据操作、数据库的运行管理、数据库的建立和维护等几个方面。

我们常说 XX 数据库,其实实质上是 XX 数据库管理系统。目前,较为流行的数据库管理系统有 MySQL、SQL Server、Oracle 和 DB2 等。

关系型数据库

关系型数据库是建立在关系模型基础上的数据库,借助于集合代数等数学概念和方法来处理数据库中的数据。简单说,关系型数据库是由多张能互相连接的表组成的数据库。

优点

  1. 都是使用表结构,格式一致,易于维护。
  2. 使用通用的 SQL 语言操作,使用方便,可用于复杂查询。
  3. 数据存储在磁盘中,安全。

缺点

  1. 读写性能比较差,不能满足海量数据的高效率读写。
  2. 不节省空间。因为建立在关系模型上,就要遵循某些规则,比如数据中某字段值即使为空仍要分配空间。
  3. 固定的表结构,灵活度较低。

常见的关系型数据库有 Oracle、DB2、PostgreSQL、Microsoft SQL Server、Microsoft Access和MySQL等。

非关系型数据库

非关系型数据库又被称为 NoSQL(Not Only SQL ),意为不仅仅是 SQL。通常指数据以对象的形式存储在数据库中,而对象之间的关系通过每个对象自身的属性来决定。

优点

  1. 非关系型数据库存储数据的格式可以是 key-value 形式、文档形式、图片形式等。使用灵活,应用场景广泛,而关系型数据库则只支持基础类型。
  2. 速度快,效率高。 NoSQL 可以使用硬盘或者随机存储器作为载体,而关系型数据库只能使用硬盘。
  3. 海量数据的维护和处理非常轻松。
  4. 非关系型数据库具有扩展简单、高并发、高稳定性、成本低廉的优势。
  5. 可以实现数据的分布式处理。

缺点

  1. 非关系型数据库暂时不提供 SQL 支持,学习和使用成本较高。
  2. 非关系数据库没有事务处理,没有保证数据的完整性和安全性。适合处理海量数据,但是不一定安全。
  3. 功能没有关系型数据库完善。

常见的非关系型数据库有 Neo4j、MongoDB、Redis、Memcached、MemcacheDB 和 HBase 等。

 六、重置root密码

下面记录如何重置忘记的root密码

环境与版本:
[root@zabbix-server etc]# cat /etc/redhat-release 
CentOS Linux release 7.9.2009 (Core)
[root@zabbix-server etc]# mysqld -V
mysqld  Ver 5.7.42 for Linux on x86_64 (MySQL Community Server (GPL))

找回密码步骤:
# systemctl stop mysqld
# sed -i.bak '$a\skip-grant-tables' /etc/my.cnf
# systemctl start mysqld
# mysql -uroot
mysql> use mysql
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> update user set authentication_string=password('123456') where user='root';
Query OK, 0 rows affected, 1 warning (0.00 sec)
Rows matched: 1  Changed: 0  Warnings: 1

mysql> \q
Bye
# sed -i '$d' /etc/my.cnf
# systemctl restart mysqld
# mysql -uroot -p123456
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 12
Server version: 5.7.42 MySQL Community Server (GPL)

Copyright (c) 2000, 2023, 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> 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值