linux连接mysql常用命令

151 阅读2分钟
//连接mysql:
mysql -h 127.0.0.1 -P 3306 -u root -proot

//docker连接本地mysql:
docker exec -it root bash;
mysql -uroot -proot;

//docker操作自己mysql部分操作
//启动mysql容器
docker run -di --name=root -p 3306:3306 -e MYSQL_ROOT_PASSWORD=root mysql:5.6
//进入mysql容器
docker exec -it root bash;
mysql -uroot -proot;

//创建root用户:
CREATE USER 'root'@'%' IDENTIFIED BY 'root';
SET PASSWORD = PASSWORD('root');
//修改密码:
update user set authentication_string=password('root') where user='root';
// 修改
ALTER USER'root'@'%' IDENTIFIED WITH mysql_native_password BY 'root';

//you password has expired to log in you must change it using a client
// 第一次当前用户密码
 SET PASSWORD = PASSWORD('root');
 //设置永不超时,我的5.6版本没有=-=
 ALTER USER 'root' PASSWORD EXPIRE NEVER;
 
 //创建admin用户允许所有ip连接(用通配符%表示)
 create user 'admin'@'%' identified by 'admin';
 //只允许指定ip连接
create user '新用户名'@'localhost' identified by '密码';
//允许所有ip连接(用通配符%表示)
create user '新用户名'@'%' identified by '密码';
 
 //设置权限
grant all privileges on 数据库名.表名 to '新用户名'@'指定ip' identified by '新用户密码' ;
//示例
//允许访问所有数据库下的所有表
grant all privileges on *.* to '新用户名'@'指定ip' identified by '新用户密码' ;
//指定数据库下的指定表
grant all privileges on test.test to '新用户名'@'指定ip' identified by '新用户密码' ;
 
 //设置用户拥有所有权限也就是管理员
grant all privileges on *.* to '新用户名'@'指定ip' identified by '新用户密码' WITH GRANT OPTION;
//拥有查询权限
grant select on *.* to '新用户名'@'指定ip' identified by '新用户密码' WITH GRANT OPTION;
//其它操作权限说明,select查询 insert插入 delete删除 update修改
//设置用户拥有查询插入的权限
grant select,insert on *.* to '新用户名'@'指定ip' identified by '新用户密码' WITH GRANT OPTION;
//取消用户查询的查询权限
REVOKE select ON what FROM '新用户名';

// 删除用户
DROP USER username@localhost;

//刷新当前配置
FLUSH PRIVILEGES;