官方文档
https://downloads.mysql.com/archives/community/
https://dev.mysql.com/doc/refman/8.1/en/binary-installation.html
官方文档操作步骤
# Preconfiguration setup
$> groupadd mysql
$> useradd -r -g mysql -s /bin/false mysql
# Beginning of source-build specific instructions
$> tar zxvf mysql-VERSION.tar.gz
$> cd mysql-VERSION
# Postinstallation setup
$> cd /usr/local/mysql
$> mkdir mysql-files
$> chown mysql:mysql mysql-files
$> chmod 750 mysql-files
$> bin/mysqld --initialize --user=mysql
$> bin/mysql_ssl_rsa_setup
$> bin/mysqld_safe --user=mysql &
# Next command is optional
$> cp support-files/mysql.server /etc/init.d/mysql.server
环境准备
[root@node01 ~]# yum remove mariadb
[root@node01 ~]# yum install libaio-devel -y
[root@node01 ~]# rm -rf /etc/my.cnf
创建用户组和用户
[root@node01 ~]# id mysql
id: mysql: no such user
[root@node01 ~]# groupadd -g 54321 mysql
[root@node01 ~]# useradd -r -g mysql -s /bin/false -u 54321 mysql
[root@node01 ~]# id mysql
uid=54321(mysql) gid=54321(mysql) groups=54321(mysql)
下载、解压缩、创建软链接
1、wget下载mysql
[root@node01 ~]# wget https://downloads.mysql.com/archives/get/p/23/file/mysql-8.0.33-linux-glibc2.12-x86_64.tar
2、解压缩
[root@node01 ~]# tar -xf mysql-8.0.33-linux-glibc2.12-x86_64.tar
[root@node01 ~]# tar -xf mysql-8.0.33-linux-glibc2.12-x86_64.tar.xz -C /usr/local/cluster/
3、创建软链接
[root@node01 ~]# ln -s /usr/local/cluster/mysql-8.0.33-linux-glibc2.12-x86_64/ /usr/local/mysql
[root@node03 ~]# ll -h /usr/local/mysql/
total 292K
drwxr-xr-x 2 7161 31415 4.0K Mar 17 03:33 bin
drwxr-xr-x 2 7161 31415 38 Mar 17 03:33 docs
drwxr-xr-x 3 7161 31415 282 Mar 17 03:33 include
drwxr-xr-x 6 7161 31415 201 Mar 17 03:33 lib
-rw-r--r-- 1 7161 31415 279K Mar 17 01:22 LICENSE
drwxr-xr-x 4 7161 31415 30 Mar 17 03:33 man
-rw-r--r-- 1 7161 31415 666 Mar 17 01:22 README
drwxr-xr-x 28 7161 31415 4.0K Mar 17 03:33 share
drwxr-xr-x 2 7161 31415 77 Mar 17 03:33 support-files
创建数据目录
[root@node01 ~]# mkdir -p /data/mysql/3306/data
[root@node01 ~]# chown -R mysql:mysql /data/mysql/3306/data
参数配置
[root@node01 ~]# vim /etc/my.cnf
[client]
socket=/data/mysql/3306/data/mysql.sock
[mysql]
prompt="\\u@\\h [\\d]> "
[mysqld]
basedir=/usr/local/mysql
datadir=/data/mysql/3306/data
user=mysql
port=3306
pid-file=/data/mysql/3306/data/mysql.pid
socket=/data/mysql/3306/data/mysql.sock
log-error=/data/mysql/3306/data/mysql.err
log-timestamps=system
mysqlx_port=33060
mysqlx_socket=/data/mysql/3306/data/mysqlx.sock
初始化数据库
默认使用/etc/my.cnf参数文件
[root@node01 ~]# /usr/local/mysql/bin/mysqld --defaults-file=/etc/my.cnf --user=mysql --initialize
看到日志输出显示临时密码说明初始化成功
[root@node01 ~]# more /data/mysql/3306/data/mysql.err | grep temporary
2023-07-28T14:32:43.094407+08:00 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: d&e:Vf=ss4D<
启动数据库
[root@node01 ~]# /usr/local/mysql/bin/mysqld_safe --defaults-file=/etc/my.cnf &
[1] 77851
[root@node01 ~]# 2023-07-28T06:33:14.981353Z mysqld_safe Logging to '/data/mysql/3306/data/mysql.err'.
2023-07-28T06:33:15.026927Z mysqld_safe Starting mysqld daemon with databases from /data/mysql/3306/data
[root@node01 ~]# ps -ef |grep mysql | grep -v grep
root 77851 69072 0 14:33 pts/1 00:00:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --defaults-file=/etc/my.cnf
mysql 78013 77851 11 14:33 pts/1 00:00:03 /usr/local/mysql/bin/mysqld --defaults-file=/etc/my.cnf --basedir=/usr/local/mysql --datadir=/data/mysql/3306/data --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=/data/mysql/3306/data/mysql.err --pid-file=/data/mysql/3306/data/mysql.pid --socket=/data/mysql/3306/data/mysql.sock --port=3306
配置环境变量
[root@node01 ~]# vim .bash_profile
# .bash_profile
# Get the aliases and functions
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi
# User specific environment and startup programs
PATH=$PATH:$HOME/bin
export PATH
export JAVA_HOME=/usr/local/cluster/jdk
export PATH=$PATH:$JAVA_HOME/bin
export MYSQL_HOME=/usr/local/mysql
export PATH=$PATH:$MYSQL_HOME/bin
[root@node01 ~]# source .bash_profile
登录测试
登录成功
[root@node01 ~]# mysql -uroot -p -S /data/mysql/3306/data/mysql.sock
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.33
Copyright (c) 2000, 2023, 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.
修改密码
[root@node01 ~]# mysql -uroot -p -S /data/mysql/3306/data/mysql.sock
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.33
Copyright (c) 2000, 2023, 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.
root@localhost [(none)]> select * from mysql.user\G
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.
root@localhost [(none)]> alter user 'root'@'localhost' identified by 'oracle';
Query OK, 0 rows affected (0.00 sec)
使用配置文件启动
修改配置文件/usr/local/mysql/support-files/mysql.server
通过查看配置文件/usr/local/mysql/support-files/mysql.server
basedir默认/usr/local/mysql
datadir默认/usr/local/mysql/data,修改为/data/mysql/3306/data
配置文件默认是/etc/my.cnf,不需要修改
经过测试datadir不需要修改,因为配置文件默认是/etc/my.cnf,参数中指定了datadir的具体值
[root@node01 ~]# vim /usr/local/mysql/support-files/mysql.server
basedir=/usr/local/mysql
datadir=/data/mysql/3306/data
if test -z "$basedir"
then
basedir=/usr/local/mysql
bindir=/usr/local/mysql/bin
if test -z "$datadir"
then
datadir=/data/mysql/3306/data
fi
sbindir=/usr/local/mysql/bin
libexecdir=/usr/local/mysql/bin
else
bindir="$basedir/bin"
if test -z "$datadir"
then
datadir="$basedir/data"
fi
sbindir="$basedir/sbin"
libexecdir="$basedir/libexec"
fi
使用配置文件/usr/local/mysql/support-files/mysql.server启动
[root@node01 ~]# /usr/local/mysql/support-files/mysql.server start
Starting MySQL................ SUCCESS!
[root@node01 ~]# /usr/local/mysql/support-files/mysql.server status
SUCCESS! MySQL running (102015)
[root@node01 ~]# /usr/local/mysql/support-files/mysql.server stop
Shutting down MySQL... SUCCESS!
配置systemctl服务
[root@node01 ~]# vim /usr/lib/systemd/system/mysqld.service
[Unit]
Description=MySQL Server
Documentation=man:mysqld(8)
Documentation=http://dev.mysql.com/doc/refman/en/using-systemd.html
After=network.target
After=syslog.target
[Install]
WantedBy=multi-user.target
[Service]
User=mysql
Group=mysql
Type=notify
TimeoutSec=0
# Execute pre and post scripts as root
PermissionsStartOnly=true
# Start main service
#ExecStart=/usr/local/mysql/support-files/mysql.server start
ExecStart=/usr/local/mysql/bin/mysqld --defaults-file=/etc/my.cnf $MYSQLD_OPTS
# Use this to switch malloc implementation
#EnvironmentFile=-/etc/sysconfig/mysql
# Sets open_files_limit
@LimitNOFILE = 5000
Restart=on-failure
RestartPreventExitStatus=1
PrivateTmp=false
加载服务
[root@node01 ~]# systemctl daemon-reload
设置开机自启
[root@node01 ~]# systemctl enable mysqld.service
[root@node01 ~]# systemctl start mysqld.service
[root@node01 ~]# systemctl status mysqld.service
[root@node01 ~]# systemctl stop mysqld.service
[root@node01 ~]# systemctl disable mysqld.service
[root@node01 ~]# systemctl is-enabled mysqld.service
[root@node01 ~]# systemctl status mysqld.service
● mysqld.service - MySQL Server
Loaded: loaded (/usr/lib/systemd/system/mysqld.service; disabled; vendor preset: disabled)
Active: inactive (dead)
Docs: man:mysqld(8)
http://dev.mysql.com/doc/refman/en/using-systemd.html
Jul 27 19:20:44 node01 systemd[1]: [/usr/lib/systemd/system/mysqld.service:28] Unknown lvalue '@LimitNOFILE' in section 'Service'
启动mysql
[root@node01 ~]# systemctl start mysqld.service
[root@node01 ~]# systemctl status mysqld.service
● mysqld.service - MySQL Server
Loaded: loaded (/usr/lib/systemd/system/mysqld.service; disabled; vendor preset: disabled)
Active: active (running) since Thu 2023-07-27 19:24:00 CST; 2s ago
Docs: man:mysqld(8)
http://dev.mysql.com/doc/refman/en/using-systemd.html
Process: 113461 ExecStart=/usr/local/mysql/support-files/mysql.server start (code=exited, status=0/SUCCESS)
Main PID: 113725 (mysqld)
Tasks: 39
Memory: 291.3M
CGroup: /system.slice/mysqld.service
├─113474 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/data/mysql --pid-file=/data/mysql/mysql.pid
└─113725 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/data/mysql --plugin-dir=/usr/local/mysql/li...
Jul 27 19:23:59 node01 systemd[1]: Starting MySQL Server...
Jul 27 19:24:00 node01 systemd[1]: mysqld.service: Supervising process 113725 which is not our child. We'll most likely no... exits.
Jul 27 19:24:00 node01 systemd[1]: Started MySQL Server.
Hint: Some lines were ellipsized, use -l to show in full.
[root@node01 ~]# ps -ef |grep mysql
mysql 113474 1 0 19:23 ? 00:00:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/data/mysql --pid-file=/data/mysql/mysql.pid
mysql 113725 113474 4 19:23 ? 00:00:00 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/data/mysql --plugin-dir=/usr/local/mysql/lib/plugin --log-error=/data/mysql/mysql.err --pid-file=/data/mysql/mysql.pid --port=3306
root 113895 107141 0 19:24 pts/1 00:00:00 grep --color=auto mysql
配置文件最终版
[root@node01 ~]# more /usr/lib/systemd/system/mysqld.service
[Unit]
Description=MySQL Server
Documentation=man:mysqld(8)
Documentation=http://dev.mysql.com/doc/refman/en/using-systemd.html
After=network.target
After=syslog.target
[Install]
WantedBy=multi-user.target
[Service]
User=mysql
Group=mysql
Type=notify
TimeoutSec=0
# Execute pre and post scripts as root
PermissionsStartOnly=true
# Start main service
ExecStart=/usr/local/mysql/bin/mysqld --defaults-file=/etc/my.cnf $MYSQLD_OPTS
# Use this to switch malloc implementation
#EnvironmentFile=-/etc/sysconfig/mysql
# Sets open_files_limit
@LimitNOFILE = 5000
Restart=on-failure
RestartPreventExitStatus=1
PrivateTmp=false