mariadb使用

来源:互联网 发布:移动网络转换器 编辑:程序博客网 时间:2024/06/03 05:16

mariadb安装

yum install mariadb-server.x86_64 -ysystemctl start mariadbsystemctl enable mariadb

1.使用netstat -antlpe | grep mysql命令查看mysql程序运行的端口。
这里写图片描述

2.编辑/etc/my.cnf配置文件,添加如下行。

skip-networking=1      ##禁止远程登陆

3.运行mysql_secure_installation安全配置向导,设置root用户密码,以及进行一系列初始化操作,提升mysql库的安全性。详情参考http://www.jb51.net/article/47727.htm

基本sql命令
在实现mariadb的全部安装后,执行mysql -uroot -p命令,输入密码,进入mysql。
这里写图片描述

进入mysql后,执行以下命令,展示数据库及基本表。

//数据库的查询、表结构展示show databases;       ##展示数据库use mysql;            ##进入mysql库show tables;          ##显示当前库中的所有表desc user;            ##展示user表结构select * from user;   ##展示user表的全部内容

这里可以看到,安装mysql后,会有三个自带数据库。information_schema是一个信息数据库,保存着关于MySQL服务器所维护的所有其他数据库的信息。如数据库名,数据库的表,表栏的数据类型及访问权限等。performance_schema是一个存储引擎,主要用于收集数据库服务器性能参数。mysql是核心数据库,主要负责存储数据库的用户、权限设置、关键字等mysql自己需要使用的控制和管理信息。
这里写图片描述

使用use mysql命令进入mysql库,然后查看当前库中的所有表。
这里写图片描述

1.数据库的创建及基本表查询

//创建westos数据库,并进入westos库,新建student表,包括name、id、class和age字段create database westos;use westos;create table student (name varchar(50) not null,id varchar(50) not null,class varchar(50) not null,age varchar(4));

查看student表结构
这里写图片描述

2.表数据的插入、删除及修改

//向student表插入数行数据insert into student values ('Cecilia','04131087','1303','20');insert into student values ('Ilona','04131088','1303','19');insert into student values ('Briony','04131089','1303','21');

打印student表全部内容
这里写图片描述

//修改student表数据alter table student rename linux;                       ##将student表重命名为linuxalter table student add password varchar(50);           ##向student表添加password字段alter table student add password varchar(50) after id;  ##向student表添加password字段于id字段后update student set class='1304';                        ##将class字段的值统一修改为'1304'update student set class='1305' where id='04131087';    ##将id为'04131087'的学生的班级修改为'1305'

打印student表全部内容,可以看到修改项
这里写图片描述

//删除表中数据、表及库delete from student where name='Ilona' and id='04131088';   ##删除姓名为'Ilona'且学号是'04131088'学生的信息drop table student;         ##删除student表drop database westos;       ##删除westos数据库

3.数据库的备份及恢复

mysqldump -uroot -predhat westos > /mnt/westos.sql    ##备份westos数据库至/mnt/westos.sql下drop database westos;          ##删除westos库mysql -uroot -predhat -e "create database westos;"    ##新建库mysql -uroot -predhat westos < /mnt/westos.sql        ##将原有数据恢复

4.用户及访问权限的设置

create user Lily@localhost identified by 'redhat';     ##添加用户Lily,并设置其密码为redhatgrant insert,update,delete,select on westos.* to Lily@localhost;    ##赋予Lily对westos数据库的所有表insert,update,delete,select权限show grants for Lily@localhost;   ##查看用户Lily的授权情况revoke insert,update,delete,select on westos.* from Lily@localhost;      ##撤销用户Lily的若干权限drop user Lily@localhost;   ##删除用户Lily

这里写图片描述

5.数据库密码恢复
当我们忘记数据库密码时,可以通过以下方法恢复。

systemctl stop mariadb.service      ##关闭mariadb服务mysqld_safe --skip-grant-tables &   ##进入安全模式mysql -uroot                        ##免密码进入mysqluse mysql                           ##进入mysql数据库update user set Password=password('redhat') where User='root';             ##重置密码

随后退出mysql,利用ps aux | grep mysql 命令查看进程号,然后使用kill命令杀死该进程,最后重启mariadb服务。
这里写图片描述

原创粉丝点击