zoukankan      html  css  js  c++  java
  • centos7服务器安装mysql8

    前言

    CentOS 7 版本将MySQL数据库软件从默认的程序列表中移除,用mariadb代替了。
    本文记录了在线安装mysql及离线安装mysql的方法和常见问题。
    如果需要继续使用MySQL,可参考本文操作。

    在线安装

    获取yum源

    1. 在MySQL官网中下载YUM源rpm安装包,复制下载链接使用wget下载。
    wget https://dev.mysql.com/get/mysql80-community-release-el7-1.noarch.rpm
    
    1. 安装yum源
    yum localinstall mysql80-community-release-el7-1.noarch.rpm
    
    1. 检查yum源
    yum repolist enabled | grep "mysql.*-community.*"
    

    可能遇到的问题:

    • 安装了较旧的yum源,先删除旧版本

      1. 查询yum源

        rpm -qa |grep -i repo-name

      2. 删除该yum源

        rpm -e repo-name

      参考博客地址

    安装mysql

    1. 可以修改/etc/yum.repos.d/mysql-community.repo源,改变默认安装的mysql版本,enabled=0禁用,enabled=1启用。

    2. 安装默认版本

      ※ 此处会耗费很长时间,可以先忙别的。

    yum install -y mysql-community-server
    

    执行完毕即安装完成。受服务器网速影响(预估耗时30h+),在线安装到此截止。改用离线安装。↓

    离线安装

    1. 官网下载RPM Bundle

    2. 上传Bundle包至服务器,rz -be上传、或xftp上传。

    3. 解压Bundle包至/usr/local/mysql目录下。

    4. 依次安装common、libs、client、server

    [root@VM-0-4-centos mysql]# tar -xvf mysql-8.0.21-1.el7.x86_64.rpm-bundle.tar 
    mysql-community-common-8.0.21-1.el7.x86_64.rpm
    mysql-community-embedded-compat-8.0.21-1.el7.x86_64.rpm
    mysql-community-libs-8.0.21-1.el7.x86_64.rpm
    mysql-community-devel-8.0.21-1.el7.x86_64.rpm
    mysql-community-server-8.0.21-1.el7.x86_64.rpm
    mysql-community-client-8.0.21-1.el7.x86_64.rpm
    mysql-community-libs-compat-8.0.21-1.el7.x86_64.rpm
    mysql-community-test-8.0.21-1.el7.x86_64.rpm
    [root@VM-0-4-centos mysql]# rpm -qa | grep mariadb
    [root@VM-0-4-centos mysql]# rpm -ivh mysql-community-common-8.0.21-1.el7.x86_64.rpm 
    Preparing...                          ################################# [100%]
    Updating / installing...
       1:mysql-community-common-8.0.21-1.e################################# [100%]
    [root@VM-0-4-centos mysql]# rpm -ivh mysql-community-libs-8.0.21-1.el7.x86_64.rpm 
    Preparing...                          ################################# [100%]
    Updating / installing...
       1:mysql-community-libs-8.0.21-1.el7################################# [100%]
    [root@VM-0-4-centos mysql]# rpm -ivh mysql-community-client-8.0.21-1.el7.x86_64.rpm 
    Preparing...                          ################################# [100%]
    Updating / installing...
       1:mysql-community-client-8.0.21-1.e################################# [100%]
    [root@VM-0-4-centos mysql]# rpm -ivh mysql-community-server-8.0.21-1.el7.x86_64.rpm 
    Preparing...                          ################################# [100%]
    Updating / installing...
       1:mysql-community-server-8.0.21-1.e################################# [100%]
    

    至此,mysql已安装完毕。

    服务启停

    1. 查看服务状态

      systemctl status mysqld.service

    2. 启动服务

      service mysqld start

    3. 停止服务

      service mysqld stop

    # 启动服务
    [root@VM-0-4-centos etc]# service mysqld start
    Redirecting to /bin/systemctl start mysqld.service
    # 查看服务
    [root@VM-0-4-centos etc]# systemctl status mysqld.service 
    ● mysqld.service - MySQL Server
       Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
       Active: active (running) since Tue 2020-09-01 00:08:51 CST; 37s ago
         Docs: man:mysqld(8)
               http://dev.mysql.com/doc/refman/en/using-systemd.html
      Process: 21087 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)
     Main PID: 21110 (mysqld)
       Status: "Server is operational"
       CGroup: /system.slice/mysqld.service
               └─21110 /usr/sbin/mysqld
    
    # 关闭服务
    [root@VM-0-4-centos etc]# service mysqld stop
    Redirecting to /bin/systemctl stop mysqld.service
    [root@VM-0-4-centos etc]# systemctl status mysqld.service 
    ● mysqld.service - MySQL Server
       Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
       Active: inactive (dead) since Tue 2020-09-01 00:09:49 CST; 11s ago
         Docs: man:mysqld(8)
               http://dev.mysql.com/doc/refman/en/using-systemd.html
      Process: 21110 ExecStart=/usr/sbin/mysqld $MYSQLD_OPTS (code=exited, status=0/SUCCESS)
      Process: 21087 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)
     Main PID: 21110 (code=exited, status=0/SUCCESS)
       Status: "Server shutdown complete"
    
    1. 遇到问题

      [ERROR] [MY-012271] [InnoDB] The innodb_system data file 'ibdata1' must be writable

      查看日志发现是ibdata1无写权限,修改/var/lib/mysql权限为777

      chmod -R 777 mysql

    数据库设置

    1. 数据库初始化

      使用指令1对数据库进行初始化mysqld --initialize;,并得到临时密码。

    [root@VM-0-4-centos etc]# mysqld --initialize;
    2020-08-31T16:13:01.986611Z 0 [System] [MY-013169] [Server] /usr/sbin/mysqld (mysqld 8.0.21) initializing of server in progress as process 21288
    2020-08-31T16:13:01.995195Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
    2020-08-31T16:13:04.085829Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
    2020-08-31T16:13:07.225825Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: fu-k1yjtnllY	《==============临时密码
    

    ​ 或查看日志获取临时密码(我安装了两次所以两种方式显示密码不一致,请忽略)

    [root@VM-0-4-centos mysql]# cat /var/log/mysqld.log |grep pass
    2020-08-31T17:45:15.371100Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: gX:Uey-fg2JV	《==============临时密码
    
    1. 登录mysql

      使用临时密码登录mysql

    [root@VM-0-4-centos mysql]# mysql -uroot -p
    Enter password: 
    Welcome to the MySQL monitor.  Commands end with ; or g.
    Your MySQL connection id is 9
    Server version: 8.0.21
    
    Copyright (c) 2000, 2020, 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> 
    
    1. 修改root密码

      使用 alter USER 'root'@'localhost' IDENTIFIED BY 'root';修改root密码。

    mysql> alter USER 'root'@'localhost' IDENTIFIED BY 'root';
    Query OK, 0 rows affected (0.05 sec)
    mysql> quit
    Bye
    [root@VM-0-4-centos mysql]# mysql -uroot -p
    Enter password: <===========================输入新密码root
    Welcome to the MySQL monitor.  Commands end with ; or g.
    Your MySQL connection id is 11
    Server version: 8.0.21 MySQL Community Server - GPL
    
    Copyright (c) 2000, 2020, 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>
    
    1. 授权远程连接
    # 切换数据库
    mysql> use mysql;
    Reading table information for completion of table and column names
    You can turn off this feature to get a quicker startup with -A
    
    Database changed
    # 修改root host为%
    mysql> update user set host = "%" where user='root';
    Query OK, 1 row affected (0.02 sec)
    Rows matched: 1  Changed: 1  Warnings: 0
    mysql> flush privileges;
    Query OK, 0 rows affected (0.01 sec)
    # 修改远程登录密码为root
    mysql> alter USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY 'root';
    Query OK, 0 rows affected (0.01 sec)
    
    mysql> flush privileges;
    Query OK, 0 rows affected (0.01 sec)
    
    1. 使用Navicat测试连接,可在/etc/my.cnf中配置端口号,默认为3306。

    END

    mysql的安装出现了挺多error,不过还好大部分都能在网上找到答案,总结起来大概包含以下内容

    • rpm安装需要先移除旧版本。

      rpm -qa|grep XXX

      rpm -e XXX

    • 移除旧版本时报错缺少依赖。

      rpm -ev XXX --force --nodeps

    • mysql安装完成后初始化失败。

      清空/var/lib/mysql目录。有些版本是data目录。

    • mysql服务启动失败。

      查看/var/log/mysqld.log错误日志定位问题,缺路径就建文件夹,少权限就授权。

    • 登录mysql失败,需要密码。

      初始化后查看日志获取临时密码cat /var/log/mysqld.log |grep pass

    另外说句题外话:

    ​ 服务器上安装任何软件都尽量修改其访问端口,使用默认端口很危险,在学习redis、mysql、filebeat等软件时,都曾因使用默认端口被残忍挖矿,血泪教训。o(╥﹏╥)o

  • 相关阅读:
    初级程序员面试不靠谱指南(三)
    [细节决定B度]之回首一瞥cout<<"Hello,world"<<endl;
    20110310日记wmh
    SliverLight 控件属性的赋值方式
    20110309wmh日记
    20110308wmh日记
    20110307wmh日记
    STOAdiary20110314完成的任务
    20110313wmh日记
    20110311wmh日记
  • 原文地址:https://www.cnblogs.com/bcomll/p/13593994.html
Copyright © 2011-2022 走看看