获取安装链接
下载地址:https://dev.mysql.com/downloads/mysql/5.7.html#downloads
可以通过 wget 和 windows 下载后上传到服务器,只要服务器下载速度可以通过服务器下载
wget https://downloads.mysql.com/archives/get/p/23/file/mysql-5.7.31-linux-glibc2.12-x86_64.tar.gz
解压文件
我一般将mysql 文件安装到/usr/local/mysql 下,如果是下载下来的压缩包,放在/opt 下
## 解压下载下来的 gz 文件
tar -zxvf mysql-5.7.31-linux-glibc2.12-x86_64.tar.gz
cd mysql-5.7.31-linux-glibc2.12-x86_64 # 查看解压文件里面的内容
## 将解压的文件移动到 /usr/local/mysql 下
mv mysql-5.7.31-linux-glibc2.12-x86_64 /usr/local/mysql
配置my.cnf
在/etc 存在my.cnf 文件 通过编辑 my.cnf 修改mysql 启动的配置参数
## 配置本地客户端的配置
[client]
port=3309
# socket
socket=/data/mysql/mysql.sock
# 配置服务端的文件配置
[mysqld]
# 可以所有的ip 地址可以连接
bind-address=0.0.0.0
# 数据存放文件
datadir=/data/mysql
# socket
socket=/data/mysql/mysql.sock
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
# Settings user and group are ignored when systemd is used.
# If you need to run mysqld under a different user or group,
# customize your systemd unit file for mariadb according to the
# instructions in http://fedoraproject.org/wiki/Systemd
# 刚刚存放的启动的基础
basedir=/usr/local/mysql
# 归属用户名
user=mysql
# 端口号
port=3309
pid-file=/data/mysql/mysql.pid
log-error=/data/mysql/mysql.err
character_set_server=utf8mb4
[mysqld_safe]
log-error=/data/mysql/mariadb/mariadb.log
pid-file=/data/mysql/mariadb/mariadb.pid
#
# include all files from the config directory
#
!includedir /etc/my.cnf.d
新增对应mysql 的用户
groupadd mysql
useradd -r -g mysql mysql
创建数据目录并赋予权限
mkdir /data/mysql
mkdir /data/mysql/mariadb
chown mysql:mysql -R /data/mysql
初始化数据库
#到/usr/local/mysql/bin
cd /usr/local/mysql/bin
# 初始化数据库
./mysqld --defaults-file=/etc/my.cnf --basedir=/usr/local/mysql --datadir=/data/mysql --user=mysql --initialize
如果在初始数据库出现以下情况是因为缺少一个依赖包。
通过 安装libaio 依赖包就行了。
yum install -y libaio
在标识这个位置是初始密码。
启动mysql 服务
cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysql
service mysql start ## 启动mysql
service mysql status # 查看mysql 状态。
- 查看密码
cat /data/mysql/mysql.err
错误
因为在my.cnf 的时候我修改了mysqld_safe 的位置,所以导致无法没能够成功启动,创建/data/mysql/mariadb/mariadb.log 文件。
修改用户密码
通过软链接连接到/usr/local/mysql
ln -s /usr/local/mysql/bin/mysql /usr/bin/mysql
mysql -u root -p
# 输入在/data/mysql/mysql.err 文件下的密码
按照顺序执行以下三条sql 语句
SET PASSWORD = PASSWORD('123456');
ALTER USER 'root'@'localhost' PASSWORD EXPIRE NEVER;
FLUSH PRIVILEGES;