Linux Engineer學習------Mariadb入門
database:數據庫
什麽是數據庫
DB,數據庫:一批數據的集合,主流的數據庫多用來存放關系型表格數據
1.1虛擬機Server:安裝MariaDB數據庫
[root@server0 ~]# yum -y install mariadb-server
MariaDB數據庫 ,端口:3306
1.2啟動數據庫服務
[root@server0 ~]# systemctl restart mariadb
[root@server0 ~]# systemctl enable mariadb
[root@server0 ~]#netstate -anptu | grep 3306 #查看是否啟動
MariaDB數據庫基本操作
[root@server0 ~]# mysql #默認沒有密碼
MariaDB [(none)]> show databases; #查看所有庫,註意分號結尾
MariaDB [(none)]> create database nsd1710; #創建庫
MariaDB [(none)]> show databases;
MariaDB [(none)]> drop database nsd1710; #刪除庫
MariaDB [(none)]> show databases;
MariaDB [(none)]> quit
1.3設置MariaDB數據庫管理員的密碼
數據庫管理員名:root MariaDB數據庫 mysql----->user
系統管理員名:root /etc/passwd
mysqladmin [-u用戶名] [-p[舊密碼]] password '新密碼'
[root@server0 ~]# mysqladmin -u root password '123' #設置數據庫密碼
[root@server0 ~]# mysql -u root -p #交互式設置
Enter password: 輸入密碼
[root@server0 ~]# mysql -u root -p123 #非交互輸入密碼進入
1.4禁止監聽,只服務於本機
[root@server0 ~]# vim /etc/my.cnf
[mysqld]
skip-networking //跳過網絡監聽,數據庫只能被 localhost 訪問
.. ..
[root@server0 ~]# systemctl restart mariadb
– 使用/選擇數據庫:USE 數據庫名;
– 列出庫裏有哪些表:SHOW TABLES;
[root@server0 ~]# mysql -u root -p123
MariaDB [mysql]> show databases;
MariaDB [mysql]> use mysql;
MariaDB [mysql]> show tables;
MariaDB [mysql]> create database nsd1710;
MariaDB [mysql]> show databases;
1.5導入數據到數據庫中
在虛擬機Server0上操作:下載事先備份好的數據庫文件
wget http://classroom/pub/materials/users.sql
[root@server0 ~]# mysql -u root -p123 nsd1710 < users.sql
[root@server0 ~]# mysql -u root -p123
MariaDB [(none)]> use nsd1710;
MariaDB [nsd1710]> show tables;
1.6查詢數據庫中表記錄
select 表字段 from 庫.表名
select * from nsd1710.base;
MariaDB [nsd1710]> select * from location;
MariaDB [nsd1710]> select * from base;
增 insert
刪 delete
改 update
查 select
1.7有條件的查詢
1.在base表中查詢密碼為123的用戶的名字?
[root@server0 ~]# mysql -u root -p123
MariaDB [(none)]> use nsd1710;
MariaDB [nsd1710]> show tables;
> select * from base where password='123';
> select name,password from base where password='123';
> select name from base where password='123';
> select id,name from base;
並使用相 應的 SQL 查詢以回答下列問題:
1)密碼是 solicitous 的人的名字?
> select name from base where password='solicitous';
2)有多少人的姓名是 Barbara 同時居住在 Sunnyvale?
> select * from base,location where base.name='Barbara' and location.city='Sunnyvale' and base.id=location.id;
> select count(*) from base,location where base.name='Barbara' and location.city='Sunnyvale' and base.id=location.id;
> insert base values (6,'Barbara','321'); #插入表記錄
> insert location values (6,'Sunnyvale'); #插入表記錄
> select * from base;
> select * from location ;
> select * from base,location where base.name='Barbara' and location.city='Sunnyvale' and base.id=location.id;
1.8數據庫授權
– 除了 root 用戶,此nsd1710數據庫只能被用戶 lisi 查詢,此用戶的密碼為123
MariaDB數據庫 mysql----->user
– GRANT 權限列表 ON 數據庫名.表名 TO 用戶名@ 客戶機地址 IDENTIFIED BY '密碼';
[root@server0 ~]# mysql -u root -p123
grant select on nsd1710.* to lisi@localhost identified by '123';
當lisi從本地localhost登陸,輸入密碼123.將會獲得nsd1710庫中所有表的查詢的權限
驗證:測試lisi登陸
[root@server0 ~]# mysql -u lisi -p123
1.9DELETE 刪除表記錄
? MariaDB [(none)]> 交互指令
– DELETE FROM [數據庫.]表名 WHERE 條件語句;
1. 禁止空密碼root用戶訪問 mariadb 數據庫
[root@server0 ~]# mysql -u root -p123
> use mysql;
> select user,host,password from user;
> select user,host,password from user where password='';
> delete from user where password='';
> select user,host,password from user;
刷新user表的記錄:
MariaDB [(none)]> flush privileges;
Linux Engineer學習------Mariadb入門