1、创建用户组

    1. [root@todd ~]# groupadd mysql #创建一个mysql用户组
    2. [root@todd ~]# useradd -g mysql mysql -d /home/mysql #创建mysql用户
    3. [root@todd ~]# passwd mysql #设置密码
    4. Changing password for user mysql.
    5. New password:
    6. Retype new password:
    7. passwd: all authentication tokens updated successfully.

    2、下载tar文件并解压到指定目录

    [root@todd ~]# wget https://downloads.mysql.com/archives/get/p/23/file/mysql-8.0.27-linux-glibc2.12-x86_64.tar
    
    [root@todd ~]# tar -xvf mysql-8.0.27-linux-glibc2.12-x86_64.tar.xz             #解压文件
    mysql-8.0.27-linux-glibc2.12-x86_64/bin/
    mysql-8.0.27-linux-glibc2.12-x86_64/bin/myisam_ftdump
    ...
    [root@todd ~]# mv mysql-8.0.27-linux-glibc2.12-x86_64 /usr/local/mysql            #将解压后的文件移动到指定目录并重命名为mysql
    [root@todd ~]# cd /usr/local/
    [root@todd local]# chown -R mysql:mysql mysql            #赋予目录权限
    

    3、创建配置文件

    [root@todd local]# vim /etc/my.cnf            #将下面的文件复制到my.cnf内,并注释掉原有的
    [mysqld]
    character_set_server=utf8
    init_connect='SET NAMES utf8'
    basedir=/usr/local/mysql
    datadir=/usr/local/mysql/data
    socket=/tmp/mysql.sock
    log-error=/var/log/mysqld.log
    pid-file=/var/run/mysqld/mysqld.pid
    lower_case_table_names = 1
    max_connections=500
    

    4、初始化文件

    #给下面的文件赋予权限
    [root@todd local]# cd /var/log/
    [root@todd log]# vim mysqld.log
    [root@todd log]# chmod 777 mysqld.log 
    [root@todd log]# chown mysql:mysql mysqld.log 
    [root@todd log]# cd /var/run/
    [root@todd run]# mkdir mysqld
    [root@todd run]# cd mysqld/
    [root@todd mysqld]# vim mysqld.pid
    
    [root@todd ~]# cd /var/run/
    [root@todd run]# ls
    atd.pid      cron.reboot        log        setrans      udev
    auditd.pid   cryptsetup         mount      sshd.pid     user
    chrony       dbus               mysqld     sudo         utmp
    chronyd.pid  dhclient-eth0.pid  netreport  syslogd.pid
    cloud-init   faillock           nscd       systemd
    console      initramfs          plymouth   tmpfiles.d
    crond.pid    lock               sepermit   tuned
    [root@todd run]# chmod 777 mysqld
    [root@todd run]# chown -R mysql:mysql mysqld
    

    5、初始化数据库

    [root@todd run]# /usr/local/mysql/bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data 
    [root@todd run]# cat /var/log/mysqld.log| grep password
    2022-04-11T07:37:31.576550Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: mWVYKuyfk0_A
    

    6、启动数据库

    #从源目录启动数据库
    [root@todd run]# /usr/local/mysql/support-files/mysql.server start
    Starting MySQL..                                           [  OK  ]
    #或者
    systemctl start mysqld
    

    7、设置开机启动

    #复制启动脚本到资源目录
    [root@todd ~]# cp /usr/local/mysql/support-files/mysql.server /etc/rc.d/init.d/mysqld
    [root@todd ~]# chmod +x /etc/rc.d/init.d/mysqld            #增加mysqld服务控制脚本执行权限
    [root@todd ~]# chkconfig --add mysqld            #将mysqld服务加入到系统服务
    [root@todd ~]# chkconfig --list mysqld        #检查mysqld服务是否生效
    [root@todd ~]# su mysql
    #切换到mysql用户启动mysql
    [mysql@todd root]$ service mysqld start
    Starting MySQL[  OK  ]
    

    8、修改密码

    #系统默认会查找/usr/bin下的命令,建立一个链接文件,这样就可以使用mysql -u -p登录命令了
    [root@todd ~]# ln -s /usr/local/mysql/bin/mysql /usr/bin
    #登录mysql,修改密码
    [root@todd ~]# mysql -uroot -p
    Enter password: 
    Welcome to the MySQL monitor.  Commands end with ; or \g.
    Your MySQL connection id is 8
    Server version: 8.0.27
    
    Copyright (c) 2000, 2021, Oracle and/or its affiliates.
    
    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 'ht@todd';
    Query OK, 0 rows affected (0.01 sec)
    
    mysql>    exit
    Bye