1.2 linux下操作mysql(mysql V8.0.29)

#创建数据库demo,赋权给用户(clannad,密码123456)
use mysql;

drop database if EXISTS demo;
create database demo CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;

create user 'clannad'@'%' identified by '123456';
flush privileges;

#赋权
grant all privileges  on demo .* to `clannad`@`%`;
flush privileges;

从8.0后,MySQL也将会在某个版本中开始使用UTF-8MB4作为默认的字符编码。
所以简单说即是:UTF-8MB4才是MySQL中真正的UTF-8编码。
UTF8MB4可以让MySQL存储 Emoji表情

0.登陆

./mysql -u root -p
>输入密码
use mysql;                           #数据库mysql
show databases;                      #查看所有数据库
select * from mysql.user;            #查看所有用户
select u.host,u.user from user u;
flush privileges;                   #刷新权限

1.创建数据库,用户,赋权限

use mysql;
#创建`test`数据库
create database test;

#创建用户为clannad,密码为Hel10,`host`不限制的用户
create user 'clannad'@'%' identified by 'Hel10'; 
flush privileges;

#给已有用户赋权(授予`clannad`用户操作`test`数据库的所有权限,可使用所有权限)
grant all privileges  on test.* to `clannad`@`%`;
flush privileges;
#↓↓↓↓↓↓ 
grant all privileges  on test.* to `clannad`@`%`;
#指定Ip能够访问数据库
update user set host='xxx.xxx.xxx.xxx' where user='clannad';
flush privileges;  

2.忘记密码,修改用户密码(linux版)

(1).关停服务
#查看mysql状态
service mysqld status
#关停
service mysqld stop
(2).跳过账号密码验证
#进入mysql目录下
cd /usr/local/mysql/bin
#跳过验证启动mysql
./mysqld_safe --skip-grant-tables &
[root@VM-16-15-centos bin]# ./mysqld_safe --skip-grant-tables &
[1] 3668774
[root@VM-16-15-centos bin]# 2022-06-15T08:23:22.792936Z mysqld_safe Logging to '/data/mysql/mysql.err'.
2022-06-15T08:23:22.820213Z mysqld_safe Starting mysqld daemon with databases from /data/mysql
(3). 另开一个linux窗口(非常重要)
#进入mysql目录下
cd /usr/local/mysql/bin
#无密码登陆
./mysql -u root
#(此时应进入数据库)
use mysql;
#刷新权限(这个非常重要,不然修改用户密码会报错)
flush privileges;
#修改密码
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '1234';
flush privileges;
(4). 重启服务
service mysqld restart
(5). 验证
cd /usr/local/mysql/bin
./mysql -uroot -p
输入密码:1234
#或者直接跟密码
./mysql -uroot -p1234

以下为未刷新权限执行更新语句报错

mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '1234';
ERROR 1290 (HY000): The MySQL server is running with the --skip-grant-tables option so it cannot 

不知道service mysqld restart的可以移步linux下mysql安装

Tip1 使用grant all privileges on *.* to "root"@"%" identified by "xxxx";报错

grant all privileges on 数据库名称.* to '用户'@'host授权范围' identified by '密码';

host授权范围: %为全网,localhost代表本机

ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that
corresponds to your >MySQL server version for the right syntax to use near 
'identified by xxxxx'

#↓↓↓↓↓↓ 解决方法: 分布执行
查看数据库版本( SELECT @@VERSION),高版本数据库不能按照grant all privileges on *.* to 
"root"@"%"  identified by "xxxx";去修改用户权限

create user 'root'@'%' identified by  'password';
grant all privileges on *.* to 'root'@'%' with grant option;
flush privileges;

Tip2 以下是常用权限的简短列表

ALL–允许完全访问特定的数据库。如果未指定数据库,则允许完全访问整个MySQL。
CREATE–允许用户创建数据库和表。
DELETE–允许用户从表中删除行。
DROP–允许用户删除数据库和表。
EXECUTE–允许用户执行存储的例程。
GRANT OPTION–允许用户授予或删除另一用户的特权。
INSERT–允许用户从表中插入行。
SELECT–允许用户从数据库中选择数据。
SHOW DATABASES–允许用户查看所有数据库的列表。
UPDATE–允许用户更新表中的行。

你可能感兴趣的:(1.2 linux下操作mysql(mysql V8.0.29))