mysql之基础操作

前端之家收集整理的这篇文章主要介绍了mysql之基础操作前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

一、相关信息

服务:MysqLd     
端口:3306   
主配置文件:/etc/my.cnf   
初始化脚本:MysqL_install_db  
启动命令:MysqLd_safe   
数据目录 :/var/lib/MysqL 
套接文件:/var/lib/MysqL/MysqL.sock
当意外关闭@R_301_457@时,再开启时假如开启不了,找到这个,删除再启动 进程文件:/var/run/MysqLd/MysqLd.pid
设置密码:MysqLadmin -uroot  password ‘123456’  
登录MysqL  -u 用户名 -p 密码 -P 端口 -S 套接文件 
-p 用户密码   
-h 登陆位置(主机名或 ip 地址) 
-P 端口号(3306 改了就不是了)
-S 套接文件(/var/lib/MysqL/MysqL.sock) 
退出命令:exit 或 ctrl+d

二、基本使用

1、用户管理
创建用户
MysqL> create user love2@'%' identified by '123456';

查看权限,此时用户还没有任何权限。
MysqL> show grants for love2@'%';
+-------------------------------------------------------------------+
| Grants for love2@%                                                |
+-------------------------------------------------------------------+
| GRANT USAGE ON *.* TO 'love2'@'%' IDENTIFIED BY PASSWORD <secret> |
+-------------------------------------------------------------------+

为用户授予权限。
MysqL> grant all on *.* to love2@'%' ;
MysqL> grant select,alter,on *.* to love2@'%' ; #授予love2用户对所有@R_301_457@和数据表,查询修改表权限。

取消love2用户所有权限
MysqL> revoke all on *.* from love2@'%';

用户为自己更改密码 
MysqL> set password=password('111111');

root用户更改他人密码
MysqL> set password for love2@'%' =password('111111');

删除用户
delete from user where user='love2'
由于delete只会删除user表中的内容,而授权表中的内容不会被清除,我们需要刷新授权表
flush privileges

删除用户的第二种方法
drop user 'love2',默认删除用户 love2@'%'

2、忘记root密码。
root 找回自己的密码并修改 
1、关闭@R_301_457@
service MysqLd stop
修改配置文件(/etc/my.cnf)添加:skip-grant-tables 
2、# vim /etc/my.cnf 
skip-grant-tables 
3、启动@R_301_457@,空密码登录修改密码 
MysqL> update MysqL.user set password=password('123456') where user='root';
4、删除 skip-grant-tables,重启@R_301_457@验证新密码
3、@R_301_457@操作
MysqL>create database web;
MysqL>show databases;
4、简单数据表操作
MysqL> create database info;
#创建@R_301_457@
MysqL> use info;
#选择要使用的@R_301_457@ 
MysqL> create table student1 (id int,name char(8),age char(1));
Query OK,0 rows affected (0.07 sec)
#创建 info 表,并添加 id 和 name 字段以及类型 
MysqL> desc student1; #查看表结构
+-------+---------+------+-----+---------+-------+
| Field | Type    | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| id    | int(11) | YES  |     | NULL    |       |
| name  | char(8) | YES  |     | NULL    |       |
| age   | char(1) | YES  |     | NULL    |       |
+-------+---------+------+-----+---------+-------+
3 rows in set (0.00 sec)
4、复杂一点的表
MysqL> create table student2 (
    -> id int unsigned not null auto_increment,#字段要求为正数、且自增长、主键
    -> name char(8) not null default '',#字符型长度 30 字节,默认值为空格
    -> age int not null default 0,#字段默认值为 0
    -> primary key (id));  #设置 id 为主键
5、数据插入
MysqL> insert into student2 (id,name,age) values (1,'zs',12); #指明插入字段和数据
MysqL> insert into student2 values (2,'ls',13); 按顺序插入
MysqL> insert into student2 values (3,'ww',14),(4,'zl',15); #多个插入

将students2的数据插入students1
MysqL> insert into student1 select * from student2;
Query OK,4 rows affected,4 warnings (0.03 sec)
Records: 4  Duplicates: 0  Warnings: 4
6、删除@R_301_457@
MysqL>drop database abc; 
MysqL>show databases;
7、删除数据表
MysqL>drop table a1; 
MysqL>show table;
8、删除表里的数据记录
MysqL>delete from student2 where id=5;      
#删除 id=5 的记录 
MysqL> delete from student2 where age between 23 and 25;  
#删除年龄在 23-25 之间的
9、修改表数据
MysqL> update student2 set age=21 where id=3;
10、修改表名
MysqL>alter table student1 rename student5;
11、表字段修改
1)简单修改表内字段
MysqL> alter table student5  modify age char(4) not null;

