MySQL部署

本篇文章分别讲解MySQL (5.6、5.7、8.0)版本的安装

MySQL 5.6安装

1)安装MySQL5.6数据源

[root@k8s-master01 ~]# wget http://repo.mysql.com/mysql-community-release-el7-5.noarch.rpm
[root@k8s-master01 ~]# rpm -ivh mysql-community-release-el7-5.noarch.rpm
[root@k8s-master01 ~]# yum install mysql-server -y

2)MySQL默认配置参数解释

[root@k8s-master01 ~]# cat /etc/my.cnf	# MySQL默认配置文件路径
[mysqld]
datadir=/var/lib/mysql				# MySQL数据存储路径
socket=/var/lib/mysql/mysql.sock	# 设置socke文件所在目录
symbolic-links=0					# 符号连接,如果设置为1,则mysql数据库和表里的数据支持储存在datadir目录之外的路径下,默认都是0(较新版本的mysql下默认是1);
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES		 # mysql数据库的中有一个环境变量sql_mode,定义了mysql应该支持的sql语法,数据校验等!我们可以通过以下方式查看当前数据库使用的sql_mode
[mysqld_safe]
log-error=/var/log/mysqld.log			# 日志文件存储路径
pid-file=/var/run/mysqld/mysqld.pid		# PID文件存储路径

3)启用MySQL服务并设置密码

[root@k8s-master01 ~]# systemctl start mysqld
[root@k8s-master01 ~]# netstat -anltp | grep mysqld
tcp6       0      0 :::3306                 :::*                    LISTEN      19479/mysqld        
[root@k8s-master01 ~]# mysqladmin -uroot password "123456"		# 给MySQL设置一个密码为123456
[root@k8s-master01 ~]# mysql -uroot -p123456
Warning: Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.6.51 MySQL Community Server (GPL)

Copyright (c) 2000, 2021, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
+--------------------+
3 rows in set (0.00 sec)

MySQL 5.7安装

1)在安装之前先将系统已经安装的MySQL清理掉

[root@k8s-master01 ~]# > /var/log/mysqld.log # 清理掉5.6生成的日志
[root@k8s-master01 ~]# rm -rf /var/lib/mysql
[root@k8s-master01 ~]# yum remove mysql-server -y
[root@k8s-master01 ~]# rpm -qa | grep mysql # 如果没有查到就不需要删除,直接进行下一步安装数据源
mysql-community-release-el7-5.noarch
mysql-community-libs-5.6.51-2.el7.x86_64
mysql-community-client-5.6.51-2.el7.x86_64
mysql-community-common-5.6.51-2.el7.x86_64
[root@k8s-master01 ~]# rpm -e --nodeps mysql-community-release-el7-5.noarch
[root@k8s-master01 ~]# rpm -e --nodeps mysql-community-libs-5.6.51-2.el7.x86_64
[root@k8s-master01 ~]# rpm -e --nodeps mysql-community-client-5.6.51-2.el7.x86_64
[root@k8s-master01 ~]# rpm -e --nodeps mysql-community-common-5.6.51-2.el7.x86_64

2)安装MySQL 5.7数据源

[root@k8s-master01 ~]# wget http://dev.mysql.com/get/mysql57-community-release-el7-10.noarch.rpm
[root@k8s-master01 ~]# rpm -ivh mysql57-community-release-el7-10.noarch.rpm

3)安装MySQL 5.7

[root@k8s-master01 ~]# yum install mysql-server -y

4)启用MySQL并设置数据库密码

[root@k8s-master01 ~]# systemctl start mysqld
[root@k8s-master01 ~]# netstat -anltp | grep mysqld
tcp6       0      0 :::3306                 :::*                    LISTEN      24381/mysqld      
[root@k8s-master01 ~]# cat /var/log/mysqld.log | grep "password is generated" 
2021-01-26T03:30:23.807721Z 1 [Note] A temporary password is generated for root@localhost: As4sjPF;82ka
[root@k8s-master01 ~]# mysql -uroot -p
Enter password: 		# 输入从日志查看到的密码As4sjPF;82ka
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.33 MySQL Community Server (GPL)

Copyright (c) 2000, 2021, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> set global validate_password_length=6;
Query OK, 0 rows affected (0.00 sec)

mysql> set global validate_password_policy=LOW;
Query OK, 0 rows affected (0.00 sec)

mysql> ALTER user USER() IDENTIFIED BY '123456';
Query OK, 0 rows affected (0.00 sec)

