背景:日常与Mysql打交道的话,需要用到一些基本运维操作。在此进行学习和记录。
包含内容
- 安装Mysql
- Mysql服务相关(查看状态、重启等)
- Mysql用户管理
- Mysql配置文件 my.cnf
安装Mysql
背景:CentOS 7,安装Mysql 8
参考步骤:https://www.sjkjc.com/mysql/install-on-centos/
下载Mysql YUM仓库
1
2
|
wget https://repo.mysql.com/mysql80-community-release-el7-1.noarch.rpm
yum localinstall mysql80-community-release-el7-1.noarch.rpm
|
安装
1
|
yum install mysql-community-server -y
|
如果安装有这个报错,则说明MySQL GPG 密钥已过期:
1
2
|
Failing package is: mysql-community-server-8.0.34-1.el7.x86_64
GPG Keys are configured as: file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
|
解决方案:
1
2
|
rpm --import https://repo.mysql.com/RPM-GPG-KEY-mysql-2022
# 然后再次执行Mysql安装命令
|
启动Mysql服务
1
2
3
|
systemctl start mysqld
# 设置为开机启动
systemctl enable mysqld
|
查看数据库root密码
安装 MySQL 8.0 时,会自动为 root 用户生成一个临时密码,并记录在日志文件里:
1
2
|
[root@iZbp15qc4wmx335c268l5mZ ~]# grep "A temporary password" /var/log/mysqld.log
2023-07-30T07:35:46.519399Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: klcASenOk5!a
|
Mysql安全配置
执行以下命令来保护Mysql服务器:
1
2
3
4
5
6
7
8
|
# 会要求输入root的初始密码,并要求修改密码
[root@iZbp15qc4wmx335c268l5mZ ~]# mysql_secure_installation
Securing the MySQL server deployment.
Enter password for user root:
The existing password for the user account root has expired. Please set a new password.
New password:
# 后面会让确认一些安全策略,视情况可以都填y
|
连接数据库
1
2
3
4
5
6
7
8
9
10
11
12
13
|
mysql -u root -p
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)
|
数据库安装完成!
查看Mysql服务状态
Mysql用户管理
Mysql新增用户
1
|
create user user_name identified by password;
|
查看所有用户
1
|
select user from mysql.user;
|
给用户赋权
1
2
3
4
5
|
# 查看现有权限
show grants for user_name;
# 赋予给用户所有权限
GRANT ALL PRIVILEGES ON *.* TO user_name;
FLUSH PRIVILEGES;
|
my.cnf
my.cnf是Mysql的配置文件。
my.cnf位置
1
2
3
|
> mysql --help | grep 'my.cnf'
order of preference, my.cnf, $MYSQL_TCP_PORT,
/etc/my.cnf /etc/mysql/my.cnf /usr/etc/my.cnf ~/.my.cnf
|
下面这行列出了Mysql读取配置文件的优先顺序,当前面的文件不存在时,会继续尝试读取下一个