centos安裝配置mariadb

CentOS7下使用yum安裝MariaDB

CentOS 6 或早期的版本中提供的是 MySQL 的服務器/客戶端安裝包,但 CentOS 7 已使用了 MariaDB 替代了默認的 MySQLMariaDB數據庫管理系統是MySQL的一個分支,主要由開源社區在維護,採用GPL受權許可 MariaDB的目的是徹底兼容MySQL,包括API和命令行,使之能輕鬆成爲MySQL的代替品。html

Linux下安裝MariaDB官方文檔參見:https://mariadb.com/kb/zh-cn/installing-mariadb-with-yum/node

所有刪除MySQL/MariaDBmysql

MySQL 已經再也不包含在 CentOS 7 的源中,而改用了 MariaDB;linux

1.使用rpm -qa | grep mariadb搜索 MariaDB 現有的包:sql

若是存在,使用rpm -e --nodeps mariadb-*所有刪除:數據庫

[root@localhost ~]# rpm -qa | grep mariadbcentos

mariadb-server-5.5.52-1.el7.x86_64服務器

mariadb-libs-5.5.52-1.el7.x86_64架構

[root@localhost ~]# rpm -e mysql-*dom

錯誤:未安裝軟件包 mysql-*

 

2.使用rpm -qa | grep mariadb搜索 MariaDB 現有的包:

若是存在,使用yum remove mysql mysql-server mysql-libs compat-mysql51所有刪除;

 

[root@localhost ~]# yum remove mysql mysql-server mysql-libs compat-mysql51

已加載插件:fastestmirror, langpacks

參數 mysql 沒有匹配

參數 mysql-server 沒有匹配

參數 compat-mysql51 沒有匹配

正在解決依賴關係--> 正在檢查事務---> 軟件包 mariadb-libs.x86_64.1.5.5.52-1.el7 將被 刪除--> 正在處理依賴關係 libmysqlclient.so.18()(64bit),它被軟件包 perl-DBD-MySQL-4.023-5.el7.x86_64 須要--> 正在處理依賴關係 libmysqlclient.so.18()(64bit),它被軟件包 2:postfix-2.10.1-6.el7.x86_64 須要--> 正在處理依賴關係 libmysqlclient.so.18()(64bit),它被軟件包 1:qt-mysql-4.8.5-13.el7.x86_64 須要..........

 

 

刪除:

  mariadb-libs.x86_64 1:5.5.52-1.el7                                            

 

做爲依賴被刪除:

  akonadi-mysql.x86_64 0:1.9.2-4.el7     mariadb-server.x86_64 1:5.5.52-1.el7   

  perl-DBD-MySQL.x86_64 0:4.023-5.el7    postfix.x86_64 2:2.10.1-6.el7          

  qt-mysql.x86_64 1:4.8.5-13.el7        

 

完畢!

[root@localhost ~]# rpm -qa|grep mariadb

[root@localhost ~]#

 

3.開始新的安裝, 建立MariaDB.repo文件

vi /etc/yum.repos.d/MariaDB.repo

插入如下內容:

 

# MariaDB 10.2.4 CentOS repository list - created 2017-05-05 16:13 UTC

# http://downloads.mariadb.org/mariadb/repositories/

[mariadb]

name = MariaDB

baseurl = http://yum.mariadb.org/10.2.4/centos7-amd64

gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB

gpgcheck=1

 

系統及版本選擇:https://downloads.mariadb.org/mariadb/repositories/#mirror=tuna

4.運行安裝命令安裝MariaDB

 

[root@localhost ~]# yum -y install MariaDB-server MariaDB-client

已加載插件:fastestmirror, langpacks

mariadb                                                  | 2.9 kB     00:00     

mariadb/primary_db                                         |  18 kB   00:09     

Determining fastest mirrors

 * base: mirrors.btte.net

 * extras: mirrors.zju.edu.cn

 * updates: mirrors.zju.edu.cn