mysql> \q
Bye
[root@k8s-master01 ~]# mysql -uroot -p123456
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 13
Server version: 5.7.33 MySQL Community Server (GPL)

Copyright (c) 2000, 2021, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.00 sec)

MySQL 8.0 安装

1)在安装之前先将系统已经安装的MySQL清理掉

[root@k8s-master01 ~]# > /var/log/mysqld.log # 清理掉5.7生成的日志
[root@k8s-master01 ~]# rm -rf /var/lib/mysql
[root@k8s-master01 ~]# yum remove mysql-server -y
[root@k8s-master01 ~]# rpm -qa | grep mysql # 如果没有查到就不需要删除,直接进行下一步安装数据源
mysql-community-client-5.7.33-1.el7.x86_64
mysql57-community-release-el7-10.noarch
mysql-community-common-5.7.33-1.el7.x86_64
mysql-community-libs-5.7.33-1.el7.x86_64
[root@k8s-master01 ~]# rpm -e --nodeps mysql-community-client-5.7.33-1.el7.x86_64
[root@k8s-master01 ~]# rpm -e --nodeps mysql57-community-release-el7-10.noarch
[root@k8s-master01 ~]# rpm -e --nodeps mysql-community-common-5.7.33-1.el7.x86_64
[root@k8s-master01 ~]# rpm -e --nodeps mysql-community-libs-5.7.33-1.el7.x86_64

2)安装MySQL 8.0数据源

[root@k8s-master01 ~]# wget https://repo.mysql.com/mysql80-community-release-el7-1.noarch.rpm
[root@k8s-master01 ~]# rpm -ivh mysql80-community-release-el7-1.noarch.rpm

3)安装MySQL 8.0

[root@k8s-master01 ~]# yum install mysql-server -y

4)启用MySQL

[root@k8s-master01 ~]# systemctl start mysqld
[root@k8s-master01 ~]# netstat -anltp | grep mysqld
tcp6       0      0 :::3306                 :::*                    LISTEN      24381/mysqld      
[root@k8s-master01 ~]# cat  /var/log/mysqld.log | grep "password"		# MySQL 8.0 跟别的版本不一样他会在启动的时候随机生成一个密码,这个密码用于第一次登录的时候需要输入
2021-01-26T03:07:46.639183Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: y+kxb-cqD5q4
[root@k8s-master01 ~]# mysql -uroot -py+kxb-cqD5q4
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.23

Copyright (c) 2000, 2021, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> 

5)设置数据库密码

mysql> ALTER user USER() IDENTIFIED BY '123456';		# MySQL 8.0有密码策略的要求,如果想设置简单的话就需要修改一下密码的策略,如果不想修改就设置复杂一些密码,这里为了演示我们就修改一下密码策略。
ERROR 1819 (HY000): Your password does not satisfy the current policy requirements
mysql> set global validate_password.length=1;
Query OK, 0 rows affected (0.00 sec)

mysql> set global validate_password.policy=0;
Query OK, 0 rows affected (0.00 sec)

mysql> ALTER user USER() IDENTIFIED BY '123456';
Query OK, 0 rows affected (0.02 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

mysql> \q
Bye
[root@k8s-master01 ~]# mysql -uroot -p123456
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 9
Server version: 8.0.23 MySQL Community Server - GPL

Copyright (c) 2000, 2021, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.01 sec)
mysql> SHOW VARIABLES LIKE 'validate_password%';	# 查看当前数据密码策略
+--------------------------------------+-------+
| Variable_name                        | Value |
+--------------------------------------+-------+
| validate_password.check_user_name    | ON    |		# 
| validate_password.dictionary_file    |       |		# 指定密码验证的文件路径
| validate_password.length             | 4     |		# 固定密码的总长度
| validate_password.mixed_case_count   | 1     | 		# 整个密码中至少要包含大/小写字母的总个数
| validate_password.number_count       | 1     |		# 整个密码中至少要包含阿拉伯数字的个数
| validate_password.policy             | LOW   |		# 指定密码的强度验证等级,默认为 MEDIUM
| validate_password.special_char_count | 1     |		#  整个密码中至少要包含特殊字符的个数
+--------------------------------------+-------+
7 rows in set (0.01 sec)

注意:在生产环境中千万不要使用mysql -uroot -p123456方式登录数据库,因为可以通过history命令查看到这样就不安全了, 在这里是为了让大家更好的看效果所以就这么写了,一般使用mysql -uroot -p登录

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

运维生涯记录

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值