Advertisement

Linux下安装Mysql数据库

阅读量:

1.版本说明和引用

Linux版本Centos7

复制代码
        1. [root@localhost ~]# cat /proc/version

    
        2. Linux version 3.10.0-1160.el7.x86_64 (mockbuild@kbuilder.bsys.centos.org) (gcc version 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC) ) #1 SMP Mon Oct 19 16:18:59 UTC 2020
    
    bash

Mysql版本8.4.0

安装方式手动rpm包安装

官方安装指导文档,其它系统和版本可参考文档MySQL :: MySQL 8.4 Reference Manual :: 2.5.4 Installing MySQL on Linux Using RPM Packages from Oracle

下载Mysql rpm包 MySQL :: Download MySQL Community Server (Archived Versions)
同时支持Linux8/9 选择相应版本下载
Bundle版本意思为合集,ARM和X86为系统架构,正常情况下是X86系统(Inter使用这个)

  • 导入虚拟机
复制代码
        1. [root@localhost opt]# pwd

    
        2. /opt
    
        3. [root@localhost opt]# ls
    
        4. mysql-8.4.0-1.el7.x86_64.rpm-bundle.tar
    
    bash

解压压缩包

复制代码
        1. [root@localhost opt]# tar xvf mysql-8.4.0-1.el7.x86_64.rpm-bundle.tar

    
        2. mysql-community-client-8.4.0-1.el7.x86_64.rpm
    
        3. mysql-community-client-plugins-8.4.0-1.el7.x86_64.rpm
    
        4. mysql-community-common-8.4.0-1.el7.x86_64.rpm
    
        5. mysql-community-debuginfo-8.4.0-1.el7.x86_64.rpm
    
        6. mysql-community-devel-8.4.0-1.el7.x86_64.rpm
    
        7. mysql-community-embedded-compat-8.4.0-1.el7.x86_64.rpm
    
        8. mysql-community-icu-data-files-8.4.0-1.el7.x86_64.rpm
    
        9. mysql-community-libs-8.4.0-1.el7.x86_64.rpm
    
        10. mysql-community-libs-compat-8.4.0-1.el7.x86_64.rpm
    
        11. mysql-community-server-8.4.0-1.el7.x86_64.rpm
    
        12. mysql-community-server-debug-8.4.0-1.el7.x86_64.rpm
    
        13. mysql-community-test-8.4.0-1.el7.x86_64.rpm
    
    bash