正在解決依賴關係--> 正在檢查事務---> 軟件包 MariaDB-client.x86_64.0.10.2.4-1.el7.centos 將被 安裝--> 正在處理依賴關係 MariaDB-common,它被軟件包 MariaDB-client-10.2.4-1.el7.centos.x86_64 須要---> 軟件包 MariaDB-server.x86_64.0.10.2.4-1.el7.centos 將被 安裝--> 正在處理依賴關係 galera,它被軟件包 MariaDB-server-10.2.4-1.el7.centos.x86_64 須要--> 正在處理依賴關係 libjemalloc.so.1()(64bit),它被軟件包 MariaDB-server-10.2.4-1.el7.centos.x86_64 須要--> 正在檢查事務---> 軟件包 MariaDB-common.x86_64.0.10.2.4-1.el7.centos 將被 安裝--> 正在處理依賴關係 MariaDB-compat,它被軟件包 MariaDB-common-10.2.4-1.el7.centos.x86_64 須要---> 軟件包 galera.x86_64.0.25.3.19-1.rhel7.el7.centos 將被 安裝---> 軟件包 jemalloc.x86_64.0.3.6.0-1.el7 將被 安裝--> 正在檢查事務---> 軟件包 MariaDB-compat.x86_64.0.10.2.4-1.el7.centos 將被 安裝--> 解決依賴關係完成

 

依賴關係解決

================================================================================

 Package            架構       版本                           源           大小================================================================================

正在安裝:

 MariaDB-client     x86_64     10.2.4-1.el7.centos            mariadb      48 M

 MariaDB-server     x86_64     10.2.4-1.el7.centos            mariadb     113 M

爲依賴而安裝:

 MariaDB-common     x86_64     10.2.4-1.el7.centos            mariadb     104 k

 MariaDB-compat     x86_64     10.2.4-1.el7.centos            mariadb     2.8 M

 galera             x86_64     25.3.19-1.rhel7.el7.centos     mariadb     8.0 M

 jemalloc           x86_64     3.6.0-1.el7                    mariadb     104 k

 

事務概要================================================================================

安裝  2 軟件包 (+4 依賴軟件包)

....

 

 

首先下載安裝包,而後進行自動安裝,安裝成功以後啓動MariaDB服務。

systemctl start mariadb #啓動服務

systemctl enable mariadb #設置開機啓動

systemctl restart mariadb #從新啓動

systemctl stop mariadb.service #中止MariaDB

5.登陸到數據庫

  用mysql -uroot命令登陸到MariaDB,此時root帳戶的密碼爲空。

6.進行MariaDB的相關簡單配置,使用mysql_secure_installation命令進行配置。

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完成,接下來測試登陸

mysql -uroot -ppassword

完成。

 7.配置MariaDB的字符集

  查看/etc/my.cnf文件內容,其中包含一句!includedir /etc/my.cnf.d 說明在該配置文件中引入/etc/my.cnf.d 目錄下的配置文件。

  1)使用vi 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

 

  若是/etc/my.cnf.d 目錄下無server.cnf文件,則直接在/etc/my.cnf文件的[mysqld]標籤下添加以上內容。

2)文件/etc/my.cnf.d/client.cnf

vi /etc/my.cnf.d/client.cnf

[client]中添加

default-character-set=utf8

3)文件/etc/my.cnf.d/mysql-clients.cnf

vi /etc/my.cnf.d/mysql-clients.cnf

[mysql]中添加

default-character-set=utf8

 所有配置完成,重啓mariadb

systemctl restart mariadb

以後進入MariaDB查看字符集

mysql> show variables like "%character%";show variables like "%collation%";

顯示爲

 

 

+--------------------------+----------------------------+

| 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.00 sec)

+----------------------+-----------------+

| Variable_name        | Value          |

+----------------------+-----------------+

| collation_connection | utf8_unicode_ci |

| collation_database  | utf8_unicode_ci |

| collation_server    | utf8_unicode_ci |

+----------------------+-----------------+

3 rows in set (0.00 sec)

 

字符集配置完成。

