這里簡(jiǎn)單的介紹幾種linux平臺(tái)(centos)/mac安裝MySQL,基本上大同小異,通過(guò)一些平臺(tái)的軟件管理工具來(lái)快速安裝MySQL。
在 Centos6 中直接使用 yum 命令去安裝 mysql,極為簡(jiǎn)單,接下來(lái)介紹在Centos6中 通過(guò) yum工具安裝 mysql 的步驟。
檢查是否已安裝
# 檢查 MySQL 是否已安裝
yum list installed | grep mysql
yum list installed mysql*
# 查看當(dāng)前 mysql 版本,看是否已安裝
mysql --version
rpm -qa | grep mysql
更新安裝源
經(jīng)過(guò)查看你可以開(kāi)始安裝了,直接使用 yum 命令去安裝 mysql,按之前可以先看一下源的版本,如果源的版本過(guò)低,可以在下載開(kāi)發(fā)版新的源,在安裝的過(guò)程可以編輯vim /etc/yum.repos.d/mysql-community.repo選擇發(fā)布系列版本,編輯如下,其中參數(shù)enabled的值為 0 的表示禁用,更改為 1 的時(shí)候你就可以默認(rèn)安裝 5.7,否則安裝的將是 5.6 版。
# 安裝如果上面編輯前沒(méi)有看到你想要的版本
# 可以更新源
# ===== CentOS6 =====
# wget http://dev.mysql.com/get/mysql57-community-release-el6-8.noarch.rpm
# ===== CentOS7 =====
wget http://dev.mysql.com/get/mysql-community-release-el7-5.noarch.rpm
yum localinstall mysql-community-release-el7-5.noarch.rpm
# 卸載方法
rpm -e mysql-community-common-5.7.16-1.el6.x86_64
檢查是否下載成功
[root@localhost yum.repos.d]# yum repolist enabled | grep "mysql.*-community.*"
mysql-connectors-community/x86_64 MySQL Connectors Community 36
mysql-tools-community/x86_64 MySQL Tools Community 47
mysql56-community/x86_64 MySQL 5.6 Community Server 327
查看版本
選擇要啟用 MySQL 版本查看 MySQL 版本,執(zhí)行
yum repolist all | grep mysql
可以看到 5.5, 5.7 版本是默認(rèn)禁用的,因?yàn)楝F(xiàn)在最新的穩(wěn)定版是 5.6
[root@localhost yum.repos.d]# yum repolist all | grep mysql
mysql-connectors-community/x86_64 MySQL Connectors Community enabled: 36
mysql-connectors-community-source MySQL Connectors Community - So disabled
mysql-tools-community/x86_64 MySQL Tools Community enabled: 47
mysql-tools-community-source MySQL Tools Community - Source disabled
mysql55-community/x86_64 MySQL 5.5 Community Server disabled
mysql55-community-source MySQL 5.5 Community Server - So disabled
mysql56-community/x86_64 MySQL 5.6 Community Server enabled: 327
mysql56-community-source MySQL 5.6 Community Server - So disabled
mysql57-community-dmr/x86_64 MySQL 5.7 Community Server Deve disabled
mysql57-community-dmr-source MySQL 5.7 Community Server Deve disabled
啟動(dòng)指定版本
可以通過(guò)類(lèi)似下面的語(yǔ)句來(lái)啟動(dòng)某些版本
yum-config-manager --disable mysql56-community
yum-config-manager --enable mysql57-community-dmr
或者通過(guò)修改 /etc/yum.repos.d/mysql-community.repo 文件。
# Note: MySQL 5.7 is currently in development. For use at your own risk.
# Please read with sub pages: https://dev.mysql.com/doc/relnotes/mysql/5.7/en/
[mysql57-community-dmr]
name=MySQL 5.7 Community Server Development Milestone Release
baseurl=http://repo.mysql.com/yum/mysql-5.7-community/el/7/$basearch/
enabled=1
gpgcheck=1
gpgkey=file:/etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
其中 enabled=0 是指禁用,enabled=1 指啟用。注意: 任何時(shí)候,只能啟用一個(gè)版本。
查看當(dāng)前啟用的版本
[root@localhost yum.repos.d]# yum repolist enabled | grep mysql
mysql-connectors-community/x86_64 MySQL Connectors Community 36
mysql-tools-community/x86_64 MySQL Tools Community 47
mysql57-community-dmr/x86_64 MySQL 5.7 Community Server Development 187
安裝MySQL
yum install mysql-server
Retrieving key from file:/etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
Importing GPG key 0x5072E1F5:
Userid : "MySQL Release Engineering <mysql-build@oss.oracle.com>"
Fingerprint: a4a9 4068 76fc bd3c 4567 70c8 8c71 8d3b 5072 e1f5
Package : mysql-community-release-el7-5.noarch (installed)
From : file:/etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
Is this ok [y/N]: y
## 遇到上述提示,輸入 y 繼續(xù),執(zhí)行完成會(huì)提示“完畢!”。此時(shí)MySQL 安裝完成,它包含了 mysql-community-server、mysql-community-client、mysql-community-common、mysql-community-libs 四個(gè)包。
......
Installed:
mysql-community-libs.x86_64 0:5.7.18-1.el7 mysql-community-libs-compat.x86_64 0:5.7.18-1.el7 mysql-community-server.x86_64 0:5.7.18-1.el7
Dependency Installed:
mysql-community-client.x86_64 0:5.7.18-1.el7 mysql-community-common.x86_64 0:5.7.18-1.el7 net-tools.x86_64 0:2.0-0.17.20131004git.el7
Replaced:
mariadb-libs.x86_64 1:5.5.44-2.el7.centos
執(zhí)行
rpm -qi mysql-community-server.x86_64 0:5.6.24-3.el7
查看安裝目錄
whereis mysql
mysql: /usr/bin/mysql /usr/lib64/mysql /usr/share/mysql /usr/share/man/man1/mysql.1.gz
可以看到 MySQL 的安裝目錄是 /usr/bin/
MariaDB切換MySQL
Centos7 上面默認(rèn)是 MariaDB 不是MySQL了,所以 Centos6 安裝會(huì)報(bào)錯(cuò)。在 Centos7 中,運(yùn)行下面命令安裝就不會(huì)有錯(cuò)誤了,但是你的數(shù)據(jù)庫(kù)被換成了MariaDB,MariaDB登錄鏈接SQL語(yǔ)句等幾乎一樣。
# 安裝 mariadb-server
yum install mariadb-server -y
# 啟動(dòng) mariadb
systemctl start mariadb.service
systemctl enable mariadb.service
那在 Centos7 中默認(rèn)安裝 MySQL 會(huì)報(bào)錯(cuò)誤,大概是我下面這樣樣子:
--> 正在處理依賴關(guān)系 libmysqlclient.so.18(libmysqlclient_18)(64bit),它被軟件包 2:postfix-2.10.1-6.el7.x86_64 需要
---> 軟件包 mysql-community-libs.x86_64.0.5.7.16-1.el6 將被 舍棄
--> 解決依賴關(guān)系完成
錯(cuò)誤:軟件包:2:postfix-2.10.1-6.el7.x86_64 (@anaconda)
需要:libmysqlclient.so.18(libmysqlclient_18)(64bit)
正在刪除: 1:mariadb-libs-5.5.50-1.el7_2.x86_64 (@updates)
libmysqlclient.so.18(libmysqlclient_18)(64bit)
取代,由: mysql-community-libs-5.7.16-1.el6.x86_64 (mysql57-community)
....
如果你非要用 MySQL 那你先卸載 yum remove mariadb 和 yum remove mysql-libs 重新進(jìn)行安裝 yum install mysql-server 或許可解決 Centos7 安裝mysql報(bào)上面錯(cuò)誤的問(wèn)題,具體問(wèn)題還得具體看。
# 卸載 mariadb 和 mysql-libs
yum remove mariadb
yum remove mysql-libs
# 安裝mysql
yum install mysql-server
安裝發(fā)生依賴錯(cuò)誤
--> Finished Dependency Resolution
Error: Package: mysql-community-server-5.7.19-1.el6.x86_64 (mysql57-community)
Requires: libsasl2.so.2()(64bit)
You could try using --skip-broken to work around the problem
You could try running: rpm -Va --nofiles --nodigest
通過(guò)更新來(lái)解決
yum update
# 通過(guò)提示來(lái)解決錯(cuò)誤,80%軟件不能用,或者部分功能不能用
# 不建議下面方式解決
yum install mysql-server --skip-broken
rpm -Va --nofiles --nodiges
卸載 MySQL 軟件
卸載mysql
通過(guò)下面這條命令查看本地mysql安裝包
# Linux查看
rpm -qa | grep -i mysql
# 輸出結(jié)果
perl-DBD-MySQL-4.013-3.el6.x86_64
mysql-community-libs-5.6.33-2.el6.x86_64
mysql-community-release-el6-5.noarch
mysql-community-client-5.6.33-2.el6.x86_64
compat-mysql51-5.1.54-1.el6.remi.x86_64
mysql-community-server-5.6.33-2.el6.x86_64
mysql-community-common-5.6.33-2.el6.x86_64
你下面命令將它刪除
rpm -e mysql-community-server-5.6.33-2.el6.x86_64
rpm -e compat-mysql51-5.1.54-1.el6.remi.x86_64
刪除mysql服務(wù)
chkconfig --list | grep -i mysql
chkconfig --del mysql
刪除分散mysql文件夾
清空相關(guān)mysql的所有目錄以及文件,whereis mysql 或者 find / -name mysql 找到所有文件夾。
rm -rf /usr/lib/mysql
rm -rf /usr/share/mysql
rm -rf /usr/my.cnf