Linux 環境下快速部署 MySQL 的替代方案
原創【51CTO專稿】一 MySQL 代替者MariaDB 簡介
MariaDB名稱來自Michael Widenius的女兒Maria的名字。圖1 是MariaDB 的LOGO:
圖1 MariaDB 的LOGO
MariaDB 下載:https://downloads.mariadb.org/
MariaDB 網站:http://www.mariadb.org/
MariaDB***穩定版為:MariaDB 5.5。上一個穩定版為:MariaDB 5.3。
MariaDB 是一個采用 Maria 存儲引擎的 MySQL 分支版本,與 MySQL 相比較,MariaDB 更強的地方在于,二者支持的不同的引擎。通??梢酝ㄟ^show engines 命令來查看兩種數據庫服務器 支持的不同的引擎。 Support列的信息包括YES,NO 和 DEFAULT。
圖2 和圖3 分別是MySQL 5.5 和MariaDB 5.5 引擎列表:
圖2 MySQL 5.5引擎列表
圖3 MariaDB 5.5 引擎列表
對比二者 Sphinx全文搜索引擎是目前當前市場上最炙手可熱的開源搜索引擎,MariaDB利用SphinxSE作為存儲引擎。另外MariaDB基于事務的Maria存儲引擎,替換了MySQL的MyISAM存儲引擎,它使用了Percona的 XtraDB,是InnoDB的變體。MariaDB默認的存儲引擎是Aria,不是MyISAM。Aria可以支持事務,但是默認情況下沒有打開事務支持,因為事務支持對性能會有影響。PBXT 是 MariaDB 附帶的一種存儲引擎,PBXT 在 MariaDB 的 5.1/5.2/5.3 版本中存在,但從 MariaDB 5.5 開始就不再提供 PBXT 存儲引擎,而且以后也將不再提供。另外MariaDB已經宣布了Cassandra存儲引擎的一個預覽版本。該插件允許MariaDB通過標準SQL語法使用Cassandra集群。
MariaDB跟MySQL在絕大多數方面是兼容的,對于開發者來說,幾乎感覺不到任何不同。目前MariaDB是發展最快的MySQL分支版本 。#p#
二 主要 Linux 發行版本安裝MariaDB
本文主要介紹兩大主要Linux 發行版本類別:
- 使用rpm 軟件包格式的RHEL/CentOS/Fedora
- 使用deb軟件包格式的Debian /Ubuntu 。
1、使用rpm 軟件包格式的RHEL/CentOS/Fedora
(1)這里以Fedora 19為例
其中使用Fedora 19 是最簡單的,因為這個***Linux 發行版本可以直接使用yum 軟件包工具安裝
a、安裝軟件包
- #yum -y install mariadb-server mariadb
- #systemctl start mysqld.service
- #systemctl enable mysqld.service
- ln -s '/lib/systemd/system/mysqld.service' '/etc/systemd/system/multi-user.target.wants/mysqld.service'
2、數據庫的基本操作
***連接MariaDB如圖4:
- #mysql -u root
圖4***連接MariaDB
可以看到mariadb 版本號是5.5.31-MariaDB MariaDB Server,其他基本操作(和Mysql操作相同)。
查看用戶信息
使用內部命令:select user,host,password from mysql.user; 如圖5:
圖5查看用戶信息
設置root用戶密碼
- MariaDB [(none)]> set password for root@localhost=password('password');
- Query OK, 0 rows affected (0.00 sec)
- # set root password
- MariaDB [(none)]> set password for root@'127.0.0.1'=password('password');
- Query OK, 0 rows affected (0.00 sec)
刪除一些數據庫用戶(ipv6 和 匿名用戶)
- MariaDB [(none)]> delete from mysql.user where user='root' and host='::1';
- Query OK, 1 rows affected (0.00 sec)
- MariaDB [(none)]> delete from mysql.user where user='';
- Query OK, 2 rows affected (0.00 sec)
退出后使用root密碼重新登錄
- #mysql -u root -p
- Enter password:
- # MariaDB root password you set
- Welcome to the MariaDB monitor. Commands end with ; or \g.
- Your MariaDB connection id is 3
- Server version: 5.5.31-MariaDB MariaDB Server
- Copyright (c) 2000, 2013, Oracle, Monty Program Ab and others.
- Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
- MariaDB [(none)]>
#p#
3、安裝MariaDB客戶端工具
MariaDB的API和協議兼容MySQL,另外又添加了一些功能,以支持本地的非阻塞操作和進度報告。這意味著,所有使用MySQL的連接器、庫和應用程序也將會在MariaDB下工作。如下是支持MariaDB的工具客戶端:
- DBEdit 一個免費的MariaDB數據庫和其他數據庫管理應用程序。
- Navicat 一系列Windows、Mac OS X、Linux下專有數據庫管理應用程序。
- HeidiSQL 一個Windows上自由和開放源碼的MySQL客戶端。它支持MariaDB的5.2.7版本和以后的版本。
- phpMyAdmin 一個基于網絡的MySQL數據庫管理應用程序 。
下面介紹phpMyAdmin ,安裝使用phpMyAdmin要配置完成Apache 和 php 相關軟件包:
- # yum -y install httpd php php-mbstring php-pear
- # yum -y install phpMyAdmin php-mysql php-mcrypt
修改配置文件添加ip地址范圍:
- vi /etc/httpd/conf.d/phpMyAdmin.conf
- # line 15: add IP address you permit
- Require ip 127.0.0.1 10.0.0.0/24
- # line 32: add IP address you permit
- Require ip 127.0.0.1 10.0.0.0/24
- #systemctl restart httpd.service
然后使用瀏覽器訪問即可,如圖6:
圖6 phpMyAdmin管理mariadb數據庫
(2)其他使用rpm軟件包的發行版本
添加文件:/etc/yum.repos.d/MariaDB.repo
CentOS 6 64位發行版本的/etc/yum.repos.d/MariaDB.repo文件內容:
- [mariadb]
- name = MariaDB
- baseurl = http://yum.mariadb.org/5.5/centos6-amd64
- gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB
- gpgcheck=1
安裝命令
- #yum -y install MariaDB-server MariaDB-client
- # service mysql start
- # chkconfig mysql on
(3)其他版本安裝mariadb(以Ubuntu 12.04 為例子)
首先從 MariaDB 下載頁面 選擇貼近你的版本的資料庫鏡像,然后下載頁面會在底部顯示鏡像信息,將這些信息添加到 /etc/apt/source.list
- deb http://ftp.heanet.ie/mirrors/mariadb/repo/5.5/ubuntu lucid main
- deb-src http://ftp.heanet.ie/mirrors/mariadb/repo/5.5/ubuntu lucid main
2. 接下來需要導入簽名密鑰:
- # apt-key adv --recv-keys --keyserver keyserver.ubuntu.com 0xcbcb082a1bb943db
3. 更新
- #apt-get update
4. 安裝
- #apt-get install mariadb-server-5.5
安裝過程中要輸入mariadb的root密碼并且確認一次如圖7:
圖7 輸入mariadb的root密碼并且確認一次
#p#
三 Mariadb的root密碼的重新設置
首先停止數據庫服務器進程:
- # service mysql stop
安全模式啟動:
- # mysqld_safe --skip-grant-tables &
登錄MariaDb server:
- # mysql -u root
- Welcome to the MariaDB monitor. Commands end with ; or \g.
- Your MariaDB connection id is 1
- Server version: 5.5.32-MariaDB MariaDB Server
- Copyright (c) 2000, 2013, Oracle, Monty Program Ab and others.
- Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
設置密碼:
- MariaDB [(none)]> update mysql.user set password=PASSWORD("newpassword") where User='root';
- Query OK, 4 rows affected (0.00 sec)
- Rows matched: 4 Changed: 4 Warnings: 0
授權后退出:
- MariaDB [(none)]> flush privileges;
- Query OK, 0 rows affected (0.00 sec)
- MariaDB [(none)]> exit;
- Bye
重新啟動數據庫進程:
- # service mysql restart
使用新密碼登錄:
- # mysql -u root -p
- Enter password:
- Welcome to the MariaDB monitor. Commands end with ; or \g.
- Your MariaDB connection id is 1
- Server version: 5.5.32-MariaDB MariaDB Server
- Copyright (c) 2000, 2013, Oracle, Monty Program Ab and others.
- Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
總結:
MariaDB 基本上名門之后,加上MySQL創始人Monty的實力和號召力,是作為MySQL一個非常好的替代品,前途發展無限,值得我們嘗試使用。二者的常用工具,連接程序都可以如常運作。你也不需要導出和匯入數據。格式與文件名都是相同的。