01-mysql安装篇(rpm方式安装+二进制方式安装)
作者:mmseoamin日期:2024-03-20

01-mysql安装篇(rpm方式安装+二进制方式安装)

    • 一、rpm方式安装
      • 1、检查是否安装了mariadb
      • 2、下载mysql
      • 3、上传解压
      • 4、安装
      • 5、检查安装
      • 6、开启mysql服务
      • 7、登陆mysql
      • 8、修改密码设置规则(简单型-学习用)
      • 9、修改密码
      • 10、授权远程登陆
      • 11、启停mysql命令
      • 12、rpm方式安装说明
      • 二、二进制方式安装V5.7.18
        • 1、上传解压
        • 2、创建安装目录
        • 3、拷贝解压后的文件至安装目录/usr/local/mysql/
        • 4、创建相关目录
        • 5、创建mysql组和用户
        • 6、更改安装目录归属
        • 7、初始化MySQL
        • 8、 配置MySQL
        • 9、启动MySQL
        • 10、配置环境变量
        • 11、允许root账号远程访问
        • 12、设置mysql服务开机自启
        • 13、mysql服务启动

          01-mysql安装篇(rpm方式安装+二进制方式安装),在这里插入图片描述,第1张

          一、rpm方式安装

          1、检查是否安装了mariadb

          [root@db-mysql ~]# rpm -qa|grep mariadb
          mariadb-libs-5.5.68-1.el7.x86_64
          [root@db-mysql ~]# 
          

          卸载清除: rpm -e 文件名

          [root@db-mysql ~]# rpm -e mariadb-libs-5.5.68-1.el7.x86_64 --nodeps
          [root@db-mysql ~]# 
          [root@db-mysql ~]# rpm -qa|grep mariadb  
          [rootdb-mysql ~]# 
          

          2、下载mysql

          官网地址:https://downloads.mysql.com/archives/community/

          01-mysql安装篇(rpm方式安装+二进制方式安装),在这里插入图片描述,第2张

          3、上传解压

          [root@db-mysql ~]# mkdir mysql-bundle
          [root@db-mysql mysql-bundle]# tar -xvf mysql-5.7.37-1.el7.x86_64.rpm-bundle.tar
          mysql-community-client-5.7.37-1.el7.x86_64.rpm
          mysql-community-common-5.7.37-1.el7.x86_64.rpm
          mysql-community-devel-5.7.37-1.el7.x86_64.rpm
          mysql-community-embedded-5.7.37-1.el7.x86_64.rpm
          mysql-community-embedded-compat-5.7.37-1.el7.x86_64.rpm
          mysql-community-embedded-devel-5.7.37-1.el7.x86_64.rpm
          mysql-community-libs-5.7.37-1.el7.x86_64.rpm
          mysql-community-libs-compat-5.7.37-1.el7.x86_64.rpm
          mysql-community-server-5.7.37-1.el7.x86_64.rpm
          mysql-community-test-5.7.37-1.el7.x86_64.rpm
          [root@db-mysql mysql-bundle]# 
          

          4、安装

          ## 安装mysql所依赖的包
          [root@db-mysql mysql-bundle]# rpm -ivh mysql-community-common-5.7.37-1.el7.x86_64.rpm
          warning: mysql-community-common-5.7.37-1.el7.x86_64.rpm: Header V4 RSA/SHA256 Signature, key ID 3a79bd29: NOKEY
          Preparing...                          ################################# [100%]
          Updating / installing...
             1:mysql-community-common-5.7.37-1.e################################# [100%]
          [root@db-mysql mysql-bundle]# rpm -ivh mysql-community-libs-5.7.37-1.el7.x86_64.rpm
          warning: mysql-community-libs-5.7.37-1.el7.x86_64.rpm: Header V4 RSA/SHA256 Signature, key ID 3a79bd29: NOKEY
          Preparing...                          ################################# [100%]
          Updating / installing...
             1:mysql-community-libs-5.7.37-1.el7################################# [100%]
          [root@db-mysql mysql-bundle]# rpm -ivh mysql-community-client-5.7.37-1.el7.x86_64.rpm
          warning: mysql-community-client-5.7.37-1.el7.x86_64.rpm: Header V4 RSA/SHA256 Signature, key ID 3a79bd29: NOKEY
          Preparing...                          ################################# [100%]
          Updating / installing...
             1:mysql-community-client-5.7.37-1.e################################# [100%]
          [root@db-mysql mysql-bundle]# 
          ## 安装mysql服务
          [root@db-mysql mysql-bundle]# rpm -ivh mysql-community-server-5.7.37-1.el7.x86_64.rpm
          warning: mysql-community-server-5.7.37-1.el7.x86_64.rpm: Header V4 RSA/SHA256 Signature, key ID 3a79bd29: NOKEY
          error: Failed dependencies:
          	net-tools is needed by mysql-community-server-5.7.37-1.el7.x86_64
          [root@db-mysql mysql-bundle]# 
          

          报错:error: Failed dependencies:

          net-tools is needed by mysql-community-server-5.7.37-1.el7.x86_64

          解决:

          [root@db-mysql mysql-bundle]# yum install -y net-tools
          [root@db-mysql mysql-bundle]# yum install -y perl
          [root@db-mysql mysql-bundle]# yum install -y libaio
          
          ## 再次安装mysql服务
          [root@db-mysql mysql-bundle]# rpm -ivh mysql-community-server-5.7.37-1.el7.x86_64.rpm
          warning: mysql-community-server-5.7.37-1.el7.x86_64.rpm: Header V4 RSA/SHA256 Signature, key ID 3a79bd29: NOKEY
          Preparing...                          ################################# [100%]
          Updating / installing...
             1:mysql-community-server-5.7.37-1.e################################# [100%]
          [root@db-mysql mysql-bundle]# 
          

          5、检查安装

          [root@cdb-mysql mysql-bundle]# rpm -qa|grep mysql
          mysql-community-common-5.7.37-1.el7.x86_64
          mysql-community-libs-5.7.37-1.el7.x86_64
          mysql-community-client-5.7.37-1.el7.x86_64
          mysql-community-server-5.7.37-1.el7.x86_64
          [root@db-mysql mysql-bundle]#
          

          6、开启mysql服务

          [root@db-mysql mysql-bundle]# systemctl start mysqld
          [root@db-mysql mysql-bundle]# systemctl status mysqld
          ● mysqld.service - MySQL Server
             Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
             Active: active (running) since Thu 2022-12-22 10:48:53 CST; 9s ago
               Docs: man:mysqld(8)
                     http://dev.mysql.com/doc/refman/en/using-systemd.html
            Process: 2190 ExecStart=/usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid $MYSQLD_OPTS (code=exited, status=0/SUCCESS)
            Process: 2140 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)
           Main PID: 2194 (mysqld)
             CGroup: /system.slice/mysqld.service
                     └─2194 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid
          Dec 22 10:48:50 centos01 systemd[1]: Starting MySQL Server...
          Dec 22 10:48:53 centos01 systemd[1]: Started MySQL Server.
          [root@db-mysql mysql-bundle]# 
          

          7、登陆mysql

          第一次启动mysql,会自动生成一个随机密码,可在/var/log/mysqld.log中查看。

          [root@db-mysql ~]# grep "password" /var/log/mysqld.log
          2022-12-22T02:48:50.966917Z 1 [Note] A temporary password is generated for root@localhost: q=VIgtgHH7p_
          2022-12-22T02:54:05.987706Z 2 [Note] Access denied for user 'root'@'localhost' (using password: YES)
          2022-12-22T02:54:14.598377Z 3 [Note] Access denied for user 'root'@'localhost' (using password: NO)
          2022-12-22T02:54:27.747247Z 4 [Note] Access denied for user 'root'@'localhost' (using password: YES)
          [root@db-mysql ~]# 
          

          01-mysql安装篇(rpm方式安装+二进制方式安装),在这里插入图片描述,第3张

          8、修改密码设置规则(简单型-学习用)

          [root@centos01 mysql-bundle]# mysql -u root -p
          Enter password: 
          ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
          [root@centos01 mysql-bundle]# mysql -u root -p
          Enter password: 
          Welcome to the MySQL monitor.  Commands end with ; or \g.
          Your MySQL connection id is 6
          Server version: 5.7.37
          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> set global validate_password_policy=LOW;
          Query OK, 0 rows affected (0.00 sec)
          mysql> set global validate_password_length=4;
          Query OK, 0 rows affected (0.00 sec)
          mysql> set global validate_password_mixed_case_count=0;
          Query OK, 0 rows affected (0.00 sec)
          mysql> set global validate_password_number_count=0;
          Query OK, 0 rows affected (0.00 sec)
          mysql> set global validate_password_special_char_count=0;
          Query OK, 0 rows affected (0.00 sec)
          mysql> 
          

          9、修改密码

          mysql>  alter user root@localhost identified by '123456';
          Query OK, 0 rows affected (0.00 sec)
          

          10、授权远程登陆

          mysql> grant all privileges on *.* to 'root'@'%' identified by '123456' with grant option;
          Query OK, 0 rows affected, 1 warning (0.00 sec)
          mysql> 
          

          测试:

          01-mysql安装篇(rpm方式安装+二进制方式安装),![在这里图片描述](https://img-blog.csdnimg.cn/f02eadbd2a294ec3bcc526f068e9baa5.png,第4张

          11、启停mysql命令

          ## 启动 mysql 服务
          [root@db-mysql ~]# systemctl start mysqld
          ## 关闭
          [root@db-mysql ~]# systemctl stop mysqld
          ## 重启
          [root@db-mysql ~]# systemctl restart mysqld
          ## 状态查看
          [root@db-mysql ~]# systemctl status mysqld
          ## 设置开机自启
          [root@db-mysql ~]# systemctl enable mysqld
          

          12、rpm方式安装说明

          ## rpm方式重要文件路径说明
          主要文件默认路径如下:
          配置文件路径:/etc/my.cnf
          数据存储目录:/var/lib/mysql
          错误日志存储路径:/var/log/mysqld.log
          socket文件路径:/var/lib/mysql/mysql.sock
          参数可以通过/etc/my.cnf参数配置文件查看和自定义。
          

          二、二进制方式安装V5.7.18

          1、上传解压

          [root@db-mysql mysql-tar]# tar -zxvf mysql-5.7.18-linux-glibc2.5-x86_64.tar.gz
          

          01-mysql安装篇(rpm方式安装+二进制方式安装),在这里插入图片描述,第5张

          2、创建安装目录

          [root@db-mysql mysql-tar]# mkdir /usr/local/mysql
          

          01-mysql安装篇(rpm方式安装+二进制方式安装),在这里插入图片描述,第6张

          3、拷贝解压后的文件至安装目录/usr/local/mysql/

          [root@db-mysql ~]# mv mysql-5.7.18-linux-glibc2.5-x86_64/* /usr/local/mysql/
          

          01-mysql安装篇(rpm方式安装+二进制方式安装),在这里插入图片描述,第7张

          01-mysql安装篇(rpm方式安装+二进制方式安装),在这里插入图片描述,第8张

          4、创建相关目录

          [root@db-mysql mysql]# mkdir /usr/local/mysql/data
          [root@db-mysql mysql]# 
          [root@db-mysql mysql]# mkdir /usr/local/mysql/log
          [root@db-mysql mysql]# 
          [root@db-mysql mysql]# touch /usr/local/mysql/log/mysqld.log
          [root@db-mysql mysql]# 
          [root@db-mysql mysql]# 
          

          01-mysql安装篇(rpm方式安装+二进制方式安装),在这里插入图片描述,第9张

          5、创建mysql组和用户

          [root@db-mysql mysql]# groupadd mysql
          [root@db-mysql mysql]# 
          [root@db-mysql mysql]# useradd -r -g mysql mysql
          [root@db-mysql mysql]# 
          

          6、更改安装目录归属

          [root@db-mysql mysql]# chown -R mysql /usr/local/mysql/
          [root@db-mysql mysql]# chgrp -R mysql /usr/local/mysql/
          

          或者使用:

          [root@db-mysql mysql]# chown -R mysql:mysql /usr/local/mysql
          

          01-mysql安装篇(rpm方式安装+二进制方式安装),在这里插入图片描述,第10张

          7、初始化MySQL

          [root@db-mysql mysql]# ./bin/mysqld --user=mysql --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data --initialize
          

          01-mysql安装篇(rpm方式安装+二进制方式安装),在这里插入图片描述,第11张

          8、 配置MySQL

          [root@db-mysql mysql]# vi /etc/my.cnf
          
          [mysqld]
          datadir=/usr/local/mysql/data
          basedir=/usr/local/mysql
          socket=/tmp/mysqld.sock
          user=mysql
          port=3306
          character-set-server=utf8
          skip-grant-tables
          symbolic-links=0
          [mysqld_safe]
          log-error=/var/log/mysqld.log
          pid-file=/var/run/mysqld/mysqld.pid
          [client]
          socket=/tmp/mysqld.sock
          

          01-mysql安装篇(rpm方式安装+二进制方式安装),在这里插入图片描述,第12张

          9、启动MySQL

          [root@db-mysql mysql]# cd support-files/
          [root@db-mysql support-files]# sh mysql.server start
          Starting MySQL. SUCCESS! 
          [root@db-mysql support-files]#
          

          01-mysql安装篇(rpm方式安装+二进制方式安装),在这里插入图片描述,第13张

          查看MySQL服务是否启动成功

          [root@db-mysql support-files]# sh mysql.server status
           SUCCESS! MySQL running (2294)
          [root@db-mysql support-files]#
          

          10、配置环境变量

          [root@db-mysql support-files]# vi /etc/profile
          
          export PATH=$PATH:/usr/local/mysql/bin
          

          01-mysql安装篇(rpm方式安装+二进制方式安装),在这里插入图片描述,第14张

          使配置生效

          [root@db-mysql support-files]# source /etc/profile
          

          11、允许root账号远程访问

          [root@db-mysql support-files]# mysql -u root -p
          

          01-mysql安装篇(rpm方式安装+二进制方式安装),在这里插入图片描述,第15张

          修改mysql数据库密码报错:

          ERROR 1290 (HY000): The MySQL server is running with the --skip-grant-tables option so it cannot execute this statement.

          解决:先刷新权限表

          mysql> flush privileges;
          Query OK, 0 rows affected (0.01 sec)
          
          ##修改密码
           alter user root@localhost identified by '123456';
           
           ##root账号远程访问
           grant all privileges on *.* to 'root'@'%' identified by '123456' with grant option;
          

          01-mysql安装篇(rpm方式安装+二进制方式安装),在这里插入图片描述,第16张

          12、设置mysql服务开机自启

          [root@db-mysql mysql]# systemctl status mysqld
          

          报错: Unit mysqld.service could not be found.

          [root@db-mysql mysql]# find / -name mysql.server
          /usr/local/mysql/support-files/mysql.server
          [root@db-mysql mysql]# cp /usr/local/mysql/support-files/mysql.server  /etc/init.d/mysqld
          [root@db-mysql mysql]# systemctl enable mysqld
          mysqld.service is not a native service, redirecting to /sbin/chkconfig.
          Executing /sbin/chkconfig mysqld on
          [root@db-mysql mysql]# systemctl status mysqld
          ● mysqld.service - LSB: start and stop MySQL
             Loaded: loaded (/etc/rc.d/init.d/mysqld; bad; vendor preset: disabled)
             Active: inactive (dead)
               Docs: man:systemd-sysv-generator(8)
          [root@db-mysql mysql]# 
          

          13、mysql服务启动

          [root@db-mysql mysql]# systemctl start mysqld
          [root@db-mysql mysql]# systemctl status mysqld
          ● mysqld.service - LSB: start and stop MySQL
             Loaded: loaded (/etc/rc.d/init.d/mysqld; bad; vendor preset: disabled)
             Active: active (exited) since 三 2023-05-10 18:28:08 CST; 2s ago
               Docs: man:systemd-sysv-generator(8)
            Process: 10873 ExecStart=/etc/rc.d/init.d/mysqld start (code=exited, status=0/SUCCESS)
          5月 10 18:28:08 db-mysql systemd[1]: Starting LSB: start and stop MySQL...
          5月 10 18:28:08 db-mysql mysqld[10873]: Starting MySQL SUCCESS!
          5月 10 18:28:08 db-mysql systemd[1]: Started LSB: start and stop MySQL.
          5月 10 18:28:08 db-mysql mysqld[10873]: 2023-05-10T10:28:08.620811Z mysqld_safe A mysqld process already exists
          [root@db-mysql mysql]#