centos7采用通用二进制安装包方式安装mysql5.7

    技术2026-08-23  12

    1.去官网下载安装包到~目录

    例如:mysql-5.7.29-linux-glibc2.12-x86_64.tar.gz

    2.创建用户和组

    groupadd mysql useradd -g mysql -s /sbin/nologin mysql

    3.解压安装包

    tar -zxvf mysql-5.7.29-linux-glibc2.12-x86_64.tar.gz

     4.把解压后的安装包移动到规定的安装目录

    mv mysql-5.7.29-linux-glibc2.12-x86_64 /usr/local/mysql

    5.配置PATH

    echo "export PATH=$PATH:/usr/local/mysql/bin" >> /etc/profile source /etc/profile

    6.数据库目录规划和设置

    文件类型实例3306软链数据datadir/usr/local/mysql/data/data/mysql/data参数文件my.cnf/usr/local/mysql/etc/my.cnf 错误日志log-error/usr/local/mysql/log/mysql_error.log 二进制日志log-bin/usr/local/mysql/binlogs/mysql-bin/data/mysql/binlogs/mysql-bin慢查询日志slow_query_log_file/usr/local/mysql/log/mysql_slow_query.log 套接字socket文件/usr/local/mysql/run/mysql.sock pid文件/usr/local/mysql/run/mysql.pid 

     

     

     

     

     

     

     

     

    mkdir -p /data/mysql/{data,binlogs} mkdir -p /usr/local/mysql/{data,binlogs,log,etc,run} ln -s /data/mysql/binlogs /usr/local/mysql/binlogs ln -s /data/mysql/data /usr/local/mysql/data chown -R mysql.mysql /usr/local/mysql/{data,binlogs,log,etc,run} chown -R mysql.mysql /data/mysql

    7.配置my.cnf参数文件

    rm -f /etc/my.cnf

     在/usr/local/mysql/etc/下创建my.cnf文件,加入如下参数,其他参数根据需要配置

    vi /usr/local/mysql/etc/my.cnf

    在文件内键入以下内容

    [client] port = 3306 socket = /usr/local/mysql/run/mysql.sock [mysqld] port = 3306 socket = /usr/local/mysql/run/mysql.sock pid_file = /usr/local/mysql/run/mysql.pid datadir = /usr/local/mysql/data default_storage_engine = InnoDB max_allowed_packet = 512M max_connections = 2048 open_files_limit = 65535 explicit_defaults_for_timestamp=true skip-name-resolve lower_case_table_names=1 character-set-server = utf8mb4 collation-server = utf8mb4_unicode_ci init_connect='SET NAMES utf8mb4' innodb_buffer_pool_size = 1024M innodb_log_file_size = 2048M innodb_file_per_table = 1 innodb_flush_log_at_trx_commit = 0 key_buffer_size = 64M log-error = /usr/local/mysql/log/mysql_error.log log-bin = /usr/local/mysql/binlogs/mysql-bin slow_query_log = 1 slow_query_log_file = /usr/local/mysql/log/mysql_slow_query.log long_query_time = 5 tmp_table_size = 32M max_heap_table_size = 32M query_cache_type = 0 query_cache_size = 0 server-id=1

    8.初始化数据库

    mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data

    9.生成ssl连接方式

    mysql_ssl_rsa_setup --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data/

    10.设置启动项

    cd /usr/lib/systemd/system vi mysqld.service

    在文件内添加以下内容

    # Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved. # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; version 2 of the License. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA # # systemd service file for MySQL forking server # [Unit] Description=MySQL Server Documentation=man:mysqld(8) Documentation=http://dev.mysql.com/doc/refman/en/using-systemd.html After=network.target After=syslog.target [Install] WantedBy=multi-user.target [Service] User=mysql Group=mysql Type=forking PIDFile=/usr/local/mysql/run/mysqld.pid # Disable service start and stop timeout logic of systemd for mysqld service. TimeoutSec=0 # Execute pre and post scripts as root PermissionsStartOnly=true # Needed to create system tables #ExecStartPre=/usr/bin/mysqld_pre_systemd # Start main service ExecStart=/usr/local/mysql/bin/mysqld --daemonize --pid-file=/usr/local/mysql/run/mysqld.pid $MYSQLD_OPTS # Use this to switch malloc implementation EnvironmentFile=-/etc/sysconfig/mysql # Sets open_files_limit LimitNOFILE = 65535 Restart=on-failure RestartPreventExitStatus=1 PrivateTmp=false

    使服务生效,并设置开机自启

    systemctl daemon-reload systemctl enable mysqld.service systemctl is-enabled mysqld

    11.mysql服务管理命令

    systemctl start|status|stop mysqld.service

    12.查看mysql 进程

    ps -ef | grep mysql

    13.查看mysql 端口号监听

    netstat -anlp| grep 3306

    14.为mysql服务器做安全设置

    /usr/local/mysql/bin/mysql_secure_installation

     这里先要找到root用户的初始密码

    grep 'temporary password' /usr/local/mysql/log/mysql_error.log

     

    重新执行前面的命令

    /usr/local/mysql/bin/mysql_secure_installation

    用初始密码进去后,他让你重新设置密码

     

     

     

     

    我这里设置为123  这个要记住 等下要用的。

     

     

     

     

     

     

    这里他问你要不要安装密码校验插件。我们选择y

     

     

     

     

    这里问你需要插件校验密码的等级  我们选择0,实际情况 你们自己斟酌

     

    这里他问你是否需要重新设置root密码,我们选择no

     

     

     

     

     

     

    这里问你是否要删除匿名用户  我们选择删除 y

     

     

     

    这里问你是否禁用root用户远程登录 ,我们选择y

     

    这里问你是否删除test数据库  我们选择y

     

     

    这里问你是否重载权限表设置  我们选择 y

     

     

    出现这个字 说明安装好了。

     

    15.我们本地环境 登录mysql 看下

    mysql -uroot -p

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

    登录成功。

     

    16.为mysql设置远程访问

    新建mysql测试数据库 demo

     

    新建远程登录用户

    给密码插件设置长度为6位

     

     

    查看设置结果

    相关命令

    set global validate_password_length=6; SHOW VARIABLES LIKE 'validate_password%'; grant all privileges on demo.* to 'dzy'@'%' identified by '123456' with grant option;

    刷新权限

    FLUSH PRIVILEGES;

    查看用户

     

    17.测试远程访问 使用刚才创建的测试用户dzy

    先开放端口

    firewall-cmd --permanent --zone=public --add-port=3306/tcp

    重载防火墙配置

    firewall-cmd --reload

    设置连接

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

    出现这个效果 表示我们连接成功。

    Processed: 0.011, SQL: 9