![](https://ad.itadn.com/c/weblog/blog-img/images/2025-08-17/qAXQGDTnVs0RcauYS8r1OWB53ZFJ.png)

所有rmp包内容如下

MysqlRPM包 | Rpm包名 | 简介 |

--- ---
mysql-community-client-plugins 客户端共享插件
mysql-community-common 客户/服务端共享库文件
mysql-community-devel 客户端开发者头/库文件
mysql-community-embedded-compat 嵌入式相关支持
mysql-community-icu-data-files 正则相关的ICU支持文件
mysql-community-libs 客户端/应用库文件
mysql-community-libs-compat 旧版兼容相关库文件
mysql-community-server 服务端和工具包
mysql-community-server-debug 服务调试和插件相关
mysql-community-test 服务器测试相关
mysql-community 源码文件
Additional debuginfo RPMs 可能还有其它debuginfo相关的rmp包

2.安装数据库

  1. 安装相应rpm包
复制代码
        1. [root@localhost opt]# ls

    
        2. mysql-8.4.0-1.el7.x86_64.rpm-bundle.tar
    
        3. mysql-community-client-8.4.0-1.el7.x86_64.rpm
    
        4. mysql-community-client-plugins-8.4.0-1.el7.x86_64.rpm
    
        5. mysql-community-common-8.4.0-1.el7.x86_64.rpm
    
        6. mysql-community-debuginfo-8.4.0-1.el7.x86_64.rpm
    
        7. mysql-community-devel-8.4.0-1.el7.x86_64.rpm
    
        8. mysql-community-embedded-compat-8.4.0-1.el7.x86_64.rpm
    
        9. mysql-community-icu-data-files-8.4.0-1.el7.x86_64.rpm
    
        10. mysql-community-libs-8.4.0-1.el7.x86_64.rpm
    
        11. mysql-community-libs-compat-8.4.0-1.el7.x86_64.rpm
    
        12. mysql-community-server-8.4.0-1.el7.x86_64.rpm
    
        13. mysql-community-server-debug-8.4.0-1.el7.x86_64.rpm
    
        14. mysql-community-test-8.4.0-1.el7.x86_64.rpm
    
        15. [root@localhost opt]# pwd
    
        16. /opt
    
        17. [root@localhost opt]# yum install mysql-community-{server,client,client-plugins,icu-data-files,common,libs}-*
    
    bash
![](https://ad.itadn.com/c/weblog/blog-img/images/2025-08-17/qytgQlTo0IZ8UEhw4WPR5YS6aHcd.png)

自动安装后默认配置如下

Files or Resources Location
Client programs and scripts /usr/bin
mysqld server /usr/sbin
Configuration file(配置文件) /etc/my.cnf
Data directory /var/lib/mysql
Error log file(错误日志) For RHEL, Oracle Linux, CentOS or Fedora platforms: /var/log/mysqld.log For SLES: /var/log/mysql/mysqld.log
Value of secure_file_priv /var/lib/mysql-files
System V init script For RHEL, Oracle Linux, CentOS or Fedora platforms: /etc/init.d/mysqld For SLES: /etc/init.d/mysql
Systemd service For RHEL, Oracle Linux, CentOS or Fedora platforms: mysqld For SLES: mysql
Pid file /var/run/mysql/mysqld.pid
Socket /var/lib/mysql/mysql.sock
Keyring directory /var/lib/mysql-keyring
Unix manual pages /usr/share/man
Include (header) files /usr/include/mysql
Libraries /usr/lib/mysql
Miscellaneous support files (for example, error messages, and character set files) /usr/share/mysql
  1. 启动停止Mysql
复制代码
        1. [root@localhost opt]# systemctl start mysqld

    
        2. [root@localhost opt]# systemctl restart mysqld
    
        3. [root@localhost opt]# systemctl stop mysqld
    
    bash
  1. 查看临时密码
复制代码
        1. [root@localhost opt]# grep 'temporary password' /var/log/mysqld.log

    
        2. 2024-08-13T02:20:22.872261Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: &9Y,cyZl_/gP
    
    bash

登陆mysql

复制代码
        1. [root@localhost opt]# mysql -uroot -p

    
        2. Enter password: 
    
        3. Welcome to the MySQL monitor.  Commands end with ; or \g.
    
        4. Your MySQL connection id is 10
    
        5. Server version: 8.4.0 MySQL Community Server - GPL
    
        6.  
    
        7. Copyright (c) 2000, 2024, Oracle and/or its affiliates.
    
        8.  
    
        9. Oracle is a registered trademark of Oracle Corporation and/or its
    
        10. affiliates. Other names may be trademarks of their respective
    
        11. owners.
    
        12.  
    
        13. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
    
        14.  
    
        15. mysql> 
    
    bash
![](https://ad.itadn.com/c/weblog/blog-img/images/2025-08-17/ti54ygpTMUmwvN7P9AFkIonKE3W1.png)

修改密码(validate_password插件默认安装要求密码强度至少包含一个大写字母、一个小写字母、一位数字和一个特殊字符,并且密码总长度至少为8个字符)

复制代码
    mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'MyNewPass4!';

    
    bash

修改远程连接

复制代码
        1. mysql> use mysql

    
        2. Reading table information for completion of table and column names
    
        3. You can turn off this feature to get a quicker startup with -A
    
        4.  
    
        5. Database changed
    
        6. mysql> update user set host="%" where user="root";
    
        7. Query OK, 0 rows affected (0.00 sec)
    
        8. Rows matched: 1  Changed: 0  Warnings: 0
    
    bash

开放防火墙3306端口

复制代码
        1. [root@localhost local]# firewall-cmd --zone=public --add-port=3306/tcp --permanent

    
        2. [root@localhost local]# firewall-cmd --reload
    
        3. [root@localhost local]# firewall-cmd --list-ports 查看所有开放的端口
    
    bash

把密码强度修改为简单密码,修改文件后重启Mysql服务后登陆,再次修改密码可以设置简单密码,参见步骤4,6,7(可选)

复制代码
        1. [root@localhost opt]# vi /etc/my.cnf

    
        2.  
    
        3. # For advice on how to change settings please see
    
        4. # http://dev.mysql.com/doc/refman/8.4/en/server-configuration-defaults.html
    
        5.  
    
        6. [mysqld]
    
        7. #
    
        8. # Remove leading # and set to the amount of RAM for the most important data
    
        9. # cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
    
        10. # innodb_buffer_pool_size = 128M
    
        11. #
    
        12. # Remove the leading "# " to disable binary logging
    
        13. # Binary logging captures changes between backups and is enabled by
    
        14. # default. It's default setting is log_bin=binlog
    
        15. # disable_log_bin
    
        16. #
    
        17. # Remove leading # to set options mainly useful for reporting servers.
    
        18. # The server defaults are faster for transactions and fast SELECTs.
    
        19. # Adjust sizes as needed, experiment to find the optimal values.
    
        20. # join_buffer_size = 128M
    
        21. # sort_buffer_size = 2M
    
        22. # read_rnd_buffer_size = 2M
    
        23.  
    
        24. datadir=/var/lib/mysql
    
        25. socket=/var/lib/mysql/mysql.sock
    
        26. bind-address=0.0.0.0
    
        27. log-error=/var/log/mysqld.log
    
        28. pid-file=/var/run/mysqld/mysqld.pid
    
        29. #设置密码强度策略为低
    
        30. validate_password.policy=LOW
    
        31. #设置密码长度为6个
    
        32. validate_password.length=6
    
    bash
![](https://ad.itadn.com/c/weblog/blog-img/images/2025-08-17/MAS8vDHQcz1g5RrbuLXmyeFJCPwW.png)

添加本机免密登陆(可选)

复制代码
        1. [root@localhost opt]# vi /etc/my.cnf

    
        2.  
    
        3. # For advice on how to change settings please see
    
        4. # http://dev.mysql.com/doc/refman/8.4/en/server-configuration-defaults.html
    
        5.  
    
        6. [mysqld]
    
        7. #
    
        8. # Remove leading # and set to the amount of RAM for the most important data
    
        9. # cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
    
        10. # innodb_buffer_pool_size = 128M
    
        11. #
    
        12. # Remove the leading "# " to disable binary logging
    
        13. # Binary logging captures changes between backups and is enabled by
    
        14. # default. It's default setting is log_bin=binlog
    
        15. # disable_log_bin
    
        16. #
    
        17. # Remove leading # to set options mainly useful for reporting servers.
    
        18. # The server defaults are faster for transactions and fast SELECTs.
    
        19. # Adjust sizes as needed, experiment to find the optimal values.
    
        20. # join_buffer_size = 128M
    
        21. # sort_buffer_size = 2M
    
        22. # read_rnd_buffer_size = 2M
    
        23.  
    
        24. datadir=/var/lib/mysql
    
        25. socket=/var/lib/mysql/mysql.sock
    
        26. bind-address=0.0.0.0
    
        27. log-error=/var/log/mysqld.log
    
        28. pid-file=/var/run/mysqld/mysqld.pid
    
        29. validate_password.policy=LOW
    
        30. validate_password.length=6
    
        31. #密码用户名写在[client]下
    
        32. [client]
    
        33. user=root
    
        34. password=123456
    
    bash
![](https://ad.itadn.com/c/weblog/blog-img/images/2025-08-17/1Nx3MjUci7OoCAGTYKDebvdIHfJk.png)

直接使用mysql命令可以登陆命令行无需再输入密码

复制代码
        1. [root@localhost opt]# mysql

    
        2. Welcome to the MySQL monitor.  Commands end with ; or \g.
    
        3. Your MySQL connection id is 12
    
        4. Server version: 8.4.0 MySQL Community Server - GPL
    
        5.  
    
        6. Copyright (c) 2000, 2024, Oracle and/or its affiliates.
    
        7.  
    
        8. Oracle is a registered trademark of Oracle Corporation and/or its
    
        9. affiliates. Other names may be trademarks of their respective
    
        10. owners.
    
        11.  
    
        12. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
    
        13.  
    
        14. mysql> 
    
    bash
![](https://ad.itadn.com/c/weblog/blog-img/images/2025-08-17/8Ws6g2If4vwnpLVtuAabSjCcHNGT.png)

全部评论 (0)

还没有任何评论哟~