跟高手学习LINUX笔记-29 --MYSQL5.7安装部署 第八章 MYSQL5.7安装部署 本节所讲内容: 8.1、数据库简介及本笔记包含内容概要 8.2、MYSQL5.7安装部署三种方法及自动源码安装脚本 8.3、实战--MYSQL破解密码
实验环境: 服务器1:node1 IP:192.168.56.11 作用:yum安装数据库5.7.31 服务器2:node2 IP:192.168.56.12 作用:预编译安装数据库5.7.31 服务器3:node3 IP:192.168.56.13 作用:源码安装数据库5.7.31 服务器4:node4 IP:192.168.56.14 作用:yum安装数据库5.5后破解密码
正文部分: 8.1、数据库简介及本笔记包含内容概要 8.1.1、数据库简介 1、数据库管理三个历史阶段 1)人工管理阶段 2)文件夹式管理阶段 3)数据库管理阶段 历史仅仅是历史,只是一个经历的过程 2、数据库的构成 1)DBMS:数据库管理系统,也就是管理数据库的各个软件 A:用二维表作为存储格式的数据库管理系统:ORACLE/SQL SERVER/MYSQL/DB2等 B:用key-values作为存储格式的数据库管理系统:REDIS/,MONOGDB/MEMCACHE等 2)DBA:管理数据库的专业人员 3、SQL语言分类--结构化查询语言,也是数据库最常用语言 1)DDL:数据库定义语言--数据库中库、表、索引等创建、删除等操作:CREATE DROP ALTER 2)DML:数据库操作语言--数据库中对数据的增(insert)、删(delete)、改(update)操作 3)DQL:数据库查询语言--数据库对数据各种查询的操作(select) 4)DCL:数据库控制语言--数据库权限等管理(grant/revoke) 4、本笔记包含内容概要 1)安装与部署 2)备份恢复 3)主从复制 4)分布式数据库(Galera Cluster for MySQL ) 5)读写分离 6)HA架构(MHA) 7)性能优化 8)Redis群集实现 9)MONOGDB群集实现 个人心得:数据库学习不易,且练且提高
8.2、MYSQL5.7安装部署三种方法 8.2.1:Yum二进制安装数据库5.7.31并初始化 需要文件:mysql57-community-release-el7-10.noarch.rpm 1、查询本机是否有mariadb或mysql,如果有卸载 [root@centos7-node1 ~]# ip addr |grep inet inet 192.168.56.11/24 brd 192.168.56.255 scope global ens32 [root@centos7-node1 ~]# rpm -qa |grep mysql [root@centos7-node1 ~]# rpm -qa |grep mariadb mariadb-libs-5.5.60-1.el7_5.x86_64 [root@centos7-node1 ~]# yum -y remove mariadb-libs-5.5.60-1.el7_5.x86_64 2、下载相关rpm文件,名称为:mysql57-community-release-el7-10.noarch.rpm [root@centos7-node1 ~]# wget http://dev.mysql.com/get/mysql57-community-release-el7-10.noarch.rpm [root@centos7-node1 ~]# ll total 40 -rw-------. 1 root root 1583 Jun 11 17:29 anaconda-ks.cfg -rw-r--r-- 1 root root 25548 Apr 7 2017 mysql57-community-release-el7-10.noarch.rpm [root@centos7-node1 ~]# rpm -ivh mysql57-community-release-el7-10.noarch.rpm [root@centos7-node1 ~]# ll /etc/yum.repos.d/ total 24 -rw-r--r--. 1 root root 2523 Jun 11 17:55 CentOS-Base.repo -rw-r--r--. 1 root root 951 Oct 3 2017 epel.repo -rw-r--r--. 1 root root 1050 Oct 3 2017 epel-testing.repo -rw-r--r-- 1 root root 1627 Apr 5 2017 mysql-community.repo -rw-r--r-- 1 root root 1663 Apr 5 2017 mysql-community-source.repo
[root@centos7-node1 ~]# yum clean all && yum makecache fast
[root@centos7-node1 ~]# yum repolist all |grep mysql mysql-cluster-7.5-community/x86_64 MySQL Cluster 7.5 Community disabled mysql-cluster-7.5-community-source MySQL Cluster 7.5 Community - disabled mysql-connectors-community/x86_64 MySQL Connectors Community enabled: 165 mysql-connectors-community-source MySQL Connectors Community - disabled mysql-tools-community/x86_64 MySQL Tools Community enabled: 115 mysql-tools-community-source MySQL Tools Community - Sourc disabled mysql-tools-preview/x86_64 MySQL Tools Preview disabled mysql-tools-preview-source MySQL Tools Preview - Source disabled mysql55-community/x86_64 MySQL 5.5 Community Server disabled mysql55-community-source MySQL 5.5 Community Server - disabled mysql56-community/x86_64 MySQL 5.6 Community Server disabled mysql56-community-source MySQL 5.6 Community Server - disabled mysql57-community/x86_64 MySQL 5.7 Community Server enabled: 444 mysql57-community-source MySQL 5.7 Community Server - disabled mysql80-community/x86_64 MySQL 8.0 Community Server disabled mysql80-community-source MySQL 8.0 Community Server - disabled 最后一列为enabled在yum 安装的时候才能用,如果想调整可以参考下面的步骤操作 扩展小知识点: 假如我想安装mysql5.6:yum-config-manager引入处理 如果没有的话,可以安装下: [root@centos7-node1 ~]# yum -y install yum-utils-1.1.31-54.el7_8.noarch
[root@centos7-node1 ~]# yum-config-manager --enable mysql56-community [root@centos7-node1 ~]# yum repolist all |grep mysql mysql-cluster-7.5-community/x86_64 MySQL Cluster 7.5 Community disabled mysql-cluster-7.5-community-source MySQL Cluster 7.5 Community - disabled mysql-connectors-community/x86_64 MySQL Connectors Community enabled: 165 mysql-connectors-community-source MySQL Connectors Community - disabled mysql-tools-community/x86_64 MySQL Tools Community enabled: 115 mysql-tools-community-source MySQL Tools Community - Sourc disabled mysql-tools-preview/x86_64 MySQL Tools Preview disabled mysql-tools-preview-source MySQL Tools Preview - Source disabled mysql55-community/x86_64 MySQL 5.5 Community Server disabled mysql55-community-source MySQL 5.5 Community Server - disabled mysql56-community/x86_64 MySQL 5.6 Community Server enabled: 547 mysql56-community-source MySQL 5.6 Community Server - disabled mysql57-community/x86_64 MySQL 5.7 Community Server enabled: 444 mysql57-community-source MySQL 5.7 Community Server - disabled mysql80-community/x86_64 MySQL 8.0 Community Server disabled mysql80-community-source MySQL 8.0 Community Server - disabled 由此可以看到mysql56-community第三列已经从disabled-->enabled 如果想取消则用: [root@centos7-node1 ~]# yum-config-manager --disable mysql56-community [root@centos7-node1 ~]# yum -y install mysql-community-server.x86_64 [root@centos7-node1 ~]#systemctl start mysqld && systemctl enable mysqld [root@centos7-node1 ~]# grep 'password' /var/log/mysqld.log 2020-07-16T06:48:59.154140Z 1 [Note] A temporary password is generated for root@localhost: 565VWlcoqY>3 说明:565VWlcoqY>3 这个是数据库启动临时使用的密码 [root@centos7-node1 ~]# mysql -uroot -p'565VWlcoqY>3' -A 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 7 Server version: 5.7.31 然后用这个临时生成的密码就登录成功了,看一下有哪些数据库吧 mysql> show databases; ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement. 结果有这样的错误提示,说明必须要重新修改一下密码哟,于是乎做以下操作: mysql> alter user 'root'@'localhost' identified by '2wsx!QAZ'; mysql> flush privileges; mysql>exit; 退出后用新密码重新登录 mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | sys | +--------------------+ 4 rows in set (0.00 sec) 小知识点: [root@centos7-node1 ~]# mysql_secure_installation 除了修改密码为:no 外其他一路Y下去 (1)Remove anonymous users:删除临时用户 (2)Disallow root login remotely:不许可root远程设置 (3)Remove test database and access to it:删除临时表 (4)Reload privilege tables now:重启加载相关权限 [root@centos7-node1 ~]# netstat -tunlp |grep 3306 tcp6 0 0 :::3306 :::* LISTEN 2908/mysqld
8.2.1:二进制预编译安装数据库5.7.31并初始化--常用
[root@centos7-node2 ~]# ip addr |grep inet
inet 192.168.56.12/24 brd 192.168.56.255 scope global ens32
[root@centos7-node2 ~]# yum -y install libaio gcc gcc-c++
[root@centos7-node2 ~]# groupadd mysql
[root@centos7-node2 ~]# useradd -r -g mysql -s /sbin/nologin mysql
[root@centos7-node2 ~]# ll
total 677464
-rw-------. 1 root root 1583 Jun 11 17:29 anaconda-ks.cfg
-rw-r--r-- 1 root root 693708288 Jul 16 12:48 mysql-5.7.31-linux-glibc2.12-x86_64.tar
[root@centos7-node2 ~]# tar -zxvf mysql-5.7.31-linux-glibc2.12-x86_64.tar.gz
[root@centos7-node2 ~]# mv mysql-5.7.31-linux-glibc2.12-x86_64 /usr/local/
[root@centos7-node2 ~]# cd /usr/local/
[root@centos7-node2 local]# ln -s mysql-5.7.31-linux-glibc2.12-x86_64 mysql
以下进行初始化操作
[root@centos7-node2 local]# cd mysql
[root@centos7-node2 mysql]# pwd
/usr/local/mysql
[root@centos7-node2 mysql]#mkdir mysql-files
[root@centos7-node2 mysql]#chmod 750 mysql-files/
[root@centos7-node2 mysql]#chown -R mysql.mysql .
[root@centos7-node2 mysql]#/usr/local/mysql/bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data
2020-07-16T07:51:58.708657Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
……
2020-07-16T07:52:01.905219Z 1 [Note] A temporary password is generated for root@localhost: a1iVw,PhfZLA
说明:
A temporary password is generated for root@localhost: a1iVw,PhfZLA
初始化后,生成这个初始化密码:a1iVw,PhfZLA,第一次登录用到
[root@centos7-node2 mysql]#/usr/local/mysql/bin/mysql_ssl_rsa_setup --datadir=/usr/local/mysql/data
[root@centos7-node2 mysql]# chown -R root .
[root@centos7-node2 mysql]# chown -R mysql data mysql-files
修改/etc/my.cnf内容如下:
[root@centos7-node2 mysql]# cat /etc/my.cnf
[mysqld]
datadir=/usr/local/mysql/data
basedir=/usr/local/mysql
加入服务中,以便开机自启动
[root@centos7-node2 mysql]# cp support-files/mysql.server /etc/init.d/mysqld
[root@centos7-node2 mysql]# chkconfig --add mysqld
[root@centos7-node2 mysql]# chkconfig mysqld on
[root@centos7-node2 mysql]# service mysqld start
Starting MySQL.Logging to '/usr/local/mysql/data/centos7-node2.err'.
SUCCESS!
[root@centos7-node2 mysql]# netstat -tunlp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:2202 0.0.0.0:* LISTEN 4981/sshd
tcp6 0 0 :::3306 :::* LISTEN 5803/mysqld
如果不进行以下两步操作,直接执行mysql命令会出现“command not found”的错误提示
[root@centos7-node2 mysql]# echo 'export PATH=$PATH:/usr/local/mysql/bin' >> /etc/profile
[root@centos7-node2 mysql]# source /etc/profile
也可以通过systemctl管理:
[root@centos7-node2 ~]# cat /etc/systemd/system/mysqld.service
[Unit]
Description=MySQL Server
Documentation=man:mysqld(8)
Documentation=http://dev.mysql.com/doc/refman/en/using-systemd.html
After=network.target
After=syslog.target
[Install]
WantedBy=multi-user.target
[Service]
User=mysql
Group=mysql
ExecStart=/usr/local/mysql/bin/mysqld --defaults-file=/etc/my.cnf
LimitNOFILE = 5000
[root@centos7-node2 mysql]#systemctl restart mysqld && systemctl enable mysqld
Created symlink from /etc/systemd/system/multi-user.target.wants/mysqld.service to /etc/systemd/system/mysqld.service.
[root@centos7-node2 mysql]# netstat -tunlp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:2202 0.0.0.0:* LISTEN 4981/sshd
tcp6 0 0 :::3306 :::* LISTEN 27977/mysqld
[root@centos7-node2 mysql]# mysql -uroot -p'a1iVw,PhfZLA' -A 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.31
Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.
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> show databases; ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement mysql> alter user 'root'@'localhost' identified by '2wsx!QAZ'; mysql> flush privileges; mysql>exit; 退出后用新密码重新登录 mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | sys | +--------------------+ 4 rows in set (0.00 sec) 同样也可以用:mysql_secure_installation优化一下
实战1:重新初始化安装数据库--如版本升级等情况 (1)备份好重要数据 用mysqldump来备份数据库 (2)重新初始化数据库 A、停止数据库服务 [root@centos7-node2 mysql]# service mysqld stop Shutting down MySQL.. SUCCESS! B、删除data目录 [root@centos7-node2 mysql]# rm -rf /usr/local/mysql/data/ [root@centos7-node2 mysql]# chown -R mysql.mysql . C、重新初始化 [root@centos7-node2 mysql]# /usr/local/mysql/bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data 注意:此处会重新生成密码 root@centos7-node2 mysql]#/usr/local/mysql/bin/mysql_ssl_rsa_setup --datadir=/usr/local/mysql/data [root@centos7-node2 mysql]# chown -R root . [root@centos7-node2 mysql]# chown -R mysql data mysql-files D、参考上面的笔记进行优化
参考链接表示对作者的感谢如下:https://blog.csdn.net/weixin_43453367/article/details/107125619?%3E CentOS7源码mysql-5.7.30自动安装脚本(亲测)
8.2.3:源码安装数据库5.7.31并初始化
[root@centos7-node3 ~]# ip addr |grep inet
inet 192.168.56.13/24 brd 192.168.56.255 scope global ens32
[root@centos7-node3 ~]# yum clean all && yum makecache fast
1、安装源友安装依赖包
解决mysql依赖
[root@centos7-node3 ~]#yum install gcc gcc-c++ cmake bison ncurses ncurses-devel libaio git openssl openssl-devel -y
源码安装boost
[root@centos7-node3 ~]#cd /usr/local/src/
[root@centos7-node3 src]#wget -c http://nchc.dl.sourceforge.net/project/boost/boost/1.59.0/boost_1_59_0.tar.gz
[root@centos7-node3 src]#tar -zxvf boost_1_59_0.tar.gz
[root@centos7-node3 src]# mv boost_1_59_0 /usr/local/boost
下载源码mysql
[root@centos7-node3 src]#wget -c http://mirrors.163.com/mysql/Downloads/MySQL-5.7/mysql-5.7.31.tar.gz
[root@centos7-node3 src]# tar -zxvf mysql-5.7.31.tar.gz
[root@centos7-node3 src]# cd mysql-5.7.31/
[root@centos7-node3 mysql-5.7.31]# groupadd mysql
[root@centos7-node3 mysql-5.7.31]# useradd -r -g mysql -s /bin/false mysql
[root@centos7-node3 mysql-5.7.31]# cmake .
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql
-DSYSCONFDIR=/etc
-DMYSQL_DATADIR=/usr/local/mysql/data
-DINSTALL_MANDIR=/usr/share/man
-DMYSQL_TCP_PORT=3306
-DMYSQL_UNIX_ADDR=/tmp/mysql.sock
-DDEFAULT_CHARSET=utf8
-DEXTRA_CHARSETS=all
-DDEFAULT_COLLATION=utf8_general_ci
-DWITH_READLINE=1
-DWITH_SSL=system
-DWITH_EMBEDDED_SERVER=1
-DENABLED_LOCAL_INFILE=1
-DWITH_INNOBASE_STORAGE_ENGINE=1
-DWITH_BOOST=/usr/local/boost
[root@centos7-node3 ~]#make –j 4 && make install
[root@centos7-node3 ~]#echo $?
[root@centos7-node3 mysql-5.7.31]# cd /usr/local/mysql/
[root@centos7-node3 mysql]#pwd
/usr/local/mysql
[root@centos7-node3 mysql]# mkdir mysql-files
[root@centos7-node3 mysql]# chown -R mysql.mysql .
[root@centos7-node3 mysql]# /usr/local/mysql/bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data
2020-07-17T02:21:57.602140Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use
…….
2020-07-17T02:21:59.466668Z 1 [Note] A temporary password is generated for root@localhost: zE4tudb0<EZH
注意:此处会重新生成密码
[root@centos7-node3 mysql]# bin/mysql_ssl_rsa_setup --datadir=/usr/local/mysql/data
[root@centos7-node3 mysql]# chown -R root .
[root@centos7-node3 mysql]# chown -R mysql data mysql-files
修改/etc/my.cnf内容如下:
[root@centos7-node3 mysql]# cat /etc/my.cnf
[mysqld]
datadir=/usr/local/mysql/data
basedir=/usr/local/mysql
加入服务中,以便正常启动
[root@centos7-node3 mysql]# cp support-files/mysql.server /etc/init.d/mysqld
[root@centos7-node3 mysql]# chkconfig --add mysqld
[root@centos7-node3 mysql]# chkconfig mysqld on
[root@centos7-node3 mysql]# service mysqld start
Starting MySQL.Logging to '/usr/local/mysql/data/centos7-node2.err'.
SUCCESS!
[root@centos7-node3 mysql]# netstat -tunlp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:2202 0.0.0.0:* LISTEN 4981/sshd
tcp6 0 0 :::3306 :::* LISTEN 5803/mysqld
如果不进行以下两步操作,直接执行mysql命令会出现“command not found”的错误提示
[root@centos7-node3 mysql]# echo 'export PATH=$PATH:/usr/local/mysql/bin' >> /etc/profile
[root@centos7-node3 mysql]# source /etc/profile
[root@centos7-node3 mysql]# mysql -uroot -p'a1iVw,PhfZLA' -A
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.31
Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.
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> show databases; ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement mysql> alter user 'root'@'localhost' identified by '2wsx!QAZ'; mysql> flush privileges; mysql>exit; 2、其他部分参考上面的笔记进行优化 8.2.4:脚本自动化安装源码安装数据库5.7.31并初始化 #!/bin/bash #written by wangtao
2020-07-17-10:06:00
################################
echo "安装基本工具" yum install -y vim wget curl tar echo "国内ali源、epel源" wget -O /etc/yum.repos.d/CentOS-aliyun.repo https://mirrors.aliyun.com/repo/Centos-7.repo wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo yum clean all && yum makecache fast
echo "解决mysql依赖" yum install gcc gcc-c++ cmake bison ncurses ncurses-devel libaio git openssl openssl-devel -y
echo "安装源码boost" cd /usr/local/src wget -c http://nchc.dl.sourceforge.net/project/boost/boost/1.59.0/boost_1_59_0.tar.gz tar -zxf boost_1_59_0.tar.gz mv boost_1_59_0 /usr/local/boost
echo "下载源码mysql" cd /usr/local/src wget -c http://mirrors.163.com/mysql/Downloads/MySQL-5.7/mysql-5.7.31.tar.gz tar -zxf mysql-5.7.31.tar.gz
echo"新建用户" groupadd mysql && useradd -r -g mysql -s /bin/false mysql
echo "预编译"
cd /usr/local/src/mysql-5.7.31
cmake .
-DWITH_BOOST=/usr/local/boost
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql
-DSYSCONFDIR=/etc
-DMYSQL_DATADIR=/usr/local/mysql/data
-DINSTALL_MANDIR=/usr/share/man
-DMYSQL_TCP_PORT=3306
-DMYSQL_UNIX_ADDR=/tmp/mysql.sock
-DDEFAULT_CHARSET=utf8
-DEXTRA_CHARSETS=all
-DDEFAULT_COLLATION=utf8_general_ci
-DWITH_READLINE=1
-DWITH_SSL=system
-DWITH_EMBEDDED_SERVER=1
-DENABLED_LOCAL_INFILE=1
-DWITH_INNOBASE_STORAGE_ENGINE=1
echo $?
echo "编译"
make -j 2
echo $?
echo "安装"
make install
echo $?
echo "数据目录授权" cd /usr/local/mysql mkdir /usr/local/mysql/mysql-files chown -R mysql.mysql . /usr/local/mysql/bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data /usr/local/mysql/bin/mysql_ssl_rsa_setup --datadir=/usr/local/mysql/data
echo "拷贝源码目录的脚本文件" cd /usr/local/mysql cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld chmod +x /etc/init.d/mysqld chkconfig --add mysqld chkconfig mysqld on
echo "创建my.cnf配置文件" cat >/etc/my.cnf<<EOF [mysqld] datadir=/usr/local/mysql/data basedir=/usr/local/mysql EOF
echo "启动停止mysql数据库" service mysqld start
#查看mysqld服务; netstat -antp | grep mysqld if(( $?==0 ));then echo "mysqld启动成功";else echo "mysqld启动失败";fi
实战3:破解mysql管理员root用户密码 对于MYSQL或MARIADB所有版本操作步骤如下: 1)停止mysqld服务 systemctl stop mysqld 或 systemctl stop mariadb 2)在配置文件中[mysqld]中添加如下一行 skip-grant-tables 3)重启mysqld服务 systemctl start mysqld 或 systemctl start mariadb 4)用相关语句重新设置密码 [root@node-11 ~]# mysql -u root --此时不用密码就可以登录 1、MYSQL或MARIADB 5.7.5及以前版本 MariaDB [(none)]> select version(); +----------------+ | version() | +----------------+ | 5.5.65-MariaDB | +----------------+ 1 row in set (0.00 sec) MariaDB [(none)]> use mysql; MariaDB [mysql]> update user set password=password('2wsx!QAZ') where user='root'; Query OK, 4 rows affected (0.00 sec) Rows matched: 4 Changed: 4 Warnings: 0 mysql> flush privileges; Query OK, 0 rows affected (0.00 sec) mysql> \q Bye 2、如果是MYSQL或MARIADB 5.7.6及以上版本 mysql> select version(); +-----------+ | version() | +-----------+ | 5.7.31 | +-----------+ 1 row in set (0.00 sec) mysql> use mysql; mysql> select user,host,authentication_string from user; +---------------+-----------+-------------------------------------------+ | user | host | authentication_string | +---------------+-----------+-------------------------------------------+ | root | localhost | *A62C54DF313CE4999C38873B2FFBED2603ADEB83 | | mysql.session | localhost | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE | | mysql.sys | localhost | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE | +---------------+-----------+-------------------------------------------+ 3 rows in set (0.00 sec) mysql> update user set authentication_string=password('2wsx!QAZ') where user='root'; Query OK, 1 row affected, 1 warning (0.00 sec) Rows matched: 1 Changed: 1 Warnings: 1 mysql> flush privileges; Query OK, 0 rows affected (0.00 sec) mysql> \q Bye 5)删除掉在配置文件中[mysqld]中的skip-grant-tables这一行 6)重启数据库服务: systemctl restart mysqld 或 systemctl restart mariadb 7)用新设置的密码登录