1、MariaDB数据库
database:数据库
什么是数据库
DB,数据库:一批数据的集合,主流的数据库多用来存放关系型表格数据
1.1虚拟机Server:安装MariaDB数据库
[[email protected] ~]# yum -y install mariadb-server
MariaDB数据库 ,端口:3306
1.2启动数据库服务
[[email protected] ~]# systemctl restart mariadb
[[email protected] ~]# systemctl enable mariadb
[[email protected] ~]#netstate -anptu | grep 3306 #查看是否启动
MariaDB数据库基本操作
[[email protected] ~]# 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 '新密码'
[[email protected] ~]# mysqladmin -u root password '123' #设置数据库密码
[[email protected] ~]# mysql -u root -p #交互式设置
Enter password: 输入密码
[[email protected] ~]# mysql -u root -p123 #非交互输入密码进入
1.4禁止监听,只服务于本机
[[email protected] ~]# vim /etc/my.cnf
[mysqld]
skip-networking //跳过网络监听,数据库只能被 localhost 访问
.. ..
[[email protected] ~]# systemctl restart mariadb
– 使用/选择数据库:USE 数据库名;
– 列出库里有哪些表:SHOW TABLES;
[[email protected] ~]# 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
[[email protected] ~]# mysql -u root -p123 nsd1710 < users.sql
[[email protected] ~]# 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的用户的名字?
[[email protected] ~]# 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 '密码';
[[email protected] ~]# mysql -u root -p123
grant select on nsd1710.* to [email protected] identified by '123';
当lisi从本地localhost登陆,输入密码123.将会获得nsd1710库中所有表的查询的权限
验证:测试lisi登陆
[[email protected] ~]# mysql -u lisi -p123
1.9DELETE 删除表记录
? MariaDB [(none)]> 交互指令
– DELETE FROM [数据库.]表名 WHERE 条件语句;
1. 禁止空密码root用户访问 mariadb 数据库
[[email protected]r0 ~]# 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;
原文地址:http://blog.51cto.com/13452945/2061017