微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

mysql常用操作命令

###########mysql常用操作命令#############

1.安装MysqL
yum install MysqL MysqL-server
 /etc/init.d/MysqLd start    ##开启MysqLd服务

2.设置及登录
MysqL_secure_installation        ##第一次安装MysqL以后通过这条命令可以对MysqL进行初始设置
MysqL -uroot -predhat            ##从本机登录MysqL数据库(ps -aux|grep MysqL  kill -9 )
MysqLadmin -uroot -predhat password westos         ##修改本地MysqL,root密码
MysqLadmin -uroot -predhat -h 172.25.8.1 password westos ##修改远程172.25.8.1MysqL服务器,root密码

3.操作命令
库操作:
show databases;                ##显示数据库
use MysqL;                    ##进入数据库(按回车键出现Database changed时说明操作成功!)
show tables;                ##显示数据库中的表
desc user;                    ##查看user表的结构
flush privileges;            ##刷新数据库信息
select host,user,password from user;    ##查询user表中的host,user,password字段
create database westos;                    ##创建westos数据库
use westos;                        ##进入数据库westos

表操作:
create table linux(            ##创建表,表名linux,字段username,password
username varchar(15) not null,
password varchar(15) not null);

select * from MysqL.user;        ##查询MysqL库下的user表中的所有内容

alter table linux add age varchar(4);    ##添加age字段到linux表中

desc linux;                ##查看linux表结构

ALTER TABLE linux DROP age        ##删除linux表中的age字段

ALTER TABLE linux ADD age  VARCHAR(4)  AFTER username ##在linux表username字段后添加字段age

desc linux;                ##查看linux表结构

insert into linux values ('user1',18,'passwd1'); ##在linux表中插入username=user1,age=18,password=password1
update linux set password='passwd2' where username="user1";    ##更新linux表中user1的密码为password2
delete from linux where username='user1';         ##删除linux表中user1的所有内容
select * from linux;   ##可以进行查看

用户管理:
CREATE USER hjy@localhost identified by 'westos';    ##创建本地用户hjy并添加密码westos,认密码是加密的
CREATE USER hee@'%' identified by 'redhat';        ##创建用户hee,%表示这个账户可以在任何主机登录
select host,User,Password from user;            ##查询user表中的host,user,password字段

grant select on  *.* to user1@localhost identified by 'passwd1'; ##授权user1,密码为passwd1,并且只能在本地查询数据库的所在内容
grant all on MysqL.* to user2@'%' identified by 'passwd2';     ##授权user2,密码为passwd2,可以从远程任意主机登录MysqL并且可以对MysqL数据库任意操作(%改为ip可指定此ip登录)

FLUSH PRIVILEGES;                        ##重载授权表
SHOW GRANTS FOR hjy@localhost;                    ##查看用户授权
REVOKE DELETE,UPDATE,INSERT on MysqL.* from hjy@localhost;    ##撤销用户MysqL的DELETE,UPDATE,INSERT权限
REVOKE all on MysqL.* from hjy@localhost;            ##撤销用户所有权限
DROP USER hjy@localhost;                    ##删除用户


备份
/var/lib/MysqL
MysqLdump -uroot -predhat --all-databases    ##命令备份所有数据
MysqLdump -uroot -predhat MysqL > MysqL.bak    ##备份MysqL库导到MysqL.bak
MysqL -uroot -predhat -e "create database westos;"  ##创建一个数据库
MysqL -uroot -predhat westos < MysqL.bak    ##恢复MysqL.bak到westos库

密码恢复
/etc/init.d/MysqLd stop
MysqLd_safe --skip-grant-tables &          ##跳过grant-tables授权表,不需要认证登录本地MysqL数据库
update MysqL.user set password=password('westos') where user='root';    ##更新MysqL.user表中条件root用户的密码为westos
/etc/init.d/MysqL restart    ##重新启动nysql


版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