Linux服務器安裝mariadb教程(轉載)

轉自https://www.cnblogs.com/jpfss/p/10128043.html

感謝大佬的分享,找了好多教程,就這個最簡單清楚。

在Linux上安裝及配置MariaDB

安裝MariaDB

1.切換到root用戶,首先執行rpm -qa | grep -i mysql檢查一下是否有已安裝的與MySQL相關的東西,如果有,使用rpm -e --nodeps mysql*進行強制卸載

2.使用yum安裝MariaDB,執行yum -y install mariadb mariadb-server

3.安裝完成後,執行systemctl start mariadb 啓動MariaDB,執行systemctl enable mariadb設置開機啓動

配置MariaDB

1.執行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] <– 是否重新加載權限表,回車

2.配置完成後,執行mysql -uroot -ppassword測試登錄。其中root爲要登錄的用戶名,password爲剛纔設置的root用戶的密碼

3.測試成功後,配置MariaDB的字符集 
- 使用vi編輯器打開/etc/my.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
  • 使用vi編輯器打開/etc/my.cnf.d/client.cnf,在[client]中添加 
    default-character-set=utf8
  • 使用vi編輯器打開/etc/my.cnf.d/mysql-clients.cnf,在[mysql]中添加 
    default-character-set=utf8
  • 全部保存後,進入到MariaDB控制檯,查看字符集 
    show variables like "%character%";show variables like "%collation%"; 
    全部顯示UTF-8則配置成功

配置MariaDB遠程連接

進入到MariaDB控制檯 
1.執行如下語句建立用戶並賦予所有操作權限。 
CREATE USER 'username'@'host' IDENTIFIED BY 'password';

參數 說明
username 將要創建的用戶名
host 指定該用戶在哪個主機上可以登陸,如果是本地用戶可用localhost,如果想讓該用戶可以從任意遠程主機登陸,可以使用通配符%
password 該用戶的登陸密碼,密碼可以爲空,如果爲空則該用戶可以不需要密碼登陸服務器

2.給用戶賦予遠程登錄權限 
GRANT privileges ON databasename.tablename TO 'username'@'host'

參數 說明
privileges 用戶的操作權限,如SELECT , INSERT , UPDATE 等(權限列表見文末)。如果要授予所的權限則使用ALL
databasename 數據庫名
tablename 表名,如果要授予該用戶對所有數據庫和表的相應操作權限則可用表示,如.*

3.修改完成後在MariaDB控制檯執行FLUSH PRIVILEGES刷新配置權限使其生效

此時即可通過ip遠程訪問主機上的MariaDB了。

若仍不能訪問,可進行以下檢查: 
1.查看/etc/my.cnf,如skip-networkingbind-address(或bindaddress)被配置,則需要將這兩個參數註釋掉。

skip-networking 這個參數,會導致所有TCP/IP端口沒有被監聽,也就是說除了本機,其他客戶端都無法用網絡連接到本MariaDB服務器。 
而bind-address這個參數是指定哪些ip地址被配置,使得MariaDB服務器只回應哪些ip地址的請求

2.如果仍然不能訪問,則有可能是防火牆的原因。在shell下執行/etc/init.d/iptables  stop關閉防火牆。

附:MariaDB操作權限

權限 描述
ALTER Allows use of ALTER TABLE
ALTER ROUTINE Alters or drops stored routines
CREATE Allows user of CREATE TABLE
CREATE ROUTINE Creates stored routines
CREATE TEMPORARY TABLE Allows user of CREATE TEMPORARY TABLE
CREATE USER Allows use ofCREATE USER,DROP USER,RENAME USER, and REVOKE ALL PRIVILEGES
CREATE VIEW Allows use of CREATE VIEW
DELETE Allows use of DELETE
DROP Allows use of DROP TABLE
EXECUTE Allows the user to run stored routines
FILE Allows use of SELECT...INTO OUTFILE and LOAD DATA INFILE
INDEX Allows use of CREATE INDEX and DROP INDEX
INSERT Allows use of INSERT
LOCK TABLES Allows use of LOCK TABLES on tables for which the user also has SELECT privileges
PROCESS Allows use of `SHOW FULL PROCESSLIST
RELOAD Allows use of FLUSH
REPLICATION Allows the user to ask where slave or master
CLIENT servers are
REPLICATION SLAVE Needed for replication slaves
SELECT Allows use of SELECT
SHOW DATABASES Allows use of SHOW DATABASE
SHOW VIEW Allows use of SHOW CREATE VIEW
SHUTDOWN Allows use of mysqladmin shutdown
SUPER Allows use of CHANGE MASTER,KILL,PURGE MASTER LOGS,andSET GLOBAL SQL statements. Allowsmysqladmin debug command.Allows one extra connection to be made if maximum connections are reached.
UPDATE Allows use of UPDATE
USAGE Allows connection without any specific privileges
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章