#1. 创建用户
useradd mysql
#2.上传软件
[root@localhost ~]# cd /opt
[root@localhost ~]# yum install -y lrzsz
[root@localhost opt]# tar xf mysql-8.0.24-linux-glibc2.12-x86_64.tar.xz
[root@localhost opt]# ln -s /opt/mysql-8.0.24-linux-glibc2.12-x86_64
/usr/local/mysql
#3.修改环境变量
[root@localhost opt]# vi /etc/profile
export PATH=/usr/local/mysql/bin:$PATH
[root@localhost opt]# source /etc/profile
#4.测试环境变量
[root@localhost opt]# mysql -V
mysql Ver 8.0.24 for Linux on x86_64 (MySQL Community Server - GPL)
#5.创建数据目录,并授权
[root@localhost ~]# mkdir -p /data/3306/data
[root@localhost ~]# chown mysql.mysql /data
#6.创建配置文件
先将原有配置档备份一份
cp /etc/my.cnf /etc/my.cnf.bak
[root@localhost ~]# rm -rf /etc/my.cnf
[root@localhost ~]# vi /etc/my.cnf
[mysqld]
user=mysql
basedir=/usr/local/mysql
datadir=/data/3306/data
socket=/tmp/mysql.sock
server_id=51
[mysql]
socket=/tmp/mysql.sock
#7.初始化数据(建库)
[root@localhost ~]# mysqld --initialize-insecure
[Warning] [MY-010453] [Server] root@localhost is created with an empty password
! Please consider switching off the --initialize-insecure option.
或者:
[root@localhost ~]# mysqld --initialize
# 两种方法的区别:
--initialize : 初始化时,会自动创建超级管理员(root@'localhost'),生成随机密码,12位,4
种密码复杂度。这个密码,需要在第一次登陆时修改掉才可以正常管理数据。
#8.启动数据库
[root@localhost data]# cd /usr/local/mysql/support-files/
[root@localhost support-files]# ./mysql.server start
Starting MySQL.Logging to '/data/3306/data/localhost.localdomain.err'.
. [ OK ]
# SYS-V启动方式
[root@localhost support-files]# cp /usr/local/mysql/support-files/mysql.server
/etc/init.d/mysqld
[root@localhost ~]# service mysqld restart
Shutting down MySQL. [ OK ]
Starting MySQL.. [ OK ]
[root@localhost ~]# service mysqld stop
Shutting down MySQL.. [ OK ]
[root@localhost ~]# service mysqld start
Starting MySQL.. [ OK ]
[root@localhost ~]#
[root@localhost ~]# service mysqld status
MySQL running (11842) [ OK ]
[root@localhost ~]#
# systemd方式
[root@localhost ~]# service mysqld stop
Shutting down MySQL.. [ OK ]
[root@localhost ~]# systemctl enable mysqld
[root@localhost ~]# systemctl start mysqld
[root@localhost ~]# systemctl restart mysqld
[root@localhost ~]# systemctl stop mysqld
[root@localhost ~]# systemctl start mysqld
[root@localhost ~]# systemctl status mysqld
#9.安装后登陆
[root@db01 /data22:20:44]# mysql
# 如果遇到以下报错:
[ERROR] [MY-010457] [Server] --initialize specified but the data directory has
files in it. Aborting.
# 如果是全新环境:
[root@localhost ~]# rm -rf /data/3306/data/*
[root@localhost ~]# mysqld --initialize-insecure
[ERROR]
libaio ......
yum install -y libaio-devel