centos7 yum配置安裝Mariadb資料庫(使用國內Mariadb源)
CentOS 6 或早期的版本中提供的是 MySQL 的伺服器/客戶端安裝包,但 CentOS 7 已使用了 MariaDB 替代了預設的 MySQL。MariaDB資料庫管理系統是MySQL的一個分支,主要由開源社群在維護,採用GPL授權許可 MariaDB的目的是完全相容MySQL,包括API和命令列,使之能輕鬆成為MySQL的代替品。
Linux下安裝MariaDB官方文件參見:官網地址
全部刪除MySQL/MariaDB
MySQL 已經不再包含在 CentOS 7 的源中,而改用了 MariaDB;
1.使用
rpm -qa | grep MariaDB
搜尋 MariaDB 現有的包;
如果存在(刪不了的話,就一個一個刪),使用
rpm -e --nodeps MariaDB-*
全部刪除;
2.使用
rpm -qa | grep mysql
搜尋 mysql 現有的包:
如果存在(刪除你實際查詢出來的mysql結果即可),使用
yum remove mysql mysql-server mysql-libs compat-mysql51
全部刪除;
3.開始新的安裝, 建立MariaDB.repo檔案
vim /etc/yum.repos.d/MariaDB.repo
插入一下內容,第二個我換成了國內源中科大的(有問題可檢視,網址
[mariadb]
name = MariaDB
baseurl = http://yum.mariadb.org/10.1/centos7-amd64
gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB
gpgcheck=1
[mariadb] name = MariaDB baseurl = https://mirrors.ustc.edu.cn/mariadb/yum/10.1/centos7-amd64/ gpgkey=https://mirrors.ustc.edu.cn/mariadb/yum/RPM-GPG-KEY-MariaDB gpgcheck=1
系統及版本選擇,你可以根據這個網址(國內)去選擇你想安裝centos7 amd64位其他版本號的版本,網址。直接修改baseurl中的版本號即可。安裝Mariadb之前,你可以先匯入GPG key
rpm --import https://yum.mariadb.org/RPM-GPG-KEY-MariaDB
4.執行安裝命令安裝MariaDB(安裝過程可能會比較漫長,使用中科大的就快了)
yum -y install MariaDB-server MariaDB-client
5.下載安裝包,進行自動安裝,安裝成功之後啟動MariaDB服務,並設為開機自啟。
systemctl start mariadb #啟動服務
systemctl enable mariadb #設定開機啟動
systemctl restart mariadb #重新啟動
systemctl stop mariadb.service #停止MariaDB
6.登入到資料庫用
mysql -uroot -p
登入到MariaDB,此時root賬戶的密碼為空,直接回車即可,退出Mariadb,exit;即可。
7.進行MariaDB的相關簡單配置,使用
mysql_secure_installation
命令進行配置(先退出資料庫)。
首先是設定密碼,會提示先輸入密碼
Enter current password for root (enter for none):<–初次執行直接回車
設定密碼
Set root password? [Y/n] <– 是否設定root使用者密碼,輸入y並回車或直接回車
New password: <– 設定root使用者的密碼
Re-enter new password: <– 再輸入一次你設定的密碼
其他配置
Remove anonymous users? [Y/n] <– 是否刪除匿名使用者,回車
Disallow root login remotely? [Y/n] <–是否禁止root遠端登入,回車(後面授權配置)
Remove test database and access to it? [Y/n] <– 是否刪除test資料庫,回車
Reload privilege tables now? [Y/n] <– 是否重新載入許可權表,回車
初始化MariaDB完成,直接登入,成功。
8.配置MariaDB的字符集
使用vim /etc/my.cnf.d/server.cnf命令編輯server.cnf檔案,在[mysqld]標籤下新增:
init_connect='SET collation_connection = utf8_unicode_ci'
init_connect='SET NAMES utf8'
character-set-server=utf8
collation-server=utf8_unicode_ci
skip-character-set-client-handshake
配置初始化完成,重啟Mariadb。
systemctl restart mariadb
之後進入Mariadb,檢視字符集。
show variables like "%character%";show variables like "%collation%";
9.新增使用者,設定許可權
建立使用者命令(使用者名稱,密碼請自行修改)
create user [email protected] identified by 'password';
授予外網登陸許可權
grant all privileges on *.* to [email protected]'%' identified by 'password';
select host,user,password from user;
簡單的使用者和許可權配置就完成了。
授予部分許可權只需把all privileges改為select,insert,update,delete,create,drop,index,alter,grant,references,reload,shutdown,process,file其中一部分即可。