Linux學習篇第三章之~mariadb

Mariadb數據庫

 安裝Mariadb數據庫

 配置和管理數據庫
 配置數據庫用戶和訪問權限

 備份和恢復數據庫

1.Mariadb安裝

> yum install mariadb-server -y      ####下載數據庫軟件

> systemctl start mariadb          #####開啓服務

> mysql                      ####進入到數據庫中

Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 2
Server version: 5.5.35-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)]> quit
Bye
> netstat -antlpe | grep mysql     ####查看mysql端口情況
tcp        0      0 0.0.0.0:3306            0.0.0.0:*               LISTEN     27         76495      3536/mysqld         
> vim /etc/my.cnf    #####編寫配置文件,屏蔽網絡接口


[root@mariadb ~]# systemctl restart mariadb   
[root@mariadb ~]# netstat -antlpe | grep mysql   #####查看mysql服務
[root@mariadb ~]# mysql
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 2
Server version: 5.5.35-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)]> quit
Bye

> 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  #####不允許遠程用戶登錄
 ... Success!
Remove test database and access to it? [Y/n] Y  ######刪除測試數據庫



[root@mariadb ~]# mysql
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
[root@mariadb ~]# mysql -uroot -p    ####root用戶登錄
MariaDB [(none)]> show databases;    #####顯示數據庫



MariaDB [(none)]> use mysql     #####進入mysql庫
Database changed
MariaDB [mysql]> show tables;     ####顯示當前庫中表的名稱




MariaDB [mysql]> desc user;    ######查詢user表的結構(顯示所有字段的名稱)

MariaDB [mysql]> select * from user;   #####查詢user表中所有內容(*可以用此表中的任何字段來代替)

2.數據庫的建立

MariaDB [(none)]> create  database westos;   ####創建westos庫
MariaDB [(none)]> show databases;         #####顯示數據庫

MariaDB [(none)]> use westos     ####進入westos庫
MariaDB [westos]> create table linux(   #####創建linux表,表含有兩個字段,username,password
    -> username varchar(15) not null,   

    -> password varchar(15) not null);
MariaDB [westos]> insert into linux values ('user1','123');  ####向linux表中插入數據,username的字段的                                             數據爲user1,password字段的數據爲123
MariaDB [westos]> insert into linux values ('user2',password('123'));

MariaDB [westos]> select * from linux;   #####查詢linux表中內容


MariaDB [westos]> update linux set password=password('123') where username='user1';  #####更新                                                             user1的密碼
MariaDB [westos]> select * from linux;   #####查詢linux表中內容
MariaDB [westos]> delete from linux where username='user1';  #####刪除user1信息
MariaDB [westos]> select * from linux;


MariaDB [westos]> alter table linux add class varchar(20) not null after password; ####添加class字段到                                           linux表中且不爲空,添加到password字段


3.用戶授權
[root@mariadb mnt]# mysql  -uroot -pwestos  #####root用戶登錄
MariaDB [(none)]> select User from mysql.user;  #####查看User

MariaDB [(none)]> create user zhao@localhost identified by 'zhao';  #####建立本地用戶zhao                                       

                    
MariaDB [(none)]> create user zhao@'%' identified by 'zhao';   

[root@mariadb mnt]# mysql  -uzhao -pzhao -h 172.25.254.142  
ERROR 2003 (HY000): Can't connect to MySQL server on '172.25.254.142' (111)
[root@mariadb mnt]# vim /etc/my.cnf
skip-networking=0
[root@mariadb mnt]# systemctl restart mariadb
[root@mariadb mnt]# mysql  -uzhao -pzhao -h 172.25.254.143   ##測試
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 2
Server version: 5.5.35-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)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
+--------------------+
1 row in set (0.00 sec)
MariaDB [(none)]> quit
[root@mariadb mnt]# mysql  -uroot -pwestos
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 3
Server version: 5.5.35-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)]> grant insert,update,delete,select on westos.* to lee@localhost;
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> grant select on westos.* to zhao@'%';
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> show grants for zhao@'%';
+----------------------------------------------------------------------------------------------------+
| Grants for zhao@%                                                                                   |
+----------------------------------------------------------------------------------------------------+
| GRANT USAGE ON *.* TO 'zhao'@'%' IDENTIFIED BY PASSWORD '*9BB439A3A652A9DAD3718215F77A7AA06108A267' |
| GRANT SELECT ON `westos`.* TO 'zhao'@'%'                                                            |
+----------------------------------------------------------------------------------------------------+
2 rows in set (0.00 sec)


MariaDB [(none)]> show grants for zhao@localhost;
+------------------------------------------------------------------------------------------------------------+
| Grants for zhao@localhost                                                                                   |
+------------------------------------------------------------------------------------------------------------+
| GRANT USAGE ON *.* TO 'zhao'@'localhost' IDENTIFIED BY PASSWORD '*9BB439A3A652A9DAD3718215F77A7AA06108A267' |
| GRANT SELECT, INSERT, UPDATE, DELETE ON `westos`.* TO 'zhao'@'localhost'                                    |
+------------------------------------------------------------------------------------------------------------+
2 rows in set (0.00 sec)

4.數據庫的網頁管理工具
> yum install httpd php php-mysql -y   ##安裝服務
> systemctl start httpd
> systemctl enable httpd
>tar jxf phpMyAdmin-3.4.0-all-languages.tar.bz2 -C /var/www/html/  ##解壓文件到指定目錄
> cd /var/www/html/
> ls
phpMyAdmin-3.4.0-all-languages
> mv phpMyAdmin-3.4.0-all-languages/ mysqladmin   ##重命名
> cp -p config.sample.inc.php config.inc.php    ##複製模板
> vim config.inc.php
>systemctl restart httpd


5.密碼修改
當超級用戶忘記密碼
[root@mariadb mnt]# systemctl stop mariadb     ####關閉mysql
[root@mariadb mnt]# mysqld_safe --skip-grant-tables & ####開啓mysql登錄接口並忽略授權表
[1] 2189
[root@mariadb mnt]# 170513 01:49:03 mysqld_safe Logging to '/var/log/mariadb/mariadb.log'.
170513 01:49:03 mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql

[root@mariadb mnt]# mysql       ####直接不用密碼可以登錄
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 1
Server version: 5.5.35-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('123') where User='root';      ######更新                                                          超級用戶密碼信息

[root@mariadb mnt]# fg
mysqld_safe --skip-grant-tables
^Z
[1]+  Stopped                 mysqld_safe --skip-grant-tables
[root@mariadb mnt]# killall -9 mysqld_safe
[1]+  Killed                  mysqld_safe --skip-grant-tables
[root@mariadb mnt]# ps aux | grep mysql   #####過濾mysql的所有進程並結束這些進程
mysql     2351  0.0  9.0 859068 89732 pts/1    Sl   01:49   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      2413  0.0  0.0 112640   936 pts/1    R+   01:53   0:00 grep --color=auto mysql
[root@mariadb mnt]# kill -9 2351
[root@mariadb mnt]# ps aux | grep mysql
root      2433  0.0  0.0 112640   932 pts/1    R+   01:54   0:00 grep --color=auto mysql
[root@mariadb mnt]# systemctl start mariadb ######重新開啓mysql
[root@mariadb mnt]# mysql -uroot -p123      #####登錄測試


Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 2
Server version: 5.5.35-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)]> quit
Bye


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章