最新linux-centos7.7安装mysql步骤

  1. 下载mysql打包文件并解压放置到/usr/local/mysql

    1
    2
    3
    wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.24-linux-glibc2.12-x86_64.tar.gz
    cd /usr/local/
    mv mysql1/mysql-5.7.24-linux-glibc2.12-x86_64 mysql
  2. 查询依赖aio库并安装

    1
    2
    rpm -qa|grep libaio
    yum install libaio-devel.x86_64
  3. 创建mysql用户和用户组

    1
    2
    groupadd mysql
    useradd -r -g mysql mysql
  4. 创建mysql数据缓存目录

    1
    2
    mkdir -p /data/mysql
    chown mysql:mysql -R /data/mysql
  5. 配置mysql文件:my.cnf
    vim /etc/my.cnf

    修改内容:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    [mysqld]
    bind-address=0.0.0.0
    port=3306
    user=mysql
    basedir=/usr/local/mysql
    datadir=/data/mysql
    socket=/tmp/mysql.sock
    log-error=/data/mysql/mysql.err
    pid-file=/data/mysql/mysql.pid

    #character config
    character_set_server=utf8mb4
    symbolic-links=0
    explicit_defaults_for_timestamp=true

    #datadir=/var/lib/mysql
    #socket=/var/lib/mysql/mysql.sock
    # Disabling symbolic-links is recommended to prevent assorted security risks
    symbolic-links=0
    # 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
  6. 初始化数据库

    1
    2
    cd /usr/local/mysql/bin/
    ./mysqld --defaults-file=/etc/my.cnf --basedir=/usr/local/mysql/ --datadir=/data/mysql/ --user=mysql --initialize
  7. 查看默认密码

    1
    2
    3
    4
    5
    6
    7
    [root@iZbp12fcywzrb66n5bpk0dZ apache-tomcat-8.5.73]# cat /data/mysql/mysql.err
    2022-01-26T08:50:10.293406Z 0 [Warning] InnoDB: New log files created, LSN=45790
    2022-01-26T08:50:10.392999Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
    2022-01-26T08:50:10.455364Z 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: f8150827-7e84-11ec-99c0-00163e1ca90b.
    2022-01-26T08:50:10.457027Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
    2022-01-26T08:50:10.457722Z 1 [Note] A temporary password is generated for root@localhost: ,Kf3gMfS=jlU

    默认密码为:,Kf3gMfS=jlU

  8. 运行数据库服务

    1
    2
    cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysql
    service mysql start

    出现错误,没有对应的log存储文件,解决方式:

    1
    2
    3
    4
    mkdir /var/log/mariadb
    touch /var/log/mariadb/mariadb.log
    chown -R mysql:mysql /var/log/mariadb/
    service mysql start
  9. 测试数据库连接

    1
    2
    3
    4
    #内部
    /usr/local/mysql/bin/mysql -u root -p
    #阿里云
    /usr/local/mysql/bin/mysql -h rm-ax21bp1d7qzb23.mysql.rds.aliyuncs.com -u wei_wj
  10. 修改数据库连接密码

    1
    2
    3
    SET PASSWORD = PASSWORD('123456');
    ALTER USER 'root'@'localhost' PASSWORD EXPIRE NEVER;
    FLUSH PRIVILEGES;
  11. 修改数据库外部连接许可

    1
    2
    3
    4
    use mysql                                            #访问mysql库
    update user set host = '%' where user = 'root'; #使root能再任何host访问
    FLUSH PRIVILEGES;
    exit;
  12. 将mysql放入系统环境变量

    1
    ln -s  /usr/local/mysql/bin/mysql  /usr/bin/mysql

    可通过mysql,直接执行命令