8. 添加用戶,設置權限

建立用戶命令

mysql>create user username@localhost identified by 'password';

直接建立用戶並受權的命令

mysql>grant all on *.* to username@localhost indentified by 'password';

授予外網登錄權限 

mysql>grant all privileges on *.* to username@'%' identified by 'password';

授予權限而且能夠受權

mysql>grant all privileges on *.* to username@'hostname' identified by 'password' with grant option;

 

MariaDB [mysql]> select host,user,password from user;+-----------------------+-------+------------------------+

| host                  | user  | password               |

+-----------------------+-------+------------------------+

| localhost             | root  | *E87F9354F7E889A65E... |

| localhost.localdomain | root  | *E87F9354F7E889A65E... |

| 127.0.0.1             | root  | *E87F9354F7E889A65E... |

| ::1                   | root  | *E87F9354F7E889A65E... |

| localhost             |       |                        |

| localhost.localdomain |       |                        |

+-----------------------+-------+------------------------+

7 rows in set (0.00 sec)

 

 查詢各SchemaTable佔用的空間:

 

MariaDB [information_schema]> use information_schema;

MariaDB [information_schema]> select table_schema,round(sum(DATA_LENGTH/1024/1024),2) as datasize  from tables group by table_schema;+--------------------+----------+

| table_schema       | datasize |

+--------------------+----------+

| common             |     0.05 |

| information_schema |     0.09 |

| mysql              |     9.11 |

| nemo               |   103.23 |

| river              |     3.78 |

+--------------------+----------+

5 rows in set (2.026 sec)

MariaDB [information_schema]> select table_name,concat(round(sum(data_length/1024/1024),2),'MB') as datasize from tables where table_schema='nemo' group by table_name;
+---------------------------------+----------+ 
| table_name | datasize | 
+---------------------------------+----------+
| actions             | 0.05MB | 
| addresses            | 0.02MB | 
| addressfieldattributes    | 0.02MB | 
| addressprops          | 0.02MB |
| composedtypes          | 1.52MB |
| composedtypeslp        | 1.52MB |
| comptypegrp2comptype      | 0.05MB |
| config             | 0.02MB |
| itemcockpittemplrels      | 0.02MB |
| itemsynctimestamps        | 2.52MB |
| keyfeature            | 0.02MB |
| keyfeaturelp           | 0.02MB |
| keyvaluemap           | 2.52MB |
| keywords             | 0.02MB |
| keywordsuggestionrule     | 0.02MB |
+---------------------------------+----------+
15 rows in set (0.544 sec)

 

 

 

簡單的用戶和權限配置基本就這樣了。

其中只授予部分權限把 其中 all privileges或者all改成select,insert,update,delete,create,drop,index,alter,grant,references,reload,shutdown,process,file其中一部分。

Linux系統教程:如何檢查MariaDB服務端版本  http://www.linuxidc.com/Linux/2015-08/122382.htm

MariaDB Proxy讀寫分離的實現 http://www.linuxidc.com/Linux/2014-05/101306.htm

Linux下編譯安裝配置MariaDB數據庫的方法 http://www.linuxidc.com/Linux/2014-11/109049.htm

CentOS系統使用yum安裝MariaDB數據庫 http://www.linuxidc.com/Linux/2014-11/109048.htm

安裝MariaDBMySQL並存 http://www.linuxidc.com/Linux/2014-11/109047.htm

 

忘記root用戶名和密碼

首先用 killall -TERM mysqld  mysqld server 發送kill命令關掉mysqld server(不是 kill -9),你必須是UNIXroot用戶或者是你所運行的SERVER上的同等用戶,才能執行這個操做

而後  /usr/bin/mysqld_safe  --skip-grant-tables --skip-networking & 

登陸 : mysql -p或者使用mysql無密碼登陸 

>use mysql 
>update user set password=password("new_pass") where user="root"; 

>flush privileges;

exit;

修改完成以後重啓數據庫,便可用修改好 root 密碼登陸 .

相關文章
相關標籤/搜索