(一)安装mysql

时间:2022-11-17 20:01:57

1、环境说明

操作系统:centos7.9
mysql版本:mysql5.7
安装方式:官网yum安装

2、安装步骤

1、下载mysql的yum源安装包

下载地址: https://dev.mysql.com/downloads/repo/yum/ 下载选择linux7对应的RPM包,进入后选择No thanks, just start my download.即可跳过登录直接进行下载,下载后会得到一个.rpm的文件 (一)安装mysql

2、配置yum源

将下载好的rpm包上传到系统中,然后进行安装

1、安装
[root@mysql ~]# ll
-rw-r--r--. 1 root root 11196 Nov 18 02:35 mysql80-community-release-el7-7.noarch.rpm
[root@mysql ~]# yum install -y ./mysql80-community-release-el7-7.noarch.rpm 

2、查看yum源
安装后会在 /etc/yum.repos.d/ 目录下生成3个 .repo 的文件
[root@mysql ~]# ll /etc/yum.repos.d/
···
-rw-r--r--. 1 root root 1147 Aug 27 02:12 mysql-community-debuginfo.repo
-rw-r--r--. 1 root root 2062 Aug 27 02:12 mysql-community.repo
-rw-r--r--. 1 root root 2132 Aug 27 02:12 mysql-community-source.repo

3、修改yum源
mysql-community.repo源中包含很多mysql的版本,默认开启的是mysql最新版本的源,将最新的源关闭,开始mysql5.7版本的源
[root@mysql ~]# vim /etc/yum.repos.d/mysql-community.repo 
  
# Enable to use MySQL 5.7
[mysql57-community]
name=MySQL 5.7 Community Server
baseurl=http://repo.mysql.com/yum/mysql-5.7-community/el/7/$basearch
enabled=1		#开启mysql5.7的源,默认是0 关闭
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql-2022
       file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql

[mysql80-community]
name=MySQL 8.0 Community Server
baseurl=http://repo.mysql.com/yum/mysql-8.0-community/el/7/$basearch
enabled=0		#关闭mysql8.0的源,默认是1 开启
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql-2022
       file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql

ps:也可以使用你命令行方式修改,需提前安装yum-utils工具

修改完成之后重新加载yum源
[root@mysql ~]# yum clean all && yum makecache

3、安装mysql

1、查看可用的版本
[root@mysql ~]# yum provides mysql-server
···
mysql-community-server-5.7.40-1.el7.x86_64 : A very fast and reliable SQL database server
Repo        : mysql57-community
Matched from:
Provides    : mysql-server = 5.7.40-1.el7

ps:mysql-server为mysql的主包

2、安装mysql
[root@mysql ~]# yum install -y mysql-community-server-5.7.40-1.el7.x86_64
或者不指定报名:
yum install -y mysql-server

4、启动mysql

1、启动
[root@mysql ~]# systemctl enable mysqld --now
[root@mysql ~]# netstat -lntup		#mysql的默认端口3306
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1351/sshd           
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      1501/master         
tcp6       0      0 :::3306                 :::*                    LISTEN      2140/mysqld         
tcp6       0      0 :::22                   :::*                    LISTEN      1351/sshd           
tcp6       0      0 ::1:25                  :::*                    LISTEN      1501/master 

2、查看mysql的默认密码
[root@mysql ~]# grep password /var/log/mysqld.log 
2022-11-17T19:01:20.086710Z 1 [Note] A temporary password is generated for root@localhost: fx9aT)8=dHrk
ps:fx9aT)8=dHrk mysql的默认密码,只能登录不能使用mysql,可以使用此密码继续进行登录测试

3、修改密码
[root@mysql ~]# mysqladmin -u root -p'fx9aT)8=dHrk' password 'Admin@123'
mysqladmin: [Warning] Using a password on the command line interface can be insecure.
Warning: Since password will be sent to server in plain text, use ssl connection to ensure password safety.

4、登录测试
[root@mysql ~]# mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.40 MySQL Community Server (GPL)

Copyright (c) 2000, 2022, 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)

至此mysql5.7安装成功

3、mysql的目录

  1. 配置文件目录:/etc/my.cnf
  2. 数据库目录:/var/lib/mysql
  3. 日志文件:/var/log/mysqld.log

4、自建mysql的yum仓库

1、准备一台没有安装过mysql的机器

2、配置好mysql的安装源

可以使用官网的yum源,也可以使用国内的yum源 (阿里源,华为源,清华大学源)

3、下载软件包

[root@mysql ~]# yum install --downloadonly mysql-server
--download 参数只下载不安装
[root@mysql ~]# ll /var/cache/yum/x86_64/7/mysql57-community/packages
total 215704
-rw-r--r--. 1 root root  29373184 Aug 31 09:45 mysql-community-client-5.7.40-1.el7.x86_64.rpm
-rw-r--r--. 1 root root    318744 Aug 31 09:45 mysql-community-common-5.7.40-1.el7.x86_64.rpm
-rw-r--r--. 1 root root   2744456 Aug 31 09:45 mysql-community-libs-5.7.40-1.el7.x86_64.rpm
-rw-r--r--. 1 root root   1264356 Aug 31 09:45 mysql-community-libs-compat-5.7.40-1.el7.x86_64.rpm
-rw-r--r--. 1 root root 187168492 Aug 31 09:45 mysql-community-server-5.7.40-1.el7.x86_64.rpm

4、制作yum源

可以看到下载的包都在这个目录下,这些都是安装mysql的包,将这个些包拷贝到其他位置

[root@mysql ~]# mkdir mysql57
[root@mysql ~]# cp /var/cache/yum/x86_64/7/mysql57-community/packages/* mysql57/
[root@mysql ~]# createrepo mysql57/
Spawning worker 0 with 5 pkgs
Workers Finished
Saving Primary metadata
Saving file lists metadata
Saving other metadata
Generating sqlite DBs
Sqlite DBs complete

ps:如果没有createrepo命令可以直接使用yum安装
看到mysql57目录中出现一个repodata说明制作成功
[root@mysql ~]# ll mysql57/
total 215708
-rw-r--r--. 1 root root  29373184 Nov 18 03:33 mysql-community-client-5.7.40-1.el7.x86_64.rpm
-rw-r--r--. 1 root root    318744 Nov 18 03:33 mysql-community-common-5.7.40-1.el7.x86_64.rpm
-rw-r--r--. 1 root root   2744456 Nov 18 03:33 mysql-community-libs-5.7.40-1.el7.x86_64.rpm
-rw-r--r--. 1 root root   1264356 Nov 18 03:33 mysql-community-libs-compat-5.7.40-1.el7.x86_64.rpm
-rw-r--r--. 1 root root 187168492 Nov 18 03:33 mysql-community-server-5.7.40-1.el7.x86_64.rpm
drwxr-xr-x. 2 root root      4096 Nov 18 03:38 repodata

5、打包测试

1、打包
[root@mysql ~]# tar -zcvf mysql57.tar.gz mysql57/
[root@mysql ~]# ll
drwxr-xr-x. 3 root root      4096 Nov 18 03:38 mysql57
-rw-r--r--. 1 root root 220783296 Nov 18 03:40 mysql57.tar.gz
2、将压缩包拷贝到其他的主机上,然后进行解压
tar -zxvf mysql57.tar.gz /opt/
编写repo文件
vim /etc/yum.repos.d/mysql57.repo
[mysql57]
name = mysql57
baseurl = /opt/mysql57
gpgcheck=0
enabeld=1

3、安装测试
yum install -y mysql-server