Linux安装mysql

葛大爷 互联网 2022-03-10 273
  1. 下载Mysql5.7

    [root@VM-20-12-centos data]# wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.37-linux-glibc2.12-x86_64.tar.gz[root@VM-20-12-centos data]# tar -zxvf jdk-8u321-linux-x64.tar.gz

    [root@VM-20-12-centos data]# tar -zxvf mysql-5.7.37-linux-glibc2.12-x86_64.tar.gz

    [root@VM-20-12-centos data]# rm -rf mysql-5.7.37-linux-glibc2.12-x86_64.tar.gz

    [root@VM-20-12-centos data]# mv mysql-5.7.37-linux-glibc2.12-x86_64 /usr/local/mysql

  2. 创建数据目录并赋权限

    [root@VM-20-12-centos data]# mkdir mysql

    [root@VM-20-12-centos data]# groupadd mysql && useradd -r -g mysql mysql

    [root@VM-20-12-centos data]# chown mysql:mysql -R /data/mysql

  3. 修改配置文件

    [root@VM-20-12-centos data]# vim /etc/my.cnf

    [mysqld]

    bind-address=0.0.0.0

    port=3306

    user=mysql

    basedir=/usr/local/mysql

    datadir=/data/mysql

    socket=/data/mysql/mysql.sock

    character_set_server=utf8mb4

    explicit_defaults_for_timestamp=true

    symbolic-links=0

    log-error=/data/mysql/mysql.log

    pid-file=/data/mysql/mysql.pid

  4. 初始化(密码在最后一行中)

    [root@VM-20-12-centos data]# cd /usr/local/mysql/bin

    [root@VM-20-12-centos bin]# yum install -y libaio numactl

    [root@VM-20-12-centos bin]# ./mysqld --defaults-file=/etc/my.cnf --basedir=/usr/local/mysql/ --datadir=/data/mysql/ --user=mysql --initialize

    [root@VM-20-12-centos bin]# tail -1 /data/mysql/mysql.log

    2022-03-10T07:59:19.612476Z 1 [Note] A temporary password is generated for root@localhost: Dprirkmhy5/t

  5. 启动mysql

    [root@VM-20-12-centos bin]# cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysql

    [root@VM-20-12-centos bin]# service mysql start

    Starting MySQL. SUCCESS!

  6. 登录mysql

    [root@VM-20-12-centos bin]# /usr/local/mysql/bin/mysql -u root -h 127.0.0.1 -pmysql> SET PASSWORD = PASSWORD('gemaolin125');Query OK, 0 rows affected, 1 warning (0.00 sec)mysql> ALTER USER 'root'@'localhost' PASSWORD EXPIRE NEVER;Query OK, 0 rows affected (0.00 sec)mysql> FLUSH PRIVILEGES;Query OK, 0 rows affected (0.00 sec)

    Enter password:

  7. 修改密码

    mysql> SET PASSWORD = PASSWORD('123456');
    Query OK, 0 rows affected, 1 warning (0.00 sec)

    mysql> ALTER USER 'root'@'localhost' PASSWORD EXPIRE NEVER;
    Query OK, 0 rows affected (0.00 sec)

    mysql> FLUSH PRIVILEGES;
    Query OK, 0 rows affected (0.00 sec)