跟高手學習LINUX筆記-29
--MYSQL5.7安裝部署
第八章 MYSQL5.7安裝部署
本節所講內容:
8.一、數據庫簡介及本筆記包含內容概要
8.二、MYSQL5.7安裝部署三種方法及自動源碼安裝腳本
8.三、實戰--MYSQL破解密碼html
實驗環境:
服務器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後破解密碼node
正文部分:
8.一、數據庫簡介及本筆記包含內容概要
8.1.一、數據庫簡介
一、數據庫管理三個歷史階段
1)人工管理階段
2)文件夾式管理階段
3)數據庫管理階段
歷史僅僅是歷史,只是一個經歷的過程
二、數據庫的構成
1)DBMS:數據庫管理系統,也就是管理數據庫的各個軟件
A:用二維表做爲存儲格式的數據庫管理系統:ORACLE/SQL SERVER/MYSQL/DB2等
B:用key-values做爲存儲格式的數據庫管理系統:REDIS/,MONOGDB/MEMCACHE等
2)DBA:管理數據庫的專業人員
三、SQL語言分類--結構化查詢語言,也是數據庫最經常使用語言
1)DDL:數據庫定義語言--數據庫中庫、表、索引等建立、刪除等操做:CREATE DROP ALTER
2)DML:數據庫操做語言--數據庫中對數據的增(insert)、刪(delete)、改(update)操做
3)DQL:數據庫查詢語言--數據庫對數據各類查詢的操做(select)
4)DCL:數據庫控制語言--數據庫權限等管理(grant/revoke)
四、本筆記包含內容概要
1)安裝與部署
2)備份恢復
3)主從複製
4)分佈式數據庫(Galera Cluster for MySQL )
5)讀寫分離
6)HA架構(MHA)
7)性能優化
8)Redis羣集實現
9)MONOGDB羣集實現
我的心得:數據庫學習不易,且練且提升mysql
8.二、MYSQL5.7安裝部署三種方法
8.2.1:Yum二進制安裝數據庫5.7.31並初始化
須要文件:mysql57-community-release-el7-10.noarch.rpm
一、查詢本機是否有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
二、下載相關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.repolinux
[root@centos7-node1 ~]# yum clean all && yum makecache fastc++
[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.noarchgit
[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 ~]#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/mysqldsql
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.31vim
Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.centos
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
一、安裝源友安裝依賴包
解決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;
二、其餘部分參考上面的筆記進行優化
8.2.4:腳本自動化安裝源碼安裝數據庫5.7.31並初始化
#!/bin/bash
#written by wangtao
################################
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 --此時不用密碼就能夠登陸
一、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
二、若是是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: 1mysql> flush privileges;Query OK, 0 rows affected (0.00 sec)mysql> \qBye5)刪除掉在配置文件中[mysqld]中的skip-grant-tables這一行6)重啓數據庫服務:systemctl restart mysqld 或 systemctl restart mariadb7)用新設置的密碼登陸