注意:在MYSQL的安裝與卸載中,需要使用root用戶進行。
一、卸載不必要的環境
? 查看是否有運行的服務
[root@VM-24-10-centos etc]# ps axj |grep mysql1 22030 22029 22029 ? -1 Sl 27 0:00 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid3096 22531 22530 3096 pts/0 22530 R+ 0 0:00 grep --color=auto mysql
[root@VM-24-10-centos etc]# ps axj |grep maridb3096 22672 22671 3096 pts/0 22671 S+ 0 0:00 grep --color=auto maridb
我們可以看到當前的機器上是存在著mysql的,如果沒有則可直接跳過這步操作,下面的操作對應maridb也是一樣的。
? 關閉運行的服務
[root@VM-24-10-centos etc]# systemctl stop mysqld
[root@VM-24-10-centos etc]# ps axj |grep mysql3096 23210 23209 3096 pts/0 23209 S+ 0 0:00 grep --color=auto mysql
? 查找服務器上的mysql安裝包
[root@VM-24-10-centos etc]# rpm -qa|grep mysql
mysql-community-client-5.7.44-1.el7.x86_64
mysql-community-server-5.7.44-1.el7.x86_64
mysql-community-common-5.7.44-1.el7.x86_64
mysql-community-libs-compat-5.7.44-1.el7.x86_64
mysql57-community-release-el7-11.noarch
mysql-community-libs-5.7.44-1.el7.x86_64
? 卸載全部的安裝包
[root@VM-24-10-centos etc]# rpm -qa|grep mysql|xargs yum -y remove
Loaded plugins: fastestmirror, langpacks
Repository epel is listed more than once in the configuration
……
……
Complete!
? 再次確認
[root@VM-24-10-centos etc]# ls /etc/my.cnf
ls: cannot access /etc/my.cnf: No such file or directory
[root@VM-24-10-centos etc]# rpm -qa|grep mysql
[root@VM-24-10-centos etc]#
這就說明我們卸載干凈了。
二、安裝MySQL
? 獲取mysql官方的yum源
這是網址:http://repo.mysql.com/
,對于下載的yum源我們需要與當前系統相結合下載。
[root@VM-24-10-centos etc]# cat /etc/redhat-release
CentOS Linux release 7.6.1810 (Core)
因此我們最好能下載CentOS7.6版本的,但是我們查看后發現并沒有這一版本,因此我們下載的是http://repo.mysql.com/mysql57-community-release-el7.rpm
。
我們將其下載到windows端,然后通過rzsz小工具傳輸到Linux端。
[root@VM-24-10-centos ~]# rz[root@VM-24-10-centos ~]# ll
total 1
-rw-r--r-- 1 root root 25680 Dec 28 16:53 mysql57-community-release-el7.rpm
? 安裝mysql源
[root@VM-24-10-centos ~]# rpm -ivh mysql57-community-release-el7.rpm
Preparing... ################################# [100%]
Updating / installing...1:mysql57-community-release-el7-11 ################################# [100%]
我們可以看到在/etc/yum.repos.d/
目錄下多了與mysql相關的兩個源。
root@VM-24-10-centos ~]# ll /etc/yum.repos.d/
total 24
-rw-r--r-- 1 root root 614 Apr 14 2024 CentOS-Base.repo
-rw-r--r-- 1 root root 230 Apr 14 2024 CentOS-Epel.repo
-rw-r--r-- 1 root root 1358 Sep 5 2021 epel.repo
-rw-r--r-- 1 root root 1457 Sep 5 2021 epel-testing.repo
-rw-r--r-- 1 root root 1838 Apr 27 2017 mysql-community.repo
-rw-r--r-- 1 root root 1885 Apr 27 2017 mysql-community-source.repo
? 確認MySQL源安裝成功
[root@VM-24-10-centos ~]# yum list|grep mysql
Repository epel is listed more than once in the configuration
mysql57-community-release.noarch el7-11 installed
akonadi-mysql.x86_64 1.9.2-4.el7 os
anope-mysql.x86_64 2.1.4-1.el7 epel
apr-util-mysql.x86_64 1.5.2-6.el7_9.1 updates
calligra-kexi-driver-mysql.x86_64 2.9.10-2.el7 epel
collectd-mysql.x86_64 5.8.1-2.el7 epel
dmlite-plugins-mysql.x86_64 1.15.2-15.el7 epel
dovecot-mysql.x86_64 1:2.2.36-8.el7 os
dpm-copy-server-mysql.x86_64 1.13.0-1.el7 epel
dpm-name-server-mysql.x86_64 1.13.0-1.el7 epel
dpm-server-mysql.x86_64 1.13.0-1.el7 epel
? 安裝MySQL
[root@VM-24-10-centos ~]# yum install -y mysql-community-server
Loaded plugins: fastestmirror, langpacks
Repository epel is listed more than once in the configuration
……
Complete!
可能會碰到安裝遇到秘鑰過期的問題:
Failing package is: mysql-community-client-5.7.39-1.el7.x86_64 GPG Keys are configured as: file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
解決方案:
rpm --import https://repo.mysql.com/RPM-GPG-KEY-mysql-2022
? 確認MySQL安裝成功
[root@VM-24-10-centos ~]# ls /etc/my.cnf
/etc/my.cnf
[root@VM-24-10-centos ~]# which mysql
/usr/bin/mysql
[root@VM-24-10-centos ~]# which mysqld
/usr/sbin/mysqld
? 啟動MySQL
[root@VM-24-10-centos ~]# systemctl start mysqld
[root@VM-24-10-centos ~]# ps axj |grep mysql1 16034 16033 16033 ? -1 Sl 27 0:00 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid3096 16076 16075 3096 pts/0 16075 S+ 0 0:00 grep --color=auto mysql
三、登錄MySQL
[root@VM-24-10-centos ~]# mysql -uroot -p
Enter password:
這里我們看到登錄MySQL需要密碼,但是我們不知道密碼。最直接的方法是修改MySQL的配置文件,我們只需要在配置文件末尾加上skip-grant-tables
。
[root@VM-24-10-centos ~]# vim /etc/my.cnf
[root@VM-24-10-centos ~]# systemctl restart mysqld
不要忘記配置完文件需要重新啟動MySQL。
[root@VM-24-10-centos ~]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.44 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>
我們直接按回車鍵就進入mysql了。
四、設置配置文件
繼續配置
# 端口號設為默認值
port=3306
# 編碼方式使用utf-8
character-set-server=utf8
# 選擇存儲引擎
default-storage-engine=innodb
配置完成后再次重啟即可。
#開啟開機?啟動
systemctl enable mysqld
systemctl daemon-reload
至于自啟動配置,筆者使用的是云服務器就不配置了。