Linux--RH254---unit 8 mariadb數據庫

unit 8 Mariadb

一、數據庫的基本sql語句操作
1.安裝

yum install mariadb-server -y      ###安裝mariadb


systemctl start mariadb            ###啓動mariadb服務
netstat -antlpe | grep mysql       ###校驗mariadb的監聽端口
vim /etc/my.cnf                    ###編輯/etc/my.cnf文件

skip-networking=1                  ###阻斷監聽

netstat -antlpe | grep mysql       ###mariadb的監聽端口不存在,此時只允許通過套接字文件進行本地連接,阻斷所有來自網絡的tcp/ip連接。



mysql_secure_installation          ###第一次安裝mysql以後通過這條命令可以對mysql進行設置

Enter current password for root (enter for none) 按Enter
Set root password? [Y/n] y
New password:
Re-enter new password:
Remove anonymous users? [Y/n] y
Disallow root login remotely? [Y/n] y
Remove test database and access to it? [Y/n] y

Reload privilege tables now? [Y/n] y



2.登陸
mysql -uroot -pwestos                   ###u表示指定登陸用戶,p表示指定此用戶密碼(密碼爲明文)
mysql -uroot -p                         ###密碼爲加密

Enter password:



3.查詢

MariaDB [(none)]> show databases;       ###顯示數據庫


MariaDB [(none)]> use mysql;            ###進入mtsql庫


MariaDB [mysql]> show tables;           ###顯示當前庫中表的名字


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


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


MariaDB [mysql]> quit                   ###退出

4.數據庫及表的建立

create database westos;          ###創建westos庫


create table linux(         ###創建linux表,含有username,password兩個字段

-> username varchar(15) not null,   ###添加username字段,字符長度最大15,且不能爲空
-> password varchar(15) not null    ###添加password字段,字符長度最大15,且不能爲空

-> );


insert into linux values ('user1','123');              ###在linux表中插入數據,username字段數據爲user1,password字段數據爲123

insert into linux values ('user1',password('123'));   ###插入password字段的數據是用password加密過的



5.更新數據庫信息
update linux set password=password('456') where username='user2';                           ###更新user2的密碼

update linux set password=password('456') where ( username='user1' or username='user2' );  ###更新user1和user2的密碼


delete from linux where username='user3';                                                   ###刪除user3的信息

alter table linux add age varchar(4);                                                      ###添加age字段到linux表的最後一列

alter table linux add class varchar(4) after password;                                       ###添加class字段到password字段後


alter table linux drop age;                                                                  ###刪除age字段



6.刪除數據庫

delete from linux where username='user2';     ###從linux表中刪除user2的數據


drop table linux;                             ###刪除linux表


drop database westos;                         ###刪除westos庫



7.數據庫的備份    

mysqldump -uroot -pwestos --all-database                ###備份所有表中的左右數據


mysqldump -uroot -pwestos --all-database --no-data      ###備份所有表,但不備份數據


mysqldump -uroot -pwestos westos                        ###備份westos庫


mysqldump -uroot -pwestos westos > /mnt/westos.sql      ###備份westos庫並把數據保存到westos.sql中

mysqldump -uroot -pwestos westos linux > /mnt/linux.sql ###備份westos庫中的linux表


mysql -uroot -pwestos -e "create database westos;"      ###建立westos庫
mysql -uroot -pwestos westos < /mnt/westos.sql      ###把數據導入westos庫

mysql -uroot -pwestos -e "select * from westos.linux;"  ###查看westos庫中linux表所有內容



8.用戶授權
mysql -uroot -pwestos
select User,Host from mysql.user;                    ###查看mysql庫中user表的User,Host字段
create user redhat@localhost identified by 'redhat';                   ###建立用戶redhat,此用戶只能通過本機登陸

create user redhat@'%' identified by 'redhat';                         ###建立用戶redhat,此用戶可以通過網絡登陸


mysql -uredhat -predhat -h localhost                                   ###本機登陸


vim /etc/my.cnf                                                        ###更改配置文件
skip-networking=0                                                      ###開啓監聽端口,此時本地連接和網絡連接皆可
systemctl restart mariadb                                              ###重啓mariadb

mysql -uredhat -predhat -h 172.25.254.131                              ###網絡登陸


mysql -uroot -pwestos
grant insert,update,delete,select on westos.linux to redhat@localhost; ###本機登陸用戶授權
grant select on westos.linux to redhat@'%';                            ###網絡登陸用戶授權

show grants for redhat@localhost;                                       ###查看用戶授權


revoke delete on westos.linux from redhat@localhost;                   ###刪去用戶授權權力


drop user redhat@'%';                                                  ###刪除用戶



9.密碼修改

mysqladmin -uroot -pwestos password linux   ##在知道密碼的情況下修改超級用戶密碼


##當超級用戶密碼忘記
systemctl stop mariadb                      ##關閉Screenshot from 2017-05-16 12-29-41
mysqld_safe --skip-grant-tables &           ##開啓mysql登陸接口並忽略授權表
mysql                                       ##直接登陸不用密碼

update mysql.user set Password=password('123') where User='root';  ##更新超級用戶密碼信息


ps aux | grep mysql         ##過濾mysql的所有進程
kill -9 mysqlpid            ##結束進程
systemctl start mariadb     ##重新開啓mysql

mysql -uroot -p123          ##登陸測試



二、數據庫的網頁管理工具
1.安裝

yum install httpd php php-mysql -y      ##安裝httpd php php-mysql


systemctl start httpd                   ##開啓httpd

systemctl enable httpd                  ##開機自啓動
systemctl stop firewalld                ##關閉防火牆

systemctl disable firewalld             ##開機自關閉


tar jxf phpMyAdmin-3.4.0-all-languages.tar.bz2 -C /var/www/html/  ##解壓phpMyAdmin-3.4.0-all-languages.tar.bz2到/var/www/html/
cd /var/www/html/                       ##切換至/var/www/html/               
mv phpMyAdmin-3.4.0-all-languages/ mysqladmin    ##移動phpMyAdmin-3.4.0-all-languages下所有文件至mysqladmin
cd mysqladmin                           ##切換至mysqladmin
cp -p config.sample.inc.php config.inc.php   ##添加配置文件
vim config.inc.php                      ##修改配置文件
$cfg['blowfish_secret'] = 'mysql'; /* YOU MUST FILL IN THIS FOR COOKIE AUTH! */

systemctl restart httpd                 ##重啓httpd



2.訪問測試:172.25.254.131/mysqladmin


數據庫及表的建立:

數據庫-->新建數據庫redhat-->創建     ##創建redhat庫



點擊redhat庫-->新建linux表,字段2-->執行   ##創建linux表


字段user、password--類型VARCHAR--長度15--保存   ##添加linux表的字段


數據庫及表的刪除:

點擊redhat庫-->點擊linux2表後面的刪除-->DROP TABLE linux2(確定)  ##刪除linux2表



數據庫-->勾上redhat-->點擊刪除-->DROP DATABASE 'redhat'(是)   ##刪除redhat庫



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