添加MariaDB的yum源,指定安裝的版本,然后使用 yum 工具進行安裝
參考MariaDB官方網站對應安裝方法和步驟
[root@SERVER1 ~]# cd /etc/yum.repos.d/ [root@SERVER1 yum.repos.d]# ls #<==添加MariaDB.repo文件,並添加內容 CentOS7-ctyun.repo CentOS-MariaDB.repo [root@SERVER1 yum.repos.d]# cat CentOS-MariaDB.repo # MariaDB 10.3 CentOS repository list - created 2019-01-11 03:14 UTC # http://downloads.mariadb.org/mariadb/repositories/ [mariadb] name = MariaDB baseurl = http://yum.mariadb.org/10.3/centos7-amd64 gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB gpgcheck=1
yum自動下載並安裝MariaDB,如果此步出錯,請查看錯誤提示,檢查 /etc/yum.repo.d/ 下文件是否配置正確,並檢查 /etc/resolv.conf 中DNS是否配置
[root@SERVER1 yum.repos.d]# yum install -y MariaDB-server MariaDB-client
安裝完成MariaDB后,啟動MariaDB服務並設置為開機自啟動
[root@SERVER1 ~]# systemctl start mariadb [root@SERVER1 ~]# systemctl enable mariadb [root@SERVER1 yum.repos.d]# ss -lntup|grep mysql tcp LISTEN 0 80 :::3306 :::* users:(("mysqld",pid=2560,fd=21))
嘗試登錄看看,確保安裝正常
[root@SERVER1 ~]# mysql -uroot Welcome to the MariaDB monitor. Commands end with ; or \g. Your MariaDB connection id is 8 Server version: 10.3.12-MariaDB MariaDB Server Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. MariaDB [(none)]> select version(); +-----------------+ | version() | +-----------------+ | 10.3.12-MariaDB | +-----------------+ 1 row in set (0.000 sec) MariaDB [(none)]> \q Bye
為確保數據庫的安全性,必須進行一些簡單的初始化工作
1)設定root用戶密碼。 2)刪除匿名帳號。 3)禁止root用戶從遠程登錄。 4)刪除test數據庫並取消對其的訪問權限。 5)刷新授權表,讓初始化后的設定立即生效。
6)刪除無用的用戶。
[root@SERVER1 ~]# mysql_secure_installation Set root password? [Y/n] Y <===root用戶建立密碼 New password: Re-enter new password: Password updated successfully! Reloading privilege tables.. ... Success! Remove anonymous users? [Y/n] Y <===移除匿名用戶 ... Success! Disallow root login remotely? [Y/n] Y <===禁止root用戶遠程鏈接 ... Success! Remove test database and access to it? [Y/n] Y <===移除test庫及其表 - Dropping test database... ... Success! - Removing privileges on test database... ... Success! Reload privilege tables now? [Y/n] Y <===刷新權限 ... Success!
[root@SERVER1 ~]# mysql -uroot -p Enter password: Welcome to the MariaDB monitor. Commands end with ; or \g. Your MariaDB connection id is 8 Server version: 10.3.12-MariaDB MariaDB Server Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. MariaDB [(none)]> select user,host from mysql.user; +------+-----------+ | user | host | +------+-----------+ | root | 127.0.0.1 | | root | ::1 | | root | localhost | | root | server1 | +------+-----------+ 4 rows in set (0.000 sec) MariaDB [(none)]> drop user 'root'@'server1'; Query OK, 0 rows affected (0.000 sec) MariaDB [(none)]> drop user 'root'@'::1'; Query OK, 0 rows affected (0.000 sec) MariaDB [(none)]> flush privileges; Query OK, 0 rows affected (0.001 sec) MariaDB [(none)]> select user,host from mysql.user; +------+-----------+ | user | host | +------+-----------+ | root | 127.0.0.1 | | root | localhost | +------+-----------+ 2 rows in set (0.000 sec) MariaDB [(none)]> \q Bye
修改MariaDB字符集為utf8
[root@SERVER1 ~]# cat /etc/my.cnf # # This group is read both both by the client and the server # use it for options that affect everything # [client-server] # # include all files from the config directory # !includedir /etc/my.cnf.d [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 [root@SERVER1 ~]# cat /etc/my.cnf.d/mysql-clients.cnf # # These groups are read by MariaDB command-line tools # Use it for options that affect only one utility # [mysql] default-character-set=utf8 [root@SERVER1 my.cnf.d]# systemctl restart mariadb.service #<===重啟MariaDB服務 [root@SERVER1 my.cnf.d]# mysql -uroot -p Enter password: Welcome to the MariaDB monitor. Commands end with ; or \g. Your MariaDB connection id is 8 Server version: 10.3.12-MariaDB MariaDB Server Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. MariaDB [(none)]> show variables like "%character%"; +--------------------------+----------------------------+ | Variable_name | Value | +--------------------------+----------------------------+ | character_set_client | utf8 | | character_set_connection | utf8 | | character_set_database | utf8 | | character_set_filesystem | binary | | character_set_results | utf8 | | character_set_server | utf8 | | character_set_system | utf8 | | character_sets_dir | /usr/share/mysql/charsets/ | +--------------------------+----------------------------+ 8 rows in set (0.001 sec) MariaDB [(none)]> show variables like "%collation%"; +----------------------+-----------------+ | Variable_name | Value | +----------------------+-----------------+ | collation_connection | utf8_unicode_ci | | collation_database | utf8_unicode_ci | | collation_server | utf8_unicode_ci | +----------------------+-----------------+ 3 rows in set (0.001 sec)
注:
/etc/my.cnf 是由mariadb-libs生成的 /etc/my.cnf.d/server.cnf是由 mariadb-server生成 /etc/my.cnf.d/client.cnf是由mariadb生成