共計 8103 個字符,預(yù)計需要花費 21 分鐘才能閱讀完成。
這篇文章主要介紹“CentOS7.X 怎么安裝 MariaDB 數(shù)據(jù)庫”,在日常操作中,相信很多人在 CentOS7.X 怎么安裝 MariaDB 數(shù)據(jù)庫問題上存在疑惑,丸趣 TV 小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”CentOS7.X 怎么安裝 MariaDB 數(shù)據(jù)庫”的疑惑有所幫助!接下來,請跟著丸趣 TV 小編一起來學(xué)習(xí)吧!
一、安裝數(shù)據(jù)庫前準備:
1、查看 虛機版本
[root@ywl0115 ~]# cat /etc/centos-releaseCentOS Linux release 7.3.1611 (Core)
2、查看 虛機是否有自帶 MariaDB 數(shù)據(jù)庫
[root@ywl0115 ~]# rpm -qa | grep mariadbmariadb-libs-5.5.52-1.el7.x86_64
3、如果有 MariaDB 數(shù)據(jù)庫,卸載自帶的
[root@ywl0115 ~]# rpm -e mariadb-libs-5.5.52-1.el7.x86_64
二、添加 yum 源,安裝數(shù)據(jù)庫
1、添加 MariaDB.repo 文件
MaraDB 數(shù)據(jù)官網(wǎng) yum 連接
https://downloads.mariadb.org/mariadb/repositories/#mirror=tuna distro=CentOS distro_release=centos7-amd64–centos7 version=10.2
選中版本后把紅色框的文字拷貝到 MariaDB.repo
[root@ywl0115 ~]# cd /etc/yum.repos.d/[root@ywl0115 yum.repos.d]# lsCentOS-Base.repo CentOS-Debuginfo.repo CentOS-Media.repo CentOS-Vault.repoCentOS-CR.repo CentOS-fasttrack.repo CentOS-Sources.repo[root@ywl0115 yum.repos.d]# vim MariaDB.repo [root@ywl0115 yum.repos.d]# lsCentOS-Base.repo CentOS-Debuginfo.repo CentOS-Media.repo CentOS-Vault.repoCentOS-CR.repo CentOS-fasttrack.repo CentOS-Sources.repo MariaDB.repo[root@ywl0115 yum.repos.d]# cat MariaDB.repo # MariaDB 10.2 CentOS repository list – created 2019-01-18 09:12 UTC# http://downloads.mariadb.org/mariadb/repositories/[mariadb]name = MariaDBbaseurl = http://yum.mariadb.org/10.2/centos7-amd64gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDBgpgcheck=1[root@ywl0115 yum.repos.d]#
2、使用 YUM 安裝數(shù)據(jù)庫 MariaDB
(1)、yum 安裝
yum -y install MariaDB-client MariaDB-server MariaDB-devel 參數(shù) -y 是確認,不用提示,不輸時安裝過程中會出現(xiàn)提示需要選擇 MariaDB-server:服務(wù)器端 MariaDB-client:客戶端,用于連接并操作 Mysql 服務(wù)器 MariaDB-devel:包含開發(fā)首要的文件和一些靜態(tài)庫,可以不安裝,如果你想要編譯其它 MySQL 客戶端,例如 Perl 模塊,則需要安裝該 RPM 包
(2)、啟動數(shù)據(jù)庫
[root@ywl0115 ~]# systemctl start mariadb ## 開啟[root@ywl0115 ~]# systemctl status mariadb ## 查看狀態(tài)● mariadb.service – MariaDB 10.2.21 database server Loaded: loaded (/usr/lib/systemd/system/mariadb.service; enabled; vendor preset: disabled) Drop-In: /etc/systemd/system/mariadb.service.d └─migrated-from-my.cnf-settings.conf Active: active (running) since Fri 2019-01-18 17:49:39 CST; 8s ago Docs: man:mysqld(8) https://mariadb.com/kb/en/library/systemd/ Process: 2598 ExecStartPost=/bin/sh -c systemctl unset-environment _WSREP_START_POSITION (code=exited, status=0/SUCCESS) Process: 2553 ExecStartPre=/bin/sh -c [! -e /usr/bin/galera_recovery] VAR= || VAR=`/usr/bin/galera_recovery`; [$? -eq 0] systemctl set-environment _WSREP_START_POSITION=$VAR || exit 1 (code=exited, status=0/SUCCESS) Process: 2551 ExecStartPre=/bin/sh -c systemctl unset-environment _WSREP_START_POSITION (code=exited, status=0/SUCCESS) Main PID: 2565 (mysqld) Status: Taking your SQL requests now… CGroup: /system.slice/mariadb.service └─2565 /usr/sbin/mysqldJan 18 17:49:39 ywl0115.novalocal mysqld[2565]: 2019-01-18 17:49:39 140654635174016 [Note] Pl…d.Jan 18 17:49:39 ywl0115.novalocal mysqld[2565]: 2019-01-18 17:49:39 140653568702208 [Note] In…39Jan 18 17:49:39 ywl0115.novalocal mysqld[2565]: 2019-01-18 17:49:39 140654635174016 [Note] Se… .Jan 18 17:49:39 ywl0115.novalocal mysqld[2565]: 2019-01-18 17:49:39 140654543447808 [Warning]…deJan 18 17:49:39 ywl0115.novalocal mysqld[2565]: 2019-01-18 17:49:39 140654543447808 [Warning]…deJan 18 17:49:39 ywl0115.novalocal mysqld[2565]: 2019-01-18 17:49:39 140654635174016 [Note] Re…edJan 18 17:49:39 ywl0115.novalocal mysqld[2565]: 2019-01-18 17:49:39 140654635174016 [Note] Ad…leJan 18 17:49:39 ywl0115.novalocal mysqld[2565]: 2019-01-18 17:49:39 140654635174016 [Note] /u…s.Jan 18 17:49:39 ywl0115.novalocal mysqld[2565]: Version: 10.2.21-MariaDB socket: /var/lib…erJan 18 17:49:39 ywl0115.novalocal systemd[1]: Started MariaDB 10.2.21 database server.Hint: Some lines were ellipsized, use -l to show in full.
如果看到:
Active: active (running) since Fri 2019-01-18 17:49:39 CST; 8s ago
啟動成功。
(3)、MariaDB 安全配置(這是和 MySQL 最大的不同)
輸入命令: mysql_secure_installation①、首先是設(shè)置密碼,會提示先輸入密碼 Enter current password for root (enter for none): –初次運行直接回車②、設(shè)置密碼 Set root password? [Y/n] – 是否設(shè)置 root 用戶密碼,輸入 y 并回車或直接回車 New password: – 設(shè)置 root 用戶的密碼 Re-enter new password: – 再輸入一次你設(shè)置的密碼 Remove anonymous users? [Y/n] – 是否刪除匿名用戶,回車 Disallow root login remotely? [Y/n] –是否禁止 root 遠程登錄, 回車,Remove test database and access to it? [Y/n] – 是否刪除 test 數(shù)據(jù)庫,回車 Reload privilege tables now? [Y/n] – 是否重新加載權(quán)限表,回車
[root@ywl0115 ~]# mysql_secure_installationNOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB SERVERS IN PRODUCTION USE! PLEASE READ EACH STEP CAREFULLY!In order to log into MariaDB to secure it, we ll need the currentpassword for the root user. If you ve just installed MariaDB, andyou haven t set the root password yet, the password will be blank,so you should just press enter here.Enter current password for root (enter for none): ERROR 1045 (28000): Access denied for user root @ localhost (using password: YES)Enter current password for root (enter for none): OK, successfully used password, moving on…Setting the root password ensures that nobody can log into the MariaDBroot user without the proper authorisation.You already have a root password set, so you can safely answer n .Change the root password? [Y/n] te^H^HyYou already have a root password set, so you can safely answer n .Change the root password? [Y/n] yNew password: Re-enter new password: Password updated successfully!Reloading privilege tables.. … Success!By default, a MariaDB installation has an anonymous user, allowing anyoneto log into MariaDB without having to have a user account created forthem. This is intended only for testing, and to make the installationgo a bit smoother. You should remove them before moving into aproduction environment.Remove anonymous users? [Y/n] y … Success!Normally, root should only be allowed to connect from localhost . Thisensures that someone cannot guess at the root password from the network.Disallow root login remotely? [Y/n] y … Success!By default, MariaDB comes with a database named test that anyone canaccess. This is also intended only for testing, and should be removedbefore moving into a production environment.Remove test database and access to it? [Y/n] y – Dropping test database… … Success! – Removing privileges on test database… … Success!Reloading the privilege tables will ensure that all changes made so farwill take effect immediately.Reload privilege tables now? [Y/n] y … Success!Cleaning up…All done! If you ve completed all of the above steps, your MariaDBinstallation should now be secure.Thanks for using MariaDB!
注:若后續(xù)想修改安全設(shè)置,使用命令: mysql_secure_installation
(4)、初始化 MariaDB 完成,接下來測試登錄
mysql -uroot -p123456 – 123456 設(shè)的密碼
(5)、配置 MairaDB 的字符集
# 編輯 /etc/my.cnf
vim /etc/my.cnf
配置【my.cnf】前:
# 在 [mysqld] 標簽下添加下面內(nèi)容,沒有自己添加[mysqld]# 默認存儲引擎 default-storage-engine = innodb# 獨立表空間 innodb_file_per_table# 設(shè)置最大連接(用戶)數(shù) max_connections = 4096# 排序規(guī)則 collation-server = utf8_general_ci# 服務(wù)器字符集 character-set-server = utf8[mysqld_safe]log-error=/var/log/mariadb/mariadb.logpid-file=/var/rn/mariadb/mariadb.pid
配置【my.cnf】后
# 編輯 /etc/my.cnf.d/client.cnf(mariadb 沒有這個文件,可不配置)vim /etc/my.cnf.d/client.cnf# 在 [client] 標簽下添加下面內(nèi)容 default-character-set=utf8
# 編輯 /etc/my.cnf.d/mysql-clients.cnfvim /etc/my.cnf.d/mysql-clients.cnf# 在 [mysql] 標簽下添加下面內(nèi)容 default-character-set=utf8
配置完成后 systemctl restart mariadb 重啟服務(wù)。
Show variables like“%character%”;
三、用戶基本操作
(1)、創(chuàng)建用戶命令
create user username@localhost identified by password
(2)、新用戶創(chuàng)建完成,但是此刻如果以此用戶登陸的話,會報錯,因為我們還沒有為這個用戶分配相應(yīng)權(quán)限,分配權(quán)限的命令如下:
# 授予 username 用戶在所有數(shù)據(jù)庫上的所有權(quán)限:GRANT ALL PRIVILEGES ON *.* TO username @ localhost IDENTIFIED BY password #授予 username 只能執(zhí)行 select 和 update 命令:GRANT SELECT, UPDATE ON wordpress.* TO username @ localhost IDENTIFIED BY password 格式: ALL PRIVILEGES:表示所有權(quán)限 *.*:表示所有 數(shù)據(jù)庫 所有表 username @ localhost 表示從本地庫主機登陸的 username 用戶 用戶地址可以是 localhost,也可以是 ip 地址、機器名字、域名。也可以用’% 表示從任何地址連接。 identified by password 表示 username 用戶的密碼 with grant option 表示該用戶可以將這些權(quán)限賦予其它用戶例子: mysql grant all privileges on *.* to root @ % identified by 123456 with grant option;
(3)、如果此時發(fā)現(xiàn)剛剛給的權(quán)限太大了,如果我們只是想授予它在某個數(shù)據(jù)庫上的權(quán)限,那么需要切換到 root 用戶撤銷剛才的權(quán)限,重新授權(quán):
REVOKE ALL PRIVILEGES ON *.* FROM username @ localhost GRANT ALL PRIVILEGES ON wordpress.* TO username @ localhost IDENTIFIED BY password
(4)、每當調(diào)整權(quán)限后,通常需要執(zhí)行以下語句刷新權(quán)限
FLUSH PRIVILEGES;
權(quán)限列表:ALTER: 修改表和索引。CREATE: 創(chuàng)建數(shù)據(jù)庫和表。DELETE: 刪除表中已有的記錄。DROP: 拋棄 (刪除) 數(shù)據(jù)庫和表。INDEX: 創(chuàng)建或拋棄索引。INSERT: 向表中插入新行。REFERENCE: 未用。SELECT: 檢索表中的記錄。UPDATE: 修改現(xiàn)存表記錄。FILE: 讀或?qū)懛?wù)器上的文件。PROCESS: 查看服務(wù)器中執(zhí)行的線程信息或殺死線程。RELOAD: 重載授權(quán)表或清空日志、主機緩存或表緩存。SHUTDOWN: 關(guān)閉服務(wù)器。ALL: 所有權(quán)限,ALL PRIVILEGES 同義詞。USAGE: 特殊的 無權(quán)限 權(quán)限。用 戶賬戶包括 username 和 host 兩部分,后者表示該用戶被允許從何地接入。tom@ % 表示任何地址,默認可以省略。還可以是 tom@192.168.1.%、tom@%.abc.com 等。數(shù)據(jù)庫格式為 db@table,可以是 test.* 或 *.*,前者表示 test 數(shù)據(jù)庫的所有表,后者表示所有數(shù)據(jù)庫的所有表。子句 WITH GRANT OPTION 表示該用戶可以為其他用戶分配權(quán)限。
(5)、遠程工具測試
用第三方客戶端連接成功,MariaDB 安裝成功!
到此,關(guān)于“CentOS7.X 怎么安裝 MariaDB 數(shù)據(jù)庫”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識,請繼續(xù)關(guān)注丸趣 TV 網(wǎng)站,丸趣 TV 小編會繼續(xù)努力為大家?guī)砀鄬嵱玫奈恼拢?/p>