MySql教程

安装mysql

本文主要是介绍安装mysql,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

安装mysql

1.卸载系统自带的mariadb-lib

  • 查看mariadb版本
    rpm -qa | grep mariadb
  • 卸载mariadb
    rpm -e mariadb-libs-5.5.56-2.el7.x86_64 --nodeps
  • 重启机器
    reboot
[root@localhost ~]# rpm -qa | grep mariadb
mariadb-libs-5.5.52-1.el7.x86_64
[root@localhost ~]# rpm -e mariadb-libs-5.5.52-1.el7.x86_64 --nodeps
[root@localhost ~]# reboot

2.上传安装包并解压

下载地址:https://dev.mysql.com/downloads/mysql/

image-20210801112153895
  1. 将mysql-8.0.18-1.el8.x86_64.rpm-bundle.tar安装包上传到/opt/software目录

    • 先在opt下面创建software文件夹

      [root@localhost /]# cd /opt
      [root@localhost opt]# mkdir software
      

    linux中/opt目录用来安装附加软件包,是用户级的程序目录,可以理解为D:/Software。安装到/opt目录下的程序,它所有的数据、库文件等等都是放在同个目录下面。

    • 上传到software这个文件夹

      [root@localhost opt]# cd software/
      [root@localhost software]# rz
      [root@localhost software]# ls
      mysql-8.0.26-1.el8.x86_64.rpm-bundle.tar
      
  2. 解压mysql安装包到/opt/server目录

    [root@localhost opt]# mkdir server
    [root@localhost opt]# cd software/
    [root@localhost software]# tar -xvf  mysql-8.0.26-1.el8.x86_64.rpm-bundle.tar -C /opt/server
    
  3. 按顺序安装

    进入server文件夹进行顺序安装

    [root@localhost software]# cd ../server
    [root@localhost server]# ls
    mysql-community-client-8.0.26-1.el8.x86_64.rpm
    mysql-community-client-debuginfo-8.0.26-1.el8.x86_64.rpm
    mysql-community-client-plugins-8.0.26-1.el8.x86_64.rpm
    mysql-community-client-plugins-debuginfo-8.0.26-1.el8.x86_64.rpm
    mysql-community-common-8.0.26-1.el8.x86_64.rpm
    mysql-community-debuginfo-8.0.26-1.el8.x86_64.rpm
    mysql-community-debugsource-8.0.26-1.el8.x86_64.rpm
    mysql-community-devel-8.0.26-1.el8.x86_64.rpm
    mysql-community-libs-8.0.26-1.el8.x86_64.rpm
    mysql-community-libs-debuginfo-8.0.26-1.el8.x86_64.rpm
    mysql-community-server-8.0.26-1.el8.x86_64.rpm
    mysql-community-server-debug-8.0.26-1.el8.x86_64.rpm
    mysql-community-server-debug-debuginfo-8.0.26-1.el8.x86_64.rpm
    mysql-community-server-debuginfo-8.0.26-1.el8.x86_64.rpm
    mysql-community-test-8.0.26-1.el8.x86_64.rpm
    mysql-community-test-debuginfo-8.0.26-1.el8.x86_64.rpm
    

    必须安装(注意顺序)

    [root@localhost server]# rpm -ivh mysql-community-common-8.0.26-1.el8.x86_64.rpm 
    
    rpm -ivh mysql-community-libs-8.0.13-1.el7.x86_64.rpm
    

    image-20210801134558790

    在命令后面加上--force --nodeps即可

    [root@localhost server]# rpm -ivh mysql-community-libs-8.0.26-1.el8.x86_64.rpm --force --nodeps
    

    同理,都要加上否则报错

    [root@localhost server]# rpm -ivh mysql-community-client-8.0.26-1.el8.x86_64.rpm --force --nodeps
    
    [root@localhost server]# rpm -ivh mysql-community-server-8.0.26-1.el8.x86_64.rpm  --force --nodeps
    
    

    非必要安装(注意顺序)我这里面没有,就不安装了

    rpm -ivh mysql-community-libs-compat-8.0.13-1.el7.x86_64.rpm
    rpm -ivh mysql-community-embedded-compat-8.0.13-1.el7.x86_64.rpm
    rpm -ivh mysql-community-devel-8.0.13-1.el7.x86_64.rpm
    rpm -ivh mysql-community-test-8.0.13-1.el7.x86_64.rpm
    
  4. 启动数据库

    [root@localhost server]# service mysqld start
    

    此时显示Redirecting to /bin/systemctl start mysqld.service
    Job for mysqld.service failed because the control process exited with error code. See "systemctl status mysqld.service" and "journalctl -xe" for details.

    初始化数据库

    service mysqld start

    但是出错了,上网参考了这个 :https://blog.csdn.net/LiMarrtin/article/details/110281420

    查看rpm -qa|grep mariadb,空的不用删除

    依然报错

    [root@localhost mysql]# rm -rf /var/lib/mysql
    [root@localhost mysql]# service mysqld restart
    Redirecting to /bin/systemctl restart  mysqld.service
    Job for mysqld.service failed because the control process exited with error code. See "systemctl status mysqld.service" and "journalctl -xe" for details.
    
    

    检查

    [root@localhost mysql]# systemctl status mysqld.service
    ● mysqld.service - MySQL Server
       Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
       Active: failed (Result: start-limit) since 日 2021-08-01 14:33:22 CST; 48s ago
         Docs: man:mysqld(8)
               http://dev.mysql.com/doc/refman/en/using-systemd.html
      Process: 99347 ExecStart=/usr/sbin/mysqld $MYSQLD_OPTS (code=exited, status=127)
      Process: 99317 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)
     Main PID: 99347 (code=exited, status=127)
    
    8月 01 14:33:22 localhost.localdomain systemd[1]: mysqld.service: main process exited, code=exited, status=127/n/a
    8月 01 14:33:22 localhost.localdomain systemd[1]: Failed to start MySQL Server.
    8月 01 14:33:22 localhost.localdomain systemd[1]: Unit mysqld.service entered failed state.
    8月 01 14:33:22 localhost.localdomain systemd[1]: mysqld.service failed.
    8月 01 14:33:22 localhost.localdomain systemd[1]: mysqld.service holdoff time over, scheduling restart.
    8月 01 14:33:22 localhost.localdomain systemd[1]: start request repeated too quickly for mysqld.service
    8月 01 14:33:22 localhost.localdomain systemd[1]: Failed to start MySQL Server.
    8月 01 14:33:22 localhost.localdomain systemd[1]: Unit mysqld.service entered failed state.
    8月 01 14:33:22 localhost.localdomain systemd[1]: mysqld.service failed.
    
    

    卸载mysql

    1、查看

    [root@localhost opt]# rpm -qa |grep -i mysql
    mysql-community-libs-8.0.26-1.el8.x86_64
    mysql-community-common-8.0.26-1.el8.x86_64
    mysql-community-client-8.0.26-1.el8.x86_64
    mysql-community-server-8.0.26-1.el8.x86_64
    
    

    2、yum remove 卸载

    yum remove mysql-community-client.x86_64 
    yum remove mysql-community-common.x86_64 
    
    

    卸载两个即可

    3、检查卸载干净与否

    [root@localhost opt]#  find / -name mysql
    /etc/selinux/targeted/active/modules/100/mysql
    /usr/lib64/mysql
    /opt/software/mysql
    

    挨个删除

    [root@localhost opt]#  rm -rf /etc/selinux/targeted/active/modules/100/mysql
    [root@localhost opt]#  rm -rf /usr/lib64/mysql
    [root@localhost opt]# rm -rf /opt/software/mysql
    
    

    mysqld --initialize --console

    使用yum安装mysql

    1、https://dev.mysql.com/downloads/repo/yum/

    image-20210801143909325

    2、下载后上传到/opt

    [root@localhost opt]# rz
    [root@localhost opt]# ls
    mysql80-community-release-el7-3.noarch.rpm
    

    3、 安装yum源

    [root@localhost opt]# yum -y localinstall mysql80-community-release-el7-3.noarch.rpm 
    
    

    4、在线安装

    yum -y install mysql-community-server
    

    报错:发现 13 个已存在的 RPM 数据库问题, 'yum check' 输出如下:
    mysql-community-libs-8.0.26-1.el8.x86_64 有缺少的需求 libc.so.6(GLIBC_2.28)(64bit)
    mysql-community-libs-8.0.26-1.el8.x86_64 有缺少的需求 libcrypto.so.1.1()(64bit)
    mysql-community-libs-8.0.26-1.el8.x86_64 有缺少的需求 libcrypto.so.1.1(OPENSSL_1_1_0)(64bit)
    mysql-community-libs-8.0.26-1.el8.x86_64 有缺少的需求 libcrypto.so.1.1(OPENSSL_1_1_1)(64bit)

    使用 yum 搜索 已经安装软件

    yum list installed
    

    删除掉已安装的

    yum remove mysql-community-common.x86_64
    

    然后重新

    yum -y install mysql-community-server
    
    image-20210801145214301
    [root@localhost opt]# service mysqld start
    Redirecting to /bin/systemctl start  mysqld.service
    

    执行完service mysqld start之后,系统没有像以往那样提示"启动成功",但,通过命令systemctl status mysqld查询mysql服务状态,发现mysql服务已经启动成功

    image-20210801145338904
  5. 初始化数据库

    mysqld --initialize --console
    
  6. 目录授权,否则启动失败

    chown -R mysql:mysql /var/lib/mysql/
    
  7. 启动msyql服务

    systemctl start mysqld
    
    image-20210801145623061
  8. 在/var/log/mysqld.log下查看临时密码

    cat /var/log/mysqld.log
    
    image-20210801150327751
  9. 用临时密码登录到数据库

    • mysql -u root -p + 回车键

    • 输入临时密码(输入时不会显示出来,输入完直接回车)

      [root@localhost opt]# mysql -u root -pM/k%A4nu6ZWV
      
  10. 执行下列mysql命令 修改密码
    alter USER 'root'@'localhost' IDENTIFIED BY '新密码';

    (新密码:必须包含:数字大小写字母特殊字符)

    mysql> alter USER 'root'@'localhost' IDENTIFIED BY 'root@123ROOT';
    Query OK, 0 rows affected (0.00 sec)
    
  11. 依次执行下列mysql命令 授权远程连接

    • 查看所有用户是否可以远程连接,依次执行下列命令:
      show databases;
      use mysql;
      select host, user, authentication_string, plugin from user;
    image-20210801151138722

    host为localhsot,说明用户只能本地连接msyql服务

    • 修改root用户host值,使root用户可以远程登录
      update user set host = "%" where user='root';

      image-20210801151116398
    • 刷新
      flush privileges;

    • 退出

      mysql> quit
      Bye
      
    • 远程登录到其他人的mysql

      mysql --host=ip地址 --user=用户名 --password = 密码

这篇关于安装mysql的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!