一、安裝
1、下載MySQL離線安裝包
下載地址:https://dev.mysql.com/downloads/mysql/
選擇如下:
2、上傳tar壓縮包到服務器
這里也可以新建文件夾放也行,我就通過xftp軟件上傳放在opt的目錄下
3、刪除原有的mariadb
先通過rpm -qa|grep mariadb命令查看是否安裝mariadb-libs庫,如果能查的到就通過rpm -e --nodeps mariadb-libs命令刪除,不刪除的話等下安裝MySQL會出現(xiàn)報錯,有沖突。
[root@localhost /]# rpm -qa|grep mariadb
mariadb-libs-5.5.68-1.el7.x86_64
[root@localhost /]# rpm -e --nodeps mariadb-libs
[root@localhost /]# rpm -qa|grep mariadb
[root@localhost /]#
4、解壓縮mysql離線安裝包
通過 cd /opt 進入之前存放的數(shù)據庫壓縮包路徑,然后ls 打印下文件信息,最后通過tar -xvf mysql-8.0.16-2.el7.x86_64.rpm-bundle.tar來解壓文件,記得壓縮包名要核對下,每個版本的名稱不一致,不然解壓找不到文件。
[root@localhost /]# cd /opt
[root@localhost opt]# ls
mysql-8.2.0-1.el7.x86_64.rpm-bundle.tar rh
[root@localhost opt]# tar -xvf mysql-8.0.16-2.el7.x86_64.rpm-bundle.tar
tar: mysql-8.0.16-2.el7.x86_64.rpm-bundle.tar:無法 open: 沒有那個文件或目錄
tar: Error is not recoverable: exiting now
[root@localhost opt]# tar -xvf mysql-8.2.0-1.el7.x86_64.rpm-bundle.tar
mysql-community-client-8.2.0-1.el7.x86_64.rpm
mysql-community-client-plugins-8.2.0-1.el7.x86_64.rpm
mysql-community-common-8.2.0-1.el7.x86_64.rpm
mysql-community-debuginfo-8.2.0-1.el7.x86_64.rpm
mysql-community-devel-8.2.0-1.el7.x86_64.rpm
mysql-community-embedded-compat-8.2.0-1.el7.x86_64.rpm
mysql-community-icu-data-files-8.2.0-1.el7.x86_64.rpm
mysql-community-libs-8.2.0-1.el7.x86_64.rpm
mysql-community-libs-compat-8.2.0-1.el7.x86_64.rpm
mysql-community-server-8.2.0-1.el7.x86_64.rpm
mysql-community-server-debug-8.2.0-1.el7.x86_64.rpm
mysql-community-test-8.2.0-1.el7.x86_64.rpm
5、安裝rmp包
逐個安裝,按順序來,如果包名不一致,記得改,命令如下:
rpm -ivh mysql-community-common-8.2.0-1.el7.x86_64.rpm
rpm -ivh mysql-community-libs-8.2.0-1.el7.x86_64.rpm
rpm -ivh mysql-community-client-8.2.0-1.el7.x86_64.rpm
rpm -ivh mysql-community-server-8.2.0-1.el7.x86_64.rpm
出現(xiàn)錯誤
錯誤的提示也很明細,提示我們提前安裝mysql-community-client-plugins = 8.2.0-1.el7這個rpm包,之前解壓的tar文件都有
[root@localhost opt]# rpm -ivh mysql-community-libs-8.2.0-1.el7.x86_64.rpm
警告:mysql-community-libs-8.2.0-1.el7.x86_64.rpm: 頭V4 RSA/SHA256 Signature, 密鑰 ID 3a79bd29: NOKEY
錯誤:依賴檢測失?。? mysql-community-client-plugins = 8.2.0-1.el7 被 mysql-community-libs-8.2.0-1.el7.x86_64 需要
解決方法
目前安裝rpm -ivh mysql-community-libs-8.2.0-1.el7.x86_64.rpm出現(xiàn)的需要mysql-community-client-plugins = 8.2.0-1.el7這個依賴,所以找到之前我們解壓的包,提前安裝mysql-community-client-plugins = 8.2.0-1.el7.x86_64.rpm,后面在安裝就不會報錯了,剩下的rpm包報錯也是這樣子處理
[root@localhost opt]# rpm -ivh mysql-community-client-plugins-8.2.0-1.el7.x86_64.rpm
警告:mysql-community-client-plugins-8.2.0-1.el7.x86_64.rpm: 頭V4 RSA/SHA256 Signature, 密鑰 ID 3a79bd29: NOKEY
準備中... ################################# [100%]
正在升級/安裝...
1:mysql-community-client-plugins-8.################################# [100%]
[root@localhost opt]# rpm -ivh mysql-community-libs-8.2.0-1.el7.x86_64.rpm
警告:mysql-community-libs-8.2.0-1.el7.x86_64.rpm: 頭V4 RSA/SHA256 Signature, 密鑰 ID 3a79bd29: NOKEY
準備中... ################################# [100%]
正在升級/安裝...
1:mysql-community-libs-8.2.0-1.el7 ################################# [100%]
6、查看是否安裝成功
[root@localhost opt]# rpm -qa|grep mysql
mysql-community-icu-data-files-8.2.0-1.el7.x86_64
mysql-community-client-plugins-8.2.0-1.el7.x86_64
mysql-community-server-8.2.0-1.el7.x86_64
mysql-community-libs-8.2.0-1.el7.x86_64
mysql-community-client-8.2.0-1.el7.x86_64
mysql-community-common-8.2.0-1.el7.x86_64
二、啟動MySQL服務
1、查看MySQL服務狀態(tài)
[root@localhost opt]# 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
發(fā)現(xiàn)狀態(tài)是出于enabled(啟動)
2、關閉MySQL服務
[root@localhost opt]# systemctl stop mysqld
3、初始化數(shù)據庫
成功了也沒什么提示
[root@localhost opt]# mysqld --initialize --console
4、目錄授權
在 MySQL 中,我們可以應用 chown 命令來控制數(shù)據庫的讀寫權限。例如,下列命令的作用是將 MySQL 數(shù)據庫的所有者和所屬組修改為 mysql,以保證數(shù)據庫的安全性:
[root@localhost opt]# chown -R mysql:mysql /var/lib/mysql/
通過 chown 命令修改 MySQL 數(shù)據庫的所有者和所屬組,我們可以確保 MySQL 數(shù)據庫的安全性和穩(wěn)定性。同時,我們也需要注意文件權限的改變是否可能會對數(shù)據庫的正常運行產生影響,特別是在生產環(huán)境中,我們需要謹慎處理,避免對系統(tǒng)造成不必要的損失。
注意事項
- chown 命令需要以 root 權限運行,否則可能會被拒絕訪問。
- 在修改文件權限之前,需要先備份重要數(shù)據,以避免不必要的損失。
- 如果文件權限發(fā)生改變,可能會影響 MySQL 數(shù)據庫的正常運行,因此需要謹慎處理。
- 在修改 MySQL 數(shù)據庫的所有者和所屬組時,需要確保沒有正在運行的 MySQL 進程,否則可能會導致服務不可用。
5、啟動MySQL服務
通過systemctl start mysqld開啟服務,在通過netstat -tlpn執(zhí)行最后一行可以看到我們的MySQL服務的運行端口進程,說明已經成功,接下來是進去修改初始密碼。
[root@localhost opt]# systemctl start mysqld
[root@localhost opt]# netstat -tlpn
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:111 0.0.0.0:* LISTEN 758/rpcbind
tcp 0 0 192.168.122.1:53 0.0.0.0:* LISTEN 1515/dnsmasq
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1239/sshd
tcp 0 0 127.0.0.1:631 0.0.0.0:* LISTEN 1240/cupsd
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1448/master
tcp 0 0 127.0.0.1:6010 0.0.0.0:* LISTEN 2681/sshd: root@pts
tcp 0 0 127.0.0.1:6011 0.0.0.0:* LISTEN 55358/sshd: root@pt
tcp6 0 0 :::111 :::* LISTEN 758/rpcbind
tcp6 0 0 :::22 :::* LISTEN 1239/sshd
tcp6 0 0 ::1:631 :::* LISTEN 1240/cupsd
tcp6 0 0 ::1:25 :::* LISTEN 1448/master
tcp6 0 0 ::1:6010 :::* LISTEN 2681/sshd: root@pts
tcp6 0 0 ::1:6011 :::* LISTEN 55358/sshd: root@pt
tcp6 0 0 :::33060 :::* LISTEN 56532/mysqld
tcp6 0 0 :::3306 :::* LISTEN 56532/mysqld
三、數(shù)據庫操作
1、查看臨時密碼
通過命令:cat /var/log/mysqld.log 執(zhí)行,root@localhost:后面是密碼,建議登錄的時候復制粘貼比較好。
2、用臨時密碼登錄數(shù)據庫
命令:mysql -u root -p 回車鍵
然后復制粘貼臨時密碼(輸入時不會顯示出來,輸入完直接回車)
[root@localhost opt]# mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.2.0
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.
3、修改臨時密碼
mysql> alter USER 'root'@'localhost' IDENTIFIED BY '123456';
Query OK, 0 rows affected (0.03 sec)
4、授權遠程連接
通過use mysql;使用MySQL庫,執(zhí)行select host, user, authentication_string from user;發(fā)現(xiàn)本地root用戶是主機號localhost,所以需要將host主機改為%.
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> select host, user, authentication_string from user;
+-----------+------------------+------------------------------------------------------------------------+
| host | user | authentication_string |
+-----------+------------------+------------------------------------------------------------------------+
| localhost | mysql.infoschema | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED |
| localhost | mysql.session | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED |
| localhost | mysql.sys | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED |
| localhost | root | $A$005$-G(mK8y%."8%\!yMvV4bBdHE6w6YOCkQwWpTYBiKY3Jw6Ph1C61xWKrSf. |
+-----------+------------------+------------------------------------------------------------------------+
4 rows in set (0.00 sec)
通過update user set host = “%” where user=‘root’;修改主機為%,在查看下內容select host, user, authentication_string from user;,然后刷新權限flush privileges;
mysql> update user set host = "%" where user='root';
Query OK, 1 row affected (0.01 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select host, user, authentication_string from user;
+-----------+------------------+------------------------------------------------------------------------+
| host | user | authentication_string |
+-----------+------------------+------------------------------------------------------------------------+
| % | root | $A$005$-G(mK8y%."8%\!yMvV4bBdHE6w6YOCkQwWpTYBiKY3Jw6Ph1C61xWKrSf. |
| localhost | mysql.infoschema | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED |
| localhost | mysql.session | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED |
| localhost | mysql.sys | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED |
+-----------+------------------+------------------------------------------------------------------------+
4 rows in set (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
5、使用navicat遠程連接出現(xiàn)一下報錯信息,這是linux防火墻處于開啟狀態(tài)導致的
6、遠程防火墻設置
方案1:關閉防火墻
systemctl stop firewalld.service #停止firewall
systemctl disable firewalld.service #禁止firewall開機啟動
方案2:開放訪問端口
1、開放端口:firewall-cmd --zone=public --add-port=3306/tcp --permanent
命令含義:
--zone #作用域
--add-port=3306/tcp #添加端口,格式為:端口/通訊協(xié)議
--permanent #永久生效,沒有此參數(shù)重啟后失效
2、重啟生效:firewall-cmd --reload
本人采用的是第二種方案
[root@localhost opt]# firewall-cmd --zone=public --add-port=3306/tcp --permanent
success
[root@localhost opt]# firewall-cmd --reload
success
7、再次使用navicat連接,發(fā)現(xiàn)連接成功
文章來源:http://www.zghlxwxcb.cn/news/detail-854161.html
四、總結
本人也是參考了一些信息,做了融合,參考文獻如下。
https://www.cnblogs.com/quchunhui/p/11115339.html
https://blog.csdn.net/rao991207823/article/details/119840876
關于linux查看防火墻狀態(tài)的內容可以參考這個
https://blog.csdn.net/qq_36640713/article/details/106553833文章來源地址http://www.zghlxwxcb.cn/news/detail-854161.html
到了這里,關于linux離線安裝mysql的文章就介紹完了。如果您還想了解更多內容,請在右上角搜索TOY模板網以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網!