mysql基本用法

修改密码

use mysql;
select user, host, authentication_string, plugin from user;
update user set host='%' where user='root';
update user set plugin='mysql_native_password' where user='root';

# mysql5.7之前修改密码
update user set password=password("root") where user="root";

# mysql5.7之后(包括5.7)修改密码方法一
update user set authentication_string=password('root') where user='root';
# mysql5.7之后(包括5.7)修改密码方法二
alter user 'root'@'localhost' identified by 'root';
alter user USER() identified by 'root';
alter user 'root'@'%' identified by 'root';
# mysql5.7之后(包括5.7)修改密码方法三
alter user 'root'@'%' identified with mysql_native_password by 'root';

# 添加用户
CREATE USER 'newuser'@'localhost' IDENTIFIED BY 'password';
GRANT ALL PRIVILEGES ON *.* TO 'newuser'@'localhost';

flush privileges;

mysql使用

  • 授予所有权限
grant all privileges on databasename.tablename to 'user'@'host';
FLUSH PRIVILEGES;
  • 收回所有权限
revoke all privileges on databasename.tablename from 'user'@'host';
FLUSH PRIVILEGES;
  • 授予查询权限
grant select on databasename.tablename to 'user'@'host';
FLUSH PRIVILEGES;
  • 收回查询权限
revoke select on databasename.tablename from 'user'@'host';
FLUSH PRIVILEGES;
  • 建立新用户
create user user@host identified by 'password';
FLUSH PRIVILEGES;
  • 删除用户
drop user 'user'@'host';
  • 创建数据库并指定字符集
CREATE DATABASE IF NOT EXISTS databasename DEFAULT CHARSET utf8 COLLATE utf8_general_ci;
create database dbName default character set utf8mb4 collate utf8mb4_unicode_ci;
  • 删除数据库
drop database databasename;
  • 创建表
use databasename;
create table tbl_emp(
    emp_id int(11) auto_increment not null primary key,
    emp_name varchar(255) not null,
    gender char(1),
    email varchar(255),
    d_id int(11)
);

create table tbl_dept(
    dept_id int(11) auto_increment not null primary key,
    dept_name varchar(255) not null,
    foreign key(dept_id) references tbl_emp(d_id)
);
  • 删除表
use databasename;
drop table tablename;
update db1.table1 a inner join db2.table2 b on a.col1=b.col2 set a.col3=''

一些问题

mysql.user.plugin
  • auth_socket:root用户可能是这种plugin,导致本地连接mysql的root用户需要sudo,需要更改为mysql_native_password
mysql.user.password与mysql.user.authentication_string
  • 在 MySQL5.7 中 user 表的 password 已换成了authentication_string。
  • password() 加密函数已经在 8.0.11 中移除了,可以使用 MD5() 函数代替。
  • 修改密码后需要执行 FLUSH PRIVILEGES 语句。 这个命令执行后会重新载入授权表
bind-address=127.0.0.1导致无法远程连接
  • /etc/mysql/mysql.conf.d/mysqld.cnf的该句注释掉就可以远程连接了
mysql查看编码
show variables like 'character_set%';
mysql导出
mysqldump -uroot -proot -BdbName > 1.sql
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值