CentOS部署MySQL 5.7(详细)

Posted 程序员超时空

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了CentOS部署MySQL 5.7(详细)相关的知识,希望对你有一定的参考价值。

MySQL5.7安装步骤

  • 查看该操作系统上是否已经安装了mysql数据库

    rpm -qa | grep mysql
    
  • 如果已存在,进行删除( yum remove )

    [root@instance-38r7isl1 mysql]# rpm -qa|grep -i mysql
    mysql-libs-5.1.71-1.el6.x86_64
    [root@instance-38r7isl1 mysql]# service mysql status
    mysql: unrecognized service
    [root@instance-38r7isl1 mysql]# yum remove mysql-libs-5.1.71-1.el6.x86_64

    Removed:
    mysql-libs.x86_64 0:5.1.71-1.el6

    Dependency Removed:
    cloud-init.x86_64 0:0.7.5-10.el6.centos.2 cronie.x86_64 0:1.4.4-12.el6
    cronie-anacron.x86_64 0:1.4.4-12.el6 crontabs.noarch 0:1.10-33.el6
    postfix.x86_64 2:2.6.6-2.2.el6_1 redhat-lsb-core.x86_64 0:4.0-7.el6.centos
    sysstat.x86_64 0:9.0.4-22.el6

    Complete!
    [root@instance-38r7isl1 mysql]#

  • 没有则根据以下步骤安装

    1、在/usr/local目录下新建文件夹mysql
    2、将下载好的linux mysql安装包上传服务器

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

    3、解压文件

    [root@instance-38r7isl1 mysql]# tar -vxzf mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz

  • 配置MySQL
    1、建立mysql用户(mysql)和用户组(mysql),并创建数据库

    [root@instance-38r7isl1 mysql]# groupadd mysql
    [root@instance-38r7isl1 mysql]# useradd -r -g mysql mysql

    -r:表示mysql用户是一个系统用户

    2、创建数据库目录

    [root@instance-38r7isl1 mysql]# mkdir -p /var/mysql/data
    [root@instance-38r7isl1 mysql]# chown -R mysql:mysql /var/mysql

    3、初始化数据库(注意目录层级)

    [root@instance-38r7isl1 mysql-5.7.22-linux-glibc2.12-x86_64]# bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql/mysql-5.7.18-linux-glibc2.5-x86_64 --datadir=/var/mysql/data

    注意:初始化完成后,最后一行会生成一个临时登录密码,记下来。

    [root@instance-38r7isl1 mysql-5.7.22-linux-glibc2.12-x86_64]# bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql/mysql-5.7.22-linux-glibc2.12-x86_64 --datadir=/var/mysql/data
    2020-02-09T03:54:51.849852Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
    2020-02-09T03:54:52.849221Z 0 [Warning] InnoDB: New log files created, LSN=45790
    2020-02-09T03:54:53.009538Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
    2020-02-09T03:54:53.093615Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: ed876cce-4aef-11ea-ab5f-fa163e79b50a.
    2020-02-09T03:54:53.096645Z 0 [Warning] Gtid table is not ready to be used. Table ‘mysql.gtid_executed’ cannot be opened.
    2020-02-09T03:54:53.097556Z 1 [Note] A temporary password is generated for root@localhost: (k9.fR_%rh3=
    [root@instance-38r7isl1 mysql-5.7.22-linux-glibc2.12-x86_64]#

    注:如果初始化报错:

    [root@instance-38r7isl1 mysql-5.7.22-linux-glibc2.12-x86_64]# /bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql/mysql-5.7.22-linux-glibc2.12-x86_64 --datadir=/var/mysql/data
    -bash: /bin/mysqld: No such file or directory

    说明缺少安装包libaio和libaio-devel.执行以下命令进行安装

    [root@instance-38r7isl1 mysql-5.7.22-linux-glibc2.12-x86_64]# yum install -y libaio*

    如果报错:bin/mysqld: error while loading shared libraries: libnuma.so.1

    [root@VM-0-15-centos mysql-5.7.32-el7-x86_64]# yum -y install numactl

    3.1 把mysql加入搜索路径

    [root@instance-38r7isl1 mysql-5.7.22-linux-glibc2.12-x86_64]# vi /etc/profile
    [root@instance-38r7isl1 mysql-5.7.22-linux-glibc2.12-x86_64]# source /etc/profile

    export MYSQL_HOME=/usr/local/mysql/mysql-5.7.22-linux-glibc2.12-x86_64
    export PATH=$MYSQL_HOME/bin:$PATH
    

    刷新配置文件

    [root@instance-38r7isl1 mysql-5.7.22-linux-glibc2.12-x86_64]# source /etc/profile

    注:如果报错:cannot execute binary file

    [root@instance-38r7isl1 mysql-5.7.22-linux-glibc2.12-x86_64]#
    bin/mysqld --initialize --user=mysql –
    basedir=/usr/local/mysql/mysql-5.7.22-linux-glibc2.12-x86_64
    –datadir=/var/mysql/data -bash: bin/mysqld: cannot execute binary file

    检查系统位数:

    [root@instance-38r7isl1 /]# getconf LONG_BIT
    32

    与所安装软件位数不符,更换对应版本

    4、配置MySQL
    修改mysql配置文件my.cnf

    [root@instance-38r7isl1 mysql-5.7.22-linux-glibc2.12-x86_64]# whereis my.cnf
    my: /etc/my.cnf

    默认配置文件如下:

    [mysqld]
    datadir=/var/lib/mysql
    socket=/var/lib/mysql/mysql.sock
    user=mysql
    # Disabling symbolic-links is recommended to prevent assorted security risks
    symbolic-links=0
    [mysqld_safe]
    log-error=/var/log/mysqld.log
    pid-file=/var/run/mysqld/mysqld.pid
    [root@instance-38r7isl1 mysql-5.7.22-linux-glibc2.12-x86_64]#

    修改配置文件:

    [mysqld]
    datadir=/var/mysql/data
    socket=/var/mysql/data/mysql.sock
    # Disabling symbolic-links is recommended to prevent assorted security risks
    symbolic-links=0
    interactive_timeout=3000
    wait_timeout=3000
    character-set-server=utf8
    collation-server=utf8_general_ci
    # Settings user and group are ignored when systemd is used.
    # If you need to run mysqld under a different user or group,
    # customize your systemd unit file for mariadb according to the
    # instructions in http://fedoraproject.org/wiki/Systemd
    [mysqld_safe]
    # log-error=/var/log/mariadb/mariadb.log
    # pid-file=/var/run/mariadb/mariadb.pid
    log-error=/var/log/mysql/mysql.log
    pid-file=/var/mysql/data/mysql.pid
    [client]
    socket=/var/mysql/data/mysql.sock

    创建日志目录:

    [root@instance-38r7isl1 mysql-5.7.22-linux-glibc2.12-x86_64]# cd /var/log
    [root@instance-38r7isl1 log]# mkdir mysql
    [root@instance-38r7isl1 log]# chown -R mysql:mysql ./mysql/

    配置系统启动后自动启动mysqld

    [root@instance-38r7isl1 log]# cd /usr/local/mysql/mysql-5.7.22-linux-glibc2.12-x86_64
    [root@instance-38r7isl1 mysql-5.7.22-linux-glibc2.12-x86_64]# cp support-files/mysql.server /etc/init.d/mysqld
    [root@instance-38r7isl1 mysql-5.7.22-linux-glibc2.12-x86_64]# chmod 755 /etc/init.d/mysqld

    修改/etc/init.d/mysqld

    basedir=/usr/local/mysql/mysql-5.7.22-linux-glibc2.12-x86_64
    datadir=/var/mysql/data

    设定开机启动,并启动

    chkconfig --add mysqld
    chkconfig mysqld on
    service mysqld start

    执行结果并查看

    [root@instance-38r7isl1 data]# service mysqld start
    Starting MySQL.Logging to ‘/var/log/mysql/mysql.log’.
    [ OK ]
    [root@instance-38r7isl1 data]# ps -ef | grep mysql
    root 4324 1 0 12:25 pts/0 00:00:00 /bin/sh /usr/local/mysql/mysql-5.7.22-linux-glibc2.12-x86_64/bin/mysqld_safe --datadir=/var/mysql/data --pid-file=/var/mysql/data/instance-38r7isl1.pid
    mysql 4538 4324 2 12:25 pts/0 00:00:00 /usr/local/mysql/mysql-5.7.22-linux-glibc2.12-x86_64/bin/mysqld --basedir=/usr/local/mysql/mysql-5.7.22-linux-glibc2.12-x86_64 --datadir=/var/mysql/data --plugin-dir=/usr/local/mysql/mysql-5.7.22-linux-glibc2.12-x86_64/lib/plugin --user=mysql --log-error=/var/log/mysql/mysql.log --pid-file=/var/mysql/data/instance-38r7isl1.pid --socket=/var/mysql/data/mysql.sock
    root 4988 1836 0 12:26 pts/0 00:00:00 grep mysql
    [root@instance-38r7isl1 data]#

    密码修改和授权远程访问等

    [root@instance-38r7isl1 data]# mysql -umysql -p
    Enter password:
    ERROR 1045 (28000): Access denied for user ‘mysql’@‘localhost’ (using password: YES)
    [root@instance-38r7isl1 data]# mysql -uroot -p
    Enter password:
    Welcome to the MySQL monitor. Commands end with ; or g.
    Your MySQL connection id is 4
    Server version: 5.7.22
    Copyright 2000, 2018, 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> ALTER USER ‘root’@‘localhost’ IDENTIFIED BY ‘123456’;
    Query OK, 0 rows affected (0.00 sec)
    mysql> flush privileges;
    Query OK, 0 rows affected (0.00 sec)
    mysql> GRANT ALL PRIVILEGES ON *.* TO ‘root’@’%’ IDENTIFIED BY ‘123456’;
    Query OK, 0 rows affected, 1 warning (0.00 sec)
    mysql> flush privileges;
    Query OK, 0 rows affected (0.00 sec)
    mysql>

    使用Navicat连接测试通过

以上是关于CentOS部署MySQL 5.7(详细)的主要内容,如果未能解决你的问题,请参考以下文章

Centos部署MySQL 5.7

centos7部署MySQL 5.7

CentOS 7 安装 MySQL 5.7 详细步骤

CentOS 7 安装 MySQL 5.7 详细步骤

Centos7 安装MySQL 5.7图文详细教程

CentOS 7 部署MySQL 5.7(主从复制,传统方式及GTID)