MariaDB數據庫的安裝與配置
一、安裝
yum install mariadb-server -y
##安裝
systemctl start mariadb
##開啓服務
二、基本配置
一)安全初始化
1、默認情況下,數據庫端口是打開的,其他主機可以通過打開的端口來進行訪問,所以先將端口關閉
[root@station mysql]# netstat -antlpe | grep mysql
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 27 39838 3913/mysqld
[root@station mysql]# vim /etc/my.cnf
skip-networking=1 ##添加這一行,關閉網絡端口
[root@station mysql]# systemctl restart mariadb
[root@station mysql]# netstat -antlpe | grep mysql ##此時不會有任何返回值,端口已經關閉
2、數據庫的設定初始狀態是不安全的,所以需要設定密碼
mysQl_secure_installation
##這條命令爲數據庫設置密碼
二)密碼設置
1、更改密碼
mysqladmin -uroot -plcl970618 password lcl
#把原來的密碼修改爲lcl
2、當超級用戶密碼忘記,不能登陸數據庫時
[root@station ~]# systemctl stop mariadb ##先關掉數據庫
[root@station ~]# mysqld_safe --skip-grant-tables & ##跳過數據庫密碼驗證階段
[1] 4815
[root@station ~]# 171123 05:43:05 mysqld_safe Logging to '/var/log/mariadb/mariadb.log'.
171123 05:43:05 mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql
[root@station ~]# mysql ##此時進入數據庫不需要密碼
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 1
Server version: 5.5.44-MariaDB MariaDB Server
Copyright (c) 2000, 2015, Oracle, MariaDB Corporation 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('0618') WHERE User='root'; ##更新User爲root的密碼爲0618
Query OK, 3 rows affected (0.00 sec)
Rows matched: 3 Changed: 3 Warnings: 0
MariaDB [(none)]> quit;
Bye
[root@station ~]# ps aux|grep mysql ##列出數據庫的所有進程
root 4815 0.0 0.1 113252 1620 pts/0 S 05:43 0:00 /bin/sh /usr/bin/mysqld_safe --skip-grant-tables
mysql 4970 0.0 8.2 843504 84256 pts/0 Sl 05:43 0:00 /usr/libexec/mysqld --basedir=/usr --datadir=/var/lib/mysql --plugin-dir=/usr/lib64/mysql/plugin --user=mysql --skip-grant-tables --log-error=/var/log/mariadb/mariadb.log --pid-file=/var/run/mariadb/mariadb.pid --socket=/var/lib/mysql/mysql.sock
root 5012 0.0 0.0 112644 956 pts/0 R+ 05:44 0:00 grep --color=auto mysql
[root@station ~]# kill -9 4815 ##關閉數據庫的所有進程
[root@station ~]# kill -9 4970
[1]+ Killed mysqld_safe --skip-grant-tables
[root@station ~]# systemctl start mariadb ##開啓數據庫
[root@station ~]# mysql -uroot -p ##此時進入數據庫需要的密碼是剛纔更新的密碼
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 2
Server version: 5.5.44-MariaDB MariaDB Server
Copyright (c) 2000, 2015, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> ##成功進入
三)數據庫的管理
mysql交互模式下的數據庫、表都在/var/lib/mysql/目錄裏面,其中裏面的目錄是庫,庫目錄裏面的文件是表的名稱。
1、建立、查詢
[root@station ~]# mysql -uroot -p
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 2
Server version: 5.5.44-MariaDB MariaDB Server
Copyright (c) 2000, 2015, 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 |
+--------------------+
3 rows in set (0.00 sec)
MariaDB [(none)]> CREATE DATABASE lcl; ##創建名爲lcl的庫
Query OK, 1 row affected (0.00 sec)
MariaDB [(none)]> USE lcl;
Database changed
MariaDB [lcl]> CREATE TABLE user ( ##創建名爲user的表
-> username varchar(50) not null, ##創建字段username,字符長最大爲50,不能爲空
-> password varchar(50) not null ##創建password字段
-> );
Query OK, 0 rows affected (0.07 sec)
MariaDB [lcl]> DESC user; ##列出表結構
+----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| username | varchar(50) | NO | | NULL | |
| password | varchar(50) | NO | | NULL | |
+----------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)
MariaDB [lcl]> INSERT INTO user VALUES ('alice','123'); ##插入數據
Query OK, 1 row affected (0.03 sec)
MariaDB [lcl]> SELECT * FROM user; ##列出user表中的所有數據
+----------+----------+
| username | password |
+----------+----------+
| alice | 123 |
+----------+----------+
1 row in set (0.00 sec)
MariaDB [lcl]> SELECT username FROM user ##列出user表中的username字段的數據
-> ;
+----------+
| username |
+----------+
| alice |
+----------+
1 row in set (0.00 sec)
2、更改
MariaDB [lcl]> UPDATE user SET password=password('alice') WHERE username='alice'; ##將alice的密碼改爲加密後的‘alice’,等號後面的password表示加密
Query OK, 1 row affected (0.03 sec)
Rows matched: 1 Changed: 1 Warnings: 0
MariaDB [lcl]> SELECT * FROM user;
+----------+-------------------------------------------+
| username | password |
+----------+-------------------------------------------+
| alice | *4F5CCA657BD61D1C1127E5C4EA3B0EE4A9841B85 |
+----------+-------------------------------------------+
1 row in set (0.00 sec)
MariaDB [lcl]> ALTER TABLE user ADD class varchar(20); ##添加class字段,最大字符爲20,若沒有指定位置,則默認會添加在表的字段末尾
Query OK, 1 row affected (0.41 sec)
Records: 1 Duplicates: 0 Warnings: 0
MariaDB [lcl]> SELECT * FROM user;
+----------+-------------------------------------------+-------+
| username | password | class |
+----------+-------------------------------------------+-------+
| alice | *4F5CCA657BD61D1C1127E5C4EA3B0EE4A9841B85 | NULL |
+----------+-------------------------------------------+-------+
1 row in set (0.00 sec)
MariaDB [lcl]> ALTER TABLE user ADD age varchar(10) AFTER username; ##指定將age字段添加在username字段後面,默認只能添加在某個字段之後,而不能用before
Query OK, 1 row affected (0.16 sec)
Records: 1 Duplicates: 0 Warnings: 0
MariaDB [lcl]> SELECT * FROM user;
+----------+------+-------------------------------------------+-------+
| username | age | password | class |
+----------+------+-------------------------------------------+-------+
| alice | NULL | *4F5CCA657BD61D1C1127E5C4EA3B0EE4A9841B85 | NULL |
+----------+------+-------------------------------------------+-------+
1 row in set (0.00 sec)
MariaDB [lcl]> UPDATE user SET password='123' WHERE username='alice';
Query OK, 1 row affected (0.02 sec)
Rows matched: 1 Changed: 1 Warnings: 0
MariaDB [lcl]> SELECT * FROM user; ##密碼沒有經過加密
+----------+------+----------+-------+
| username | age | password | class |
+----------+------+----------+-------+
| alice | NULL | 123 | NULL |
+----------+------+----------+-------+
1 row in set (0.00 sec)
MariaDB [lcl]> ALTER TABLE user RENAME redhat; ##更改表的名字爲redhat
Query OK, 0 rows affected (0.04 sec)
MariaDB [lcl]> SELECT * FROM redhat;
+----------+------+----------+-------+
| username | age | password | class |
+----------+------+----------+-------+
| alice | NULL | 123 | NULL |
+----------+------+----------+-------+
1 row in set (0.00 sec)
3、刪除
MariaDB [lcl]> DELETE FROM redhat WHERE username='alice'; ##刪除alice數據
Query OK, 1 row affected (0.02 sec)
MariaDB [lcl]> SELECT * FROM redhat;
Empty set (0.00 sec)
MariaDB [lcl]> DROP TABLE redhat; ##刪除表redhat
Query OK, 0 rows affected (0.03 sec)
MariaDB [lcl]> DROP DATABASE lcl; ##刪除庫lcl
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> SHOW DATABASES; ##可以看出已經刪除
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
+--------------------+
3 rows in set (0.00 sec)
4、用戶的授權
MariaDB [(none)]> USE mysql;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
MariaDB [mysql]> CREATE user lcl@localhost identified by 'redhat'; ##創建lcl用戶,並且設置密碼爲redhat,@localhost表示只能在本地登陸
Query OK, 0 rows affected (0.00 sec)
MariaDB [mysql]> GRANT SELECT,INSERT on lcl.* to lcl@localhost; ##授予lcl用戶SELECT,INSERT權限
Query OK, 0 rows affected (0.00 sec)
MariaDB [mysql]> SHOW GRANTS for lcl@localhost; ##列出lcl用戶的權限,和設置的相符合
+------------------------------------------------------------------------------------------------------------+
| Grants for lcl@localhost |
+------------------------------------------------------------------------------------------------------------+
| GRANT USAGE ON *.* TO 'lcl'@'localhost' IDENTIFIED BY PASSWORD '*84BB5DF4823DA319BBF86C99624479A198E6EEE9' |
| GRANT SELECT, INSERT ON `lcl`.* TO 'lcl'@'localhost' |
+------------------------------------------------------------------------------------------------------------+
2 rows in set (0.00 sec)
MariaDB [mysql]> REVOKE INSERT on lcl.* from lcl@localhost; ##去掉lcl用戶的insert權限
Query OK, 0 rows affected (0.00 sec)
MariaDB [mysql]> SHOW GRANTS for lcl@localhost; ##lcl用戶此時只剩下SELECT權限
+------------------------------------------------------------------------------------------------------------+
| Grants for lcl@localhost |
+------------------------------------------------------------------------------------------------------------+
| GRANT USAGE ON *.* TO 'lcl'@'localhost' IDENTIFIED BY PASSWORD '*84BB5DF4823DA319BBF86C99624479A198E6EEE9' |
| GRANT SELECT ON `lcl`.* TO 'lcl'@'localhost' |
+------------------------------------------------------------------------------------------------------------+
2 rows in set (0.00 sec)
5、數據庫的備份與恢復
備份
[root@station ~]# mysqldump -uroot -p0618 lcl > /mnt/redhat.sql
#將lcl庫備份到/mnt/redhat.sql文件中
mysqldump -uroot -plcl redhat –no-data ##將redhat庫的框架進行備份,不備份數據
mysqldump -uroot -plcl –all-database ##將所有數據庫的內容和框架進行備份
mysqldump -uroot -plcl –all-database –no-data ##將所有數據庫的框架進行備份,不備份數據
恢復
方法一:
[root@station ~]# mysql -uroot -p0618 -e "CREATE DATABASE redhat;" ##進入數據庫並創建redhat庫
[root@station ~]# mysql -uroot -p0618 redhat < /mnt/redhat.sql ##從/mnt/redhat.sql文件中將文件內容導入redhat庫中
[root@station ~]# mysql -uroot -p0618
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 8
Server version: 5.5.44-MariaDB MariaDB Server
Copyright (c) 2000, 2015, 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 |
| lcl |
| mysql |
| performance_schema |
| redhat |
+--------------------+
5 rows in set (0.00 sec)
MariaDB [(none)]> USE redhat;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
MariaDB [redhat]> SHOW TABLES;
+------------------+
| Tables_in_redhat |
+------------------+
| redhat |
+------------------+
1 row in set (0.00 sec)
MariaDB [redhat]> SELECT * FROM redhat;
+----------+----------+
| username | password |
+----------+----------+
| alice | 123 |
+----------+----------+
1 row in set (0.00 sec)
此時恢復成功
方法二:
MariaDB [redhat]> DROP TABLE redhat; ##先將表刪除
Query OK, 0 rows affected (0.04 sec)
MariaDB [redhat]> DROP DATABASE redhat; ##刪除庫
Query OK, 0 rows affected (0.00 sec)
[root@station ~]# vim /mnt/redhat.sql ##修改備份文件
21 CREATE DATABASE redhat; ##創建庫
22 USE redhat; ##使用庫
23 DROP TABLE IF EXISTS `redhat`; ##這行本身就有,是想說明在哪裏添加
[root@station ~]# mysql -uroot -p0618 < /mnt/redhat.sql
##此時進入數據庫查看就會發現恢復成功
四)phpmyadmin數據庫圖形化管理
- 下載phpadmin軟件包
- yum install php php-mysql -y
- systemctl restart httpd
- tar jxf phpMyAdmin-3.4.0-all-languages.tar.bz2 -C /var/www/html/ ##記得解壓到htpp的默認目錄中,這樣可以通過瀏覽器查看
- mv phpMyAdmin-3.4.0-all-languages/ phpadmin ##因爲默認名稱太長,不利於瀏覽器查看,所以換了個名字
- cd phpadmin ##裏面有README文件,可以通過查看安裝方法安裝,具體方法如下:
- cp config.sample.inc.php config.inc.php
- vim Documentation.txt
139 $cfg[‘blowfish_secret’] = ‘ba17c1ec07d65003’; ##將這一行引號中的內容複製到config.inc.php中 - vim config.inc.php
17 $cfg[‘blowfish_secret’] = ‘ba17c1ec07d65003’; - systemctl restart httpd
測試