MariaDB入门操作笔记

MariaDB入门操作

前言:

使用help xxx进行命令帮助。Mariadb服务默认端口号是TCP 3306

初始化:

mysql_secure_installation #安装后,即进行初始化

使用mysql -uroot -p登陆数据库:

查看数据库状态:

status;

一、用户操作:

1、创建用户的语法为:CREATE USER 用户名@主机名 IDENTIFIED BY ‘密码’;

CREATE USER xiaok@localhost IDENTIFIED BY ‘123456’;

备注:数据库中用户名全称由两部分组成,“用户名”@“登陆终端(可以是IP地址)”。

select user();查看当前登录用户.

2、用户密码设置;

mysqladmin -u root -p password 123456

操作系统中设置

set password = password(123456) ;

设置当前用户密码

update user set password=password(“123456”) where user=‘root’;

修改指定用户密码

flush privileges;

刷新数据库信息

3、使用GRANT命令给用户授权,

grant 权限 on 数据库.表 to’用户’@‘IP地址’;

grant all privileges on zabbix.* to xiaok@localhost;

4、查看用户权限使用show grants for “用户名全称”;

show grants for xiaok@localhost;

5、移除用户权限用命令revoke代替GRANT即可。

revoke 权限 on 数据库.表 from ‘用户名’@‘IP地址’;

用户相关权限如下:

选项

权限

对权限的限制如下:

all privileges

除grant外的所有权限

select

仅查权限

select,insert

查和插入权限

usage

无访问权限

alter

使用alter table

alter routine

使用alter procedure和drop procedure

create

使用create table

create routine

使用create procedure

create temporary tables

使用create temporary tables

create user

使用create user、drop user、rename user和revoke all privileges

create view

使用create view

delete

使用delete

drop

使用drop table

execute

使用call和存储过程

file

使用select into outfile 和 load data infile

grant option

使用grant 和 revoke

index

使用index

insert

使用insert

lock tables

使用lock table

process

使用show full processlist

show databases

使用show databases

show view

使用show view

update

使用update

reload

使用flush

shutdown

使用mysqladmin shutdown(关闭MySQL)

super

使用change master、kill、logs、purge、master和set global。还允许mysqladmin调试登陆

replication client

服务器位置的访问

replication slave

由复制从属使用

对数据库及内部其他权限如下

数据库名.*

数据库中的所有

数据库名.表

指定数据库中的某张表

数据库名.存储过程

指定数据库中的存储过程

.

所有数据库

对于用户和IP的权限如下:

用户名@IP地址

用户只能在改IP下才能访问

用户名@192.168.1.%

用户只能在改IP段下才能访问(通配符%表示任意)

用户名@%

用户可以再任意IP下访问(默认IP地址为%)

二、数据库增删改查操作:

用法

作用

create database 数据库名称;

创建新的数据库。

create table test1 like testtabe;

直接调用别的表的结构

create table test2 as select * from testtable;

直接把别的表的结构和数据都拷过来

create table testtable(id int(2),name varchar(10),mail varchar(2));

创建表

delete from test1;

删的只是数据,表的结构还在

delete from testtable where id=4;

删除一行数据

drop database testdatabase;

删除数据库

drop table test1;

drop直接把表的数据和结构都给删了

truncate test3;

和delete很像,删除数据,但是这个比delete更狠,可以删除硬盘上的表

update 表单名称 set attribute=新值 where attribute=原值;

更新表单中的数据。

alter table testtable add afterid varchar(20) after id;

添加afterid这一列到id的后面

alter table testtable add firstlist varchar(20) first;

把firstlist这一列添加到第一列

alter table testtable add newlist varchar(20);

修改表的结构,插入一列

alter table testtable drop newlist;

删除newlist这一列

insert into testntable values(5,‘test5’,‘[email protected]’),(6,‘test6’,‘[email protected]’);

一次插入多行数据

insert into testt1 select * from qntable;

直接插入别的表的数据

insert into testtable(id,name,mail) values(1,‘test1’,‘[email protected]’);

插入数据(如果每列都插入数据就不用加字段名)

insert into testtable(id,name) values(1,‘test3’);

插入两列数据

use 数据库名称;

指定使用的数据库。

show databases;

显示当前已有的数据库。

show tables;

显示当前数据库中的表单。

select * from 表单名称;

从表单中选中某个记录值。

desc/describe 表单名称;

描述表单/查看表结构

select concat(round(sum(DATA_LENGTH/1024/1024),2),‘MB’) as data from TABLES;

查看所有库的大小

select concat(round(sum(DATA_LENGTH/1024/1024),2),‘MB’) as data from TABLES where table_schema=‘testdb’;

查看指定库的大小

select concat(round(sum(DATA_LENGTH/1024/1024),2),‘MB’) as data from TABLES where table_schema=‘testdb’ and table_name=‘testtable’;

查看指定库的指定表的大小

SELECT table_name AS “Tables” round(((data_length + index_length)/1024/1024),2) “Size in MB” FROM information_schema.TABLES WHERE table_schema = ‘zabbix’ ORDER BY (data_length + index_length) DESC;

统计数据库中每个表所占的空间

三、数据库备份:

mysqldump [options] db_name [tb1][tb2]…

备份单个库或库中的指定表

mysqldump [options] --databases[options] DB1 [DB2 DB3…] > /backups/bak.sql

备份多个数据库

mysqldump --databases testdb --lock-all-tables > /backups/testdb.sql

最好也加上–databases,真正的备份方式

mysqldump [options] --all-databases [options] > /backups/all.sql

备份所有数据库

mysqldump [options] testdb < /backups/testdb.sql

恢复数据

四、案例:

清理zabbix一周之前的历史数据:

!/bin/bash

User=“zabbix”

Passwd=“zabbix”

Date=date -d $(date -d "-7 day" +%Y%m%d) +%s #取7天之前的时间戳

( w h i c h m y s q l ) u (which mysql) -u {User} -p${Passwd} -e "

use zabbixdb;

DELETE FROM history WHERE ‘clock’ < $Date;

optimize table history;

DELETE FROM history_str WHERE ‘clock’ < $Date;

optimize table history_str;

DELETE FROM history_uint WHERE ‘clock’ < $Date;

optimize table history_uint;

DELETE FROM history_text WHERE ‘clock’ < $Date;

optimize table history_text;

DELETE FROM trends WHERE ‘clock’ < $Date;

optimize table trends;

DELETE FROM trends_uint WHERE ‘clock’ < $Date;

optimize table trends_uint;

DELETE FROM events WHERE ‘clock’ < $Date;

optimize table events;

  大连人流手术费用 http://www.120dlbh.com/

  大连看妇科哪里好 http://www.0411bh.com/

猜你喜欢

转载自blog.csdn.net/qq_42894764/article/details/89414873