一.mariadb的安裝
MariaDB數據庫管理系統是MySQL的一個分支,主要由開源社區在維護,采用GPL授權許可。
開發這個分支的原因之一是:甲骨文公司收購了MySQL后,有將MySQL閉源的潛在風險,因此社區采用分支的方式來避開這個風險。
MariaDB的目的是完全兼容MySQL,包括API和命令行,使之能輕松成為MySQL的代替品。
方法1:阿里源下yum安裝mariadb版本會舊一點
Red Hat Enterprise Linux/CentOS 7.0 發行版已將默認的數據庫從 MySQL 切換到MariaDB。
安裝命令 # yum -y install mariadb mariadb-server 安裝完成MariaDB,首先啟動MariaDB,兩條命令都可以 systemctl start mariadb #centos6命令 service mariadb start
方法2:yum安裝mariadbrepo倉庫配置安裝
# 如果已經添加了阿里雲的源又想安裝最新版本的mariadb,那么少就使用以下步驟
# 編輯創建mariadb.repo倉庫文件 vi /etc/yum.repos.d/MariaDB.repo 添加repo倉庫配置 [mariadb] name=MariaDB baseurl=http://yum.mariadb.org/10.1/centos7-amd64 gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB gpgcheck=1
當 MariaDB 倉庫地址添加好后,你可以通過下面的一行命令輕松安裝 MariaDB。
yum install MariaDB-server MariaDB-client -y
二.MariaDB基本配置
1.開啟關閉與查看mariadb命令
mariadb數據庫的相關命令是:
systemctl start mariadb #啟動MariaDB
systemctl stop mariadb #停止MariaDB
systemctl restart mariadb #重啟MariaDB
systemctl enable mariadb #設置開機啟動
# 查看mariadb進程
[root@node home]# netstat -ntlp |grep 3306 tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 3931/mysqld
2.初始化MariaDB 數據庫
在確認 MariaDB 數據庫軟件程序安裝完畢並成功啟動后請不要立即使用。為了確保數據 庫的安全性和正常運轉,需要先對數據庫程序進行初始化操作。這個初始化操作涉及下面 5 個 步驟。
設置 root 管理員在數據庫中的密碼值(注意,該密碼並非 root 管理員在系統中的密 碼,這里的密碼值默認應該為空,可直接按回車鍵)。
設置 root 管理員在數據庫中的專有密碼。
隨后刪除匿名賬戶,並使用 root 管理員從遠程登錄數據庫,以確保數據庫上運行的業
務的安全性。
刪除默認的測試數據庫,取消測試數據庫的一系列訪問權限。
刷新授權列表,讓初始化的設定立即生效。
初始化命令: mysql_secure_installation 首先是設置密碼,會提示先輸入密碼 Enter current password for root (enter for none):<–初次運行直接回車 設置密碼 Set root password? [Y/n] y<– 是否設置root用戶密碼,輸入y並回車或直接回車 New password: <– 設置root用戶的密碼 Re-enter new password: <– 再輸入一次你設置的密碼 其他配置 Remove anonymous users? [Y/n] y<– 是否刪除匿名用戶,回車 Disallow root login remotely? [Y/n] n<–是否禁止root遠程登錄,回車,不過一般為y Remove test database and access to it? [Y/n] y<– 是否刪除test數據庫,回車 Reload privilege tables now? [Y/n] y<– 是否重新加載權限表,回車 初始化MariaDB完成,接下來測試登錄 # mysql -uroot -p 進入數據庫(數據庫中的操作命令和mysql是一樣的)
3.中文編碼設置,utf8編碼
文件/etc/my.cnf 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 進行查看就支持utf8了
三.mariadb的使用
關於mariadb的使用其實是和mysql的語句完全是一樣的
1.開啟關閉和設置開機啟動
mariadb數據庫的相關命令是:
systemctl start mariadb #啟動MariaDB
systemctl stop mariadb #停止MariaDB
systemctl restart mariadb #重啟MariaDB
systemctl enable mariadb #設置開機啟動
2.設置密碼建庫建表
# 修改mysql密碼 MariaDB [(none)]> set password = PASSWORD('hsz123'); Query OK, 0 rows affected (0.34 sec) # 創建tests數據庫 如果加charset=utf8 表示指定utf8編碼 MariaDB [(none)]> create database test; Query OK, 1 row affected (0.00 sec) # 進入test數據庫 MariaDB [(none)]> use test; Database changed # 創建mytest數據表 MariaDB [test]> create table mytest(id int,name char(32)); Query OK, 0 rows affected (0.02 sec) # 查看數據表 MariaDB [test]> show tables; +----------------+ | Tables_in_test | +----------------+ | mytest | +----------------+ 1 row in set (0.00 sec) # 查看mytest數據表的表結構 MariaDB [test]> desc mytest; +-------+----------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+----------+------+-----+---------+-------+ | id | int(11) | YES | | NULL | | | name | char(32) | YES | | NULL | | +-------+----------+------+-----+---------+-------+ 2 rows in set (0.05 sec)
2.簡單的增刪改查
# 給表增加兩條數據 MariaDB [test]> insert into mytest(id,name) values(1,"zero"),(2,"one"); Query OK, 2 rows affected (0.35 sec) Records: 2 Duplicates: 0 Warnings: 0 # 查看id,name 字段mytest的數據 MariaDB [test]> select id,name from mytest; +------+------+ | id | name | +------+------+ | 1 | zero | | 2 | one | +------+------+ 2 rows in set (0.00 sec) # 刪除mytest表中id=3 的數據 MariaDB [test]> delete from mytest id=2; ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'id=2' at line 1 MariaDB [test]> delete from mytest where id=2; Query OK, 1 row affected (0.00 sec) # 查看表的所有數據 MariaDB [test]> select * from mytest; +------+------+ | id | name | +------+------+ | 1 | zero | +------+------+ 1 row in set (0.01 sec) # 更新表id=1表的字段name=ten MariaDB [test]> update mytest set name=ten where id=1; ERROR 1054 (42S22): Unknown column 'ten' in 'field list' MariaDB [test]> update mytest set name="ten" where id=1; Query OK, 1 row affected (0.00 sec) Rows matched: 1 Changed: 1 Warnings: 0 MariaDB [test]> select * from mytest; +------+------+ | id | name | +------+------+ | 1 | ten | +------+------+ 1 row in set (0.01 sec)
3.關於用戶及權限常用命令
# 創建用戶和密碼 MariaDB [test]> create user zero@'%' identified by 'zero'; Query OK, 0 rows affected (0.01 sec) mysql使用grant命令對賬戶進行授權,grant命令常見格式如下 grant 權限 on 數據庫.表名 to 賬戶@主機名 對特定數據庫中的特定表授權 grant 權限 on 數據庫.* to 賬戶@主機名 對特定數據庫中的所有表給與授權 grant 權限1,權限2,權限3 on *.* to 賬戶@主機名 對所有庫中的所有表給與多個授權 grant all privileges on *.* to 賬戶@主機名 對所有庫和所有表授權所有權限 #授予用戶最大的權限,所有的權限 grant all privileges on *.* to username@'%' identified by 'password';
#授予zero用戶,只有創建test數據庫的權限 MariaDB [test]> grant create on test.* to zero@'%' identified by 'zero'; Query OK, 0 rows affected (0.00 sec) # 所以查詢zero用戶的數據庫只有如下所示 [root@node ~]# mysql -uzero -pzero Welcome to the MariaDB monitor. Commands end with ; or \g. Your MariaDB connection id is 6 Server version: 5.5.60-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 databases; +--------------------+ | Database | +--------------------+ | information_schema | | test | +--------------------+ 2 rows in set (0.00 sec) MariaDB [(none)]>
#授予one創建的權限,對於所有的庫表生效 MariaDB [test]> grant create on *.* to one@"%" identified by 'one'; Query OK, 0 rows affected (0.00 sec) # 所以查詢數據庫可以顯示如下所示 [root@node ~]# mysql -uone -pone Welcome to the MariaDB monitor. Commands end with ; or \g. Your MariaDB connection id is 7 Server version: 5.5.60-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 databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | test | | text | +--------------------+ 5 rows in set (0.00 sec) MariaDB [(none)]> # 刪除one用戶 MariaDB [test]> drop user one; Query OK, 0 rows affected (0.00 sec) # 刷新權限 MariaDB [test]> flush privileges; Query OK, 0 rows affected (0.00 sec)
4.數據庫備份與恢復
# mysqldump命令用於備份數據庫數據 ##備份所有數據庫命令 [root@node ~]# mysqldump -u root -p --all-databases > /tmp/db.dump Enter password: [root@node ~]# ll /tmp/db.dump -rw-r--r--. 1 root root 515562 Sep 13 23:00 /tmp/db.dump ##備份單個數據庫命令 [root@node ~]# mysqldump -u root -p text > /tmp/text.sql Enter password: [root@node ~]# ll /tmp/text.sql -rw-r--r--. 1 root root 1261 Sep 13 23:01 /tmp/text.sql ## 將備份的數據庫導入 [root@node ~]# mysql -uroot -p text2< /tmp/text.sql # 刪除數據庫 MariaDB [text2]> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | test | | text | | text2 | +--------------------+ 6 rows in set (0.00 sec) MariaDB [text2]> drop database text; Query OK, 0 rows affected (0.00 sec) MariaDB [text2]> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | test | | text2 | +--------------------+ 5 rows in set (0.00 sec)
