MySQL安装

1、下载MySQL

  • mysql-5.7.35-linux-glibc2.12-x86_64.tar.gz

2、安装

1
2
3
4
cd /usr
mkdir tools
cd tools
mkdir mysql
  • 将下载文件上传到 mysql 目录中
1
2
3
4
5
6
7
8
9
tar -zxvf mysql-5.7.35-linux-glibc2.12-x86_64.tar.gz
mv mysql-5.7.35-linux-glibc2.12-x86_64 mysql-5.7.35
cat /etc/group|grep mysql #检查mysql组和用户是否存在
groupadd mysql
useradd -r -g mysql mysql  #useradd -r参数表示mysql用户是系统用户,不可用于登录系统
mkdir -p /data/mysql #在根目录创建数据目录,也可以是其他目录
chown mysql:mysql -R /data #赋予用户目录权限
chown -R mysql.mysql /data
vim /etc/my.cnf # 编辑完后保存文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
[mysqld]
# bind-address=127.0.0.1 暂时不配置

basedir=/usr/tools/mysql/mysql-5.7.35

datadir=/data/mysql/
socket=socket=/tmp/mysql.sock

user=mysql

port=3306

# 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

log-error=/data/mysql/mysql.err
pid-file=/data/mysql/mysql.pid

character_set_server=utf8mb4

explicit_defaults_for_timestamp=true

[mysqld_safe]
log-error=/data/mysql/mysql.err
pid-file=/data/mysql/mysql.pid

#
# include all files from the config directory
#
!includedir /etc/my.cnf.d
1
2
3
4
5
cd bin
./mysqld --defaults-file=/etc/my.cnf --basedir=/usr/tools/mysql/mysql-5.7.35/ --datadir=/data/mysql/ --user=mysql --initialize #执行mysql初始化
cat /data/mysql/mysql.err #查看该文件,找到 root@localhost: f8DSqswsuN;7
cp support-files/mysql.server /etc/init.d/mysql #将启动脚本拷贝到开机初始化目录下
service mysql start #启动mysql
  • 启动如果出现 Starting MySQL... ERROR! The server quit without updating PID file (/data/mysql/mysql.pid). 错误,ps -ef | grep mysqld 查看是否已经有进程,使用 pkill -9 mysqld 杀掉进程在启动

  • 在 bin 目录下 mysql -u root -h localhost -p 回车,然后输入上面查看到的密码

  • 修改 root 用户的密码

    1
    2
    3
    SET PASSWORD=PASSWORD('密码');
    ALTER USER 'root'@'localhost' PASSWORD EXPIRE NEVER;
    FLUSH PRIVILEGES;
  • 创建数据库用户

    1
    create user testUser IDENTIFIED by 'testUser';