MysqL> desc student5;
+-------+---------+------+-----+---------+-------+
| Field | Type    | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| id    | int(11) | YES  |     | NULL    |       |
| name  | char(8) | YES  |     | NULL    |       |
| age   | char(4) | NO   |     | NULL    |       |
+-------+---------+------+-----+---------+-------+

2)修改数据表的字段类型详情名字
MysqL> alter table student5 change age gender char(1) not null default '男';

MysqL> desc student5;
+--------+---------+------+-----+---------+-------+
| Field  | Type    | Null | Key | Default | Extra |
+--------+---------+------+-----+---------+-------+
| id     | int(11) | YES  |     | NULL    |       |
| name   | char(8) | YES  |     | NULL    |       |
| gender | char(1) | NO   |     | 男      |       |
+--------+---------+------+-----+---------+-------+

3)添加字段
#默认添加
alter table  student5 add age int not null;

#添加字段到第一列 
alter table student5 add birthday year first;

#添加到指定字段后
MysqL>alter table student5 add class int default 1 after id;  

MysqL> desc student5;
+----------+---------+------+-----+---------+-------+
| Field    | Type    | Null | Key | Default | Extra |
+----------+---------+------+-----+---------+-------+
| birthday | year(4) | YES  |     | NULL    |       |
| id       | int(11) | YES  |     | NULL    |       |
| class    | int(11) | YES  |     | 1       |       |
| name     | char(8) | YES  |     | NULL    |       |
| gender   | char(1) | NO   |     | 男      |       |
| age      | int(11) | NO   |     | NULL    |       |
+----------+---------+------+-----+---------+-------+

4)删除字段

alter table student5 drop birthday ;

三、备份和还原

1、MysqLdump
MysqLdump -u 用户名 -p @R_301_457@名  > /备份路径/备份文件名(备份整个@R_301_457@)

MysqLdump  -u root -p123456 info > /root/info.dump (备份info@R_301_457@)

MysqLdump -u 用户名 -p @R_301_457@名 表名 > /备份路径/备份文件名(备份数据表)  
备份多个库:--databases 库 1,库 2  
备份所有库:--all-databases 
[root@centos ~]# MysqLdump -uroot -p123456  --events --all-databases > /root/all.dump
还原:MysqL @R_301_457@ < 备份文件
注意:
还原时,若导入的是某表,请指定导入到哪一个库中。
MysqL -uroot -p123456 info < info.dump
2、MysqLhotcopy 备份

类似于我们自己手动将/usr/local/MysqL/data/下的@R_301_457@文件使用cp备份.但是我们自己手动备份,不会将变化写入日志。

备份:
MysqLhotcopy --flushlog  -u='用户’ -p=’密码’ --regexp=正则  备份目录

备份info@R_301_457@到 /tmp/name/目录下
MysqLhotcopy -u root -p 123456  info /tmp/name/

还原:cp -a 备份目录 数据目录(yum安装是这个目录/var/lib/MysqL)
cp -a /tmp/name/* /usr/local/MysqL/data/ (这个是编译安装时,我们设置的目录)
3、二进制日志(log-bin 日志):

所有对@R_301_457@状态更改的操作(create、drop、update 等,都会被记录。
1)修改 my.cnf 配置文件开启 binlog 日志记录功能

# vim /etc/my.cnf 
log-bin=MysqL-bin  #启动二进制日志,默认应该是开启的

2)查看binlog日志。

[root@centos data]# ls /usr/local/MysqL/data/
centos.err          ib_logfile1         MysqL-bin.000002    test/
centos.pid          info/               MysqL-bin.000003    
ibdata1             MysqL/              MysqL-bin.index     
ib_logfile0         MysqL-bin.000001    performance_schema/ 

MysqL-bin.*文件 为二进制日志文件,使用 MysqLbinlog 命令查看
MysqLbinlog MysqL-bin.000005

3)日志还原
按时间还原: --start-datetime --stop-datetime
格式:MysqLbinlog --start-datetime ‘YY-MM-DD HH:MM:SS’ --stop-datetime ‘YY-MM-DD HH:MM:SS’ 二进制日志 | MysqL -uroot -p

示例
MysqLbinlog  --start-datetime ‘19-02-04 12:51:36’ --stop-datetime ‘19-12-04 13:30:36’ MysqL-bin.000003 | MysqL -uroot -p

文件大小还原: --start-position --stop-position,默认大小为4
格式:MysqLbinlog --start-position 数值 stop-position 数值 二进制日志 | MysqL -uroot -p

 MysqLbinlog --start-position 4  --stop-position  565672 MysqL-bin.000003 | MysqL -uroot -p

猜你在找的MySQL相关文章