?1、前期准备
[root@localhost ~]# yum install bison-devel ncurses-devel libaio-devel wget -y
#安装包
#下载二进制包
[root@localhost ~]# wget https://cdn.mysql.com//Downloads/MySQL-5.7/mysql-5.7.38-linux-glibc2.12-x86_64.tar.gz
#移除mariadb-libs
[root@localhost ~]# yum remove -y mariadb-libs
创建mysql用户
[root@localhost ~]# useradd mysql
?2、basedir目录
#创建opt目录
[root@localhost ~]# mkdir -p /opt/
#授权
[root@localhost ~]# chown 755 /opt/
[root@localhost ~]# cp mysql-5.7.38-linux-glibc2.12-x86_64.tar.gz /opt/
[root@localhost ~]# cd /opt/
[root@localhost opt]# ls
mysql-5.7.38-linux-glibc2.12-x86_64.tar.gz
#解压
[root@localhost opt]# tar zxvf mysql-5.7.38-linux-glibc2.12-x86_64.tar.gz
[root@localhost opt]# ls
mysql-5.7.38-linux-glibc2.12-x86_64 mysql-5.7.38-linux-glibc2.12-x86_64.tar.gz
#重命名
[root@localhost opt]# mv mysql-5.7.38-linux-glibc2.12-x86_64 mysql
#授权
[root@localhost ~]# chown -R mysql:mysql /opt/mysql
#以上时basedir 目录的操作 在初始化的时候会用到这个目录/opt/mysql
?3、环境变量
[root@localhost opt]# cat /etc/profile.d/mysql.sh
export PATH=/opt/mysql/bin:$PATH
[root@localhost opt]# source /etc/profile
4、创建datadir目录(同样是初始化用到)
[root@localhost bin]# mkdir -p /data01/mysql-data/workdbs
[root@localhost bin]# chown 755 /data01/mysql-data/workdbs
[root@localhost bin]# chown -R mysql:mysql /data01/mysql-data/workdbs
5、初始化
#这个目录在basedir 解压重命名的文件下
[root@localhost bin]# pwd
/opt/mysql/bin
#初始化分为--initialize-insecure 不安全初始化后数据库无密码和--initialize 安全初始化后数据库随机密码
#初始化
[root@localhost bin]# mysqld --initialize-insecure --user=mysql --basedir=/opt/mysql --datadir=/data01/mysql-data/workdbs
2022-05-10T08:44:51.617964Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2022-05-10T08:44:52.523595Z 0 [Warning] InnoDB: New log files created, LSN=45790
2022-05-10T08:44:52.594258Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2022-05-10T08:44:52.657126Z 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: 759ede4f-d03d-11ec-b685-000c29290661.
2022-05-10T08:44:52.658697Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2022-05-10T08:44:52.952512Z 0 [Warning] A deprecated TLS version TLSv1 is enabled. Please use TLSv1.2 or higher.
2022-05-10T08:44:52.952533Z 0 [Warning] A deprecated TLS version TLSv1.1 is enabled. Please use TLSv1.2 or higher.
2022-05-10T08:44:52.953223Z 0 [Warning] CA certificate ca.pem is self signed.
2022-05-10T08:44:52.963333Z 1 [Warning] root@localhost is created with an empty password ! Please consider switching off the --initialize-insecure option.
#上面命令的意思时初始化无密码 用户mysql basedir目录及解压包路径 datadir目录为为我新创建的目录注意权限
6、数据库配置文件
[root@localhost ~]# cat /etc/my.cnf
[mysqld]
basedir = /opt/mysql
bind-address = 0.0.0.0
datadir = /data01/mysql-data/workdbs
tmpdir = /data01/mysql-data/tmp/
port = 3306
socket =/opt/mysql/lib/mysql.sock
lower_case_table_names=1
character-set-server = utf8
max_allowed_packet = 150M
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES,STRICT_ALL_TABLES
log-error=/data01/mysql-data/log/mysql_3306.log
max_connections=5000
lower_case_table_names=1
max_connect_errors=1000
event_scheduler=ON
[mysql]
default-character-set = utf8
socket =/opt/mysql/lib/mysql.sock
注意我的配置文件
basedir = /opt/mysql #解压路径
bind-address = 0.0.0.0#本地及0.0.0.0 都可以
datadir = /data01/mysql-data/workdbs #数据存储路径
tmpdir = /data01/mysql-data/tmp/# tmp路径
log-error=/data01/mysql-data/log/mysql_3306.log# 注意mysql_3306.log不目录,是文件
7、创建所需的文件
[root@localhost bin]# mkdir -p /data01/mysql-data/log
[root@localhost bin]# chown 755 /data01/mysql-data/log/
[root@localhost bin]# chown -R mysql:mysql /data01/mysql-data/log/
[root@localhost bin]# touch /data01/mysql-data/log/mysql_3306.log
[root@localhost bin]# chown -R mysql:mysql /data01/mysql-data/log/mysql_3306.log
[root@localhost bin]# mkdir -p /data01/mysql-data/tmp/
[root@localhost bin]# chown -R mysql:mysql /data01/mysql-data/tmp/
[root@localhost mysql-data]# pwd
/data01/mysql-data
[root@localhost mysql-data]# ls
log tmp workdbs
[root@localhost mysql-data]#
8、启动服务并测试进入数据库,然后关闭
#启动
[root@localhost ~]# cd /opt/mysql/support-files/
[root@localhost support-files]# ./mysql.server start
Starting MySQL.. SUCCESS!
[root@localhost support-files]# mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.38 MySQL Community Server (GPL)
Copyright (c) 2000, 2022, 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> exit
Bye
[root@localhost support-files]# ./mysql.server start
Shutting down MySQL.... SUCCESS!
[root@localhost support-files]#
9、配置systemd服务
[root@localhost support-files]# cp /opt/mysql/support-files/mysql.server /etc/init.d/mysqld
[root@localhost support-files]# cat /usr/lib/systemd/system/mysqld.service
[Unit]
Description=MySQL Server
Documentation=man:mysqld(8)
Documentation=https://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
PIDFile=/opt/data/mysqld.pid
ExecStart=/opt/mysql/bin/mysqld_safe --defaults-file=/etc/my.cnf
Restart=on-failure
RestartPreventExitStatus=1
TimeoutSec=0
PrivateTmp=false
LimitNOFILE = 5000
10 启动服务器
[root@localhost support-files]# systemctl start mysqld
[root@localhost support-files]# systemctl status mysqld
● mysqld.service - MySQL Server
Loaded: loaded (/usr/lib/systemd/system/mysqld.service; disabled; vendor preset: disabled)
Active: active (running) since Tue 2022-05-10 06:02:51 EDT; 7s ago
Docs: man:mysqld(8)
https://dev.mysql.com/doc/refman/en/using-systemd.html
Main PID: 9937 (mysqld_safe)
CGroup: /system.slice/mysqld.service
├─ 9937 /bin/sh /opt/mysql/bin/mysqld_safe --defaults-file=/etc/my.cnf
└─10188 /opt/mysql/bin/mysqld --defaults-file=/etc/my.cnf --basedir=/opt/mysql --dat...
May 10 06:02:51 localhost.localdomain systemd[1]: Started MySQL Server.
May 10 06:02:51 localhost.localdomain mysqld_safe[9937]: 2022-05-10T10:02:51.311404Z mysqld_s...'.
May 10 06:02:51 localhost.localdomain mysqld_safe[9937]: 2022-05-10T10:02:51.415507Z mysqld_s...bs
Hint: Some lines were ellipsized, use -l to show in full.
[root@localhost support-files]# mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.38 MySQL Community Server (GPL)
Copyright (c) 2000, 2022, 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> ^C
mysql> exit
[root@localhost support-files]# systemctl daemon-reload
如过出现找不到服务修改
[root@localhost support-files]# vi /etc/init.d/mysqld
basedir= /opt/mysql
datadir= /data01/mysql-data/workdbs
Can't start server: can't create PID file: 没权限
在/etc/my.cnf 中查看pid-file的位置
pid-file=/var/run/mysql/mysqld.pid
创建对应的目录并修改权限
mkdir -p /var/run/mysql chown mysql.mysql /var/run/mysql 这时可以尝试重启mysql了,如果还不行继续第三步
创建一个pid文件并修改权限
touch /var/run/mysql/mysqld.pid chown mysql.mysql /var/run/mysql/mysqld.pid 再启动MySQL应该就没问题了。 ?
|