0
点赞
收藏
分享

微信扫一扫

mysql 常用命令总结

覃榜言 2023-06-07 阅读 36

mysql 常用命令总结

1、修改mysql密码

mysqladmin -u用户名 -p旧密码 password 新密码

命令行 set password for root=PASSWORD("root");

2、增加mysql新用户并授权:

create user 'test'@'localhost' identified by 'test'

grant  select,update,delete,insert on 库名.* 用户名@登录主机 identified by "密码";#给数据库查询、修改、插入、删除的权限

flush privileges;

grant all privileges on db.* to 'test'@'locahost';

flush privileges;

REVOKE INSERT ON DB.* from 'test'@'localhost' #回收权限

3、给表添加索引

alter table test  add index index_name(name);

create index index_name on  table(name);

创建唯一索引

create uniq index index_id_name on test(id);

4、查询表前两行数据

select * from test limit 2;  #查询表的前两行

select * from test limit 1,2; #从第一个之后查询两个,不包括第二个

5、修改字段类型,名称

alter table test modify test char(4) after name;

alter table test change age test1 char(4) after name;

6、容器外执行mysql命令

docker exec -it container /bin/bash -c "mysql 命令"

7、mysql备份与恢复

mysqldump -uroot -p密码 db >/tmp/1.sql  # 备份重定向一个文件

mysql -uroot -p密码 db </tmp/1.sql  #反向重定向,db必须真实存在

8、创建数据库并指定字符集

CREATE DATABASE `test_table` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;

举报

相关推荐

0 条评论