MySQL数据库安装
2025年9月13日大约 4 分钟MySQL笔记
概述
MySQL是一种开放源代码的关系型数据库管理系(RDBMS),使用最常用的数据库管理语言--结构化查询语言(SQL)进行数据库管理,于1998年一月发行第一个版本。
PS: MySQL官网下载相对应的软件包。
准备工作
安装方式有:
- 二进制安装:
- yum安装:
- docker安装:
# 列出所有版本
[root@i-xrjnk70d ~]# yum repolist all | grep mysql
# 安装 yum 配置工具
[root@i-xrjnk70d ~]# yum -y install yum-utils
# 禁用 8.0 版本
[root@i-xrjnk70d ~]# yum-config-manager --disable mysql80-community
# 启用 5.7 版本
[root@i-xrjnk70d ~]# yum-config-manager --enable mysql57-community
# 检查启用版本 注意:进行安装时请确保只有一个版本启用,否则会显示版本冲突
[root@i-xrjnk70d ~]# yum repolist enabled | grep mysql
正式安装 MySQL
- 需要安装MySQL Server, MySQL Client 已经包括在
server
套件内
[root@i-xrjnk70d ~]# yum -y install mysql-community-server mysql # 安装服务端,客户端
[root@i-xrjnk70d ~]# systemctl start mysqld # 启动 mysql 服务
[root@i-xrjnk70d ~]# systemctl enable mysqld # 设置 mysql 服务开机启动
[root@i-xrjnk70d ~]# ls /var/lib/mysql # 查看 mysql 安装
[root@i-xrjnk70d ~]# grep 'tqfeduorary password' /var/log/mysqld.log # 获取首次登录密码
[root@i-xrjnk70d ~]# mysql -uroot -p'XXX>!xxxxxxxx' # 登录 mysql 数据库
mysql > alter user 'root'@'localhost' identified by 'Dev$123456'; # 修改 mysql 数据库密码(密码必须符合复杂性要求,包含字母大小写,数字,特赦符号,长度不少于8位)
[root@i-xrjnk70d ~]# mysql -uroot -p'Dev$123456' # 用新密码登录数据库
- 重启/停止 MySQL
[root@i-xrjnk70d ~]# systemctl stop mysqld
[root@i-xrjnk70d ~]# systemctl restart mysqld
MySQL用户管理
创建用户
create user
语句创建:create user '用户名'@'IP地址' identified by '密码'
;grant
语句创建:- 分配所有的权限:
grant 权限 on 数据库.数据表 to '用户' @ '主机名'
- 精准的控制用户的权限:
grant 权限 on 数据库.数据表 to '用户' @ '主机名'
;
- 分配所有的权限:
创建实例
# create user 用户名 identified by '密码'; 新创建的用户,默认情况下是没有任何权限的。
create user test identified by '123456';
# 用户名部分为“test”,主机名默认为“%”(即对所有主机开放权限) 如果指定用户登录不需要密码,则可以省略identified BY部分
create user 'test'@'localhost' identified by '123456';
create user 'test'@'192.168.1.101' identified by '123456';
create user 'test'@'192.168.1.%' identified by '123456';
create user 'test'@'%' identified by '123456';
# grant 示例
grant all on *.* to 'test3'@’localhost’ identified by '123456';
删除用户
DROP USER
删除:DROP USER '用户名'@'IP地址'
;DELETE
语句删除:DELETE FROM mysql.user WHERE user='用户名' AND host='IP地址'
;
# drop 示例
DROP USER 'user1'@’localhost’;
# delete 示例
DELETE FROM mysql.user WHERE user=’user2’ AND host=’localhost’;
修改用户
# 语法
RENAME USER '旧用户名'@'IP地址' TO '新用户名'@'IP地址';
# 示例
RENAME USER 'old_user'@‘localhost’ TO 'new_user'@'localhost';
修改密码
PS:修改完密码必须刷新权限
FLUSH PRIVILEGES;
root用户修改自己密码
:- 方法一:
mysqladmin -uroot -p123 password 'new_password' # 123为旧密码;
- 方法二:
alter user 'root'@'localhost' identified by 'new_pssword';
- 方法三:
SET PASSWORD = password('new_password');
- 方法一:
root 修改其他用户密码
:- 方法一:
alter user 'qfedu'@'localhost' identified by 'Qfedu.1234com';
- 方法二:
GRANT SELECT ON *.* TO 用户名@'ip地址' IDENTIFIED BY 'yuan';
- 方法一:
- 普通用户修改自己密码:
SET password = password('new_password');
;
找回 root 密码
- 1.修改MySQL配置文件: 在[mysqld]下面加上 skip-grant-tables
[root@i-xrjnk70d ~]# vim /etc/my.cnf
[mysqld]
···
#设置免密登录
skip-grant-tables
2.重启 MySQL:
systemctl restart mysqld
3.修改密码:
# 终端输入 mysql 直接登录 MySQL数据库
[root@i-xrjnk70d ~]# mysql
# 切换到 MySQL 系统库 mysql
mysql> use mysql;
# 设置密码
mysql> update user set authentication_string=password('密码') where user='root';
- 4.注释掉免密登录
[root@i-xrjnk70d ~]# vim /etc/my.cnf
[mysqld]
···
#设置免密登录
#skip-grant-tables
- 5.重启 MySQL 然后登录
[root@i-xrjnk70d ~]# systemctl restart mysqld
[root@i-xrjnk70d ~]# mysql -uroot -p
MySQL用户授权
用户授权
[root@i-xrjnk70d ~]# mysql -u root -p
Enter password:
mysql> grant all privileges on *.* to 'test'@'%' identified by 'test$123456' with grant option;
all privileges
:表示将所有权限授予给用户。也可指定具体的权限,如:SELECT、CREATE、DROP等。on
:表示这些权限对哪些数据库和表生效,格式:数据库名.表名,这里写“*”表示所有数据库,所有表。如果我要指定将权限应用到test库的user表中,可以这么写:test.userto
:将权限授予哪个用户。格式:”用户名”@”登录IP或域名”。%表示没有限制,在任何主机都可以登录。比如:”test”@”192.168.0.%”,表示test这个用户只能在192.168.0IP段登录identified by
:指定用户的登录密码with grant option
:表示允许用户将自己的权限授权给其它用户
用户详情的权限列表请参考MySQL官网说明
刷新权限
对用户做了权限变更之后,记得重新加载一下权限,将权限信息从内存中写入数据库。
mysql> flush privileges;
查看用户权限
mysql> grant select,create,drop,update,alter on *.* to 'test'@'localhost' identified by 'test$123456' with grant option;
mysql> show grants for 'test'@'localhost';
回收权限
语法:revoke 权限1,权限2..on 数据库名.* from username@ip;
删除test这个用户的create权限,该用户将不能创建数据库和表。
mysql> revoke create on *.* from 'test@localhost';
mysql> flush privileges;