1. 下载上传解压

1.1 下载

image.png

1.2 上传

  1. $ rz

1.3 解压

  1. $ tar -zxvf mysql-5.7.36-linux-glibc2.12-x86_64.tar.gz
  2. $ mv mysql-5.7.36-linux-glibc2.12-x86_64 /home/flying/program/mysql-5.7.36

2. 配置

2.1 使用root用户创建mysql组及mysql用户,并授权

  1. $ groupadd mysql
  2. $ useradd -g mysql mysql

更改mysql-5.7.36 文件夹所有者属性和对应权限

  1. $ chown -R mysql:mysql /home/flying/program/mysql-5.7.36
  2. $ chown -R mysql /home/flying/program/mysql-5.7.36
  3. $ chmod -R 755 /home/flying/program/mysql-5.7.36

2.2 创建数据存放目录并授权

  1. $ mkdir data
  2. $ cd data
  1. $ chown -R mysql:mysql /home/mysql/data
  2. $ chown -R mysql /home/mysql/data
  3. $ chmod -R 755 /home/mysql/data

2.3 配置my.cnf文件

  1. $ vim /etc/my.cnf
  1. [mysqld]
  2. basedir=/home/flying/program/mysql-5.7.36
  3. datadir=/home/mysql/data
  4. socket=/var/lib/mysql/mysql.sock
  5. character_set_server=utf8mb4
  6. explicit_defaults_for_timestamp=true

3. 初始化mysql命令

  1. $ ./mysqld --user=mysql --basedir=/home/flying/program/mysql-5.7.36 --datadir=/home/mysql/data --initialize

4. 将mysql加入服务并设置开机自启动

  1. $ cp /home/flying/program/mysql-5.7.36/support-files/mysql.server /etc/init.d/mysql
  2. $ chkconfig mysql on

5. 启动mysql

  1. service mysql start

启动报错

  1. Starting MySQL.2022-05-22T12:46:45.126162Z mysqld_safe error: log-error set to '/var/log/mariadb/mariadb.log', however file don't exists. Create writable for user 'mysql'.
  2. ERROR! The server quit without updating PID file (/home/mysql/data/host2100.pid).

错误修复

  1. $ mkdir /var/log/mariadb
  2. $ touch /var/log/mariadb/mariadb.log
  3. $ chown -R mysql:mysql /var/log/mariadb/

重新启动出现socket异常

  1. Starting MySQL.2022-05-22T12:50:59.338574Z mysqld_safe Directory '/var/lib/mysql' for UNIX socket file don't exists

修复socket

  1. socket=/home/mysql/data/mysql.sock

再次重启,mysql成功启动

  1. Starting MySQL. SUCCESS!

6. linux系统内mysql命令登录测试

6.1 登录异常,bash: mysql: 未找到命令

因为/usr/local/bin目录下缺失mysql导致,只需要一下方法建立软链接,即可以解决:
把mysql安装目录,比如MYSQLPATH/bin/mysql,映射到/usr/local/bin目录下:

  1. $ cd /usr/local/bin
  2. $ ln -fs /MYSQLPATH/bin/mysql mysql

6.2 重新登录出现以下问题

  1. Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)

修复问题
原因找不到mysql.sock,解决:只需在tmp文件夹下创建一个软连接即可解决

  1. $ find / -name mysql.sock
  2. /home/mysql/data/mysql.sock #找到路径
  3. $ ln -s /home/mysql/data/mysql.sock /tmp/mysql.sock

6.3 重新登录,忘记初始密码,解决方案

  1. $ mysql -u root -p
  2. Enter password:
  3. ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)

在/etc/my.cnf中加入如下内容:在[mysqld]下添加skip-grant-tables,然后保存并退出

  1. [mysqld]
  2. skip-grant-tables #新增这一行
  3. basedir=/home/flying/program/mysql-5.7.36

重启mysql服务,重新登录无密码登录

  1. $ mysql -uroot -p

修改密码

  1. mysql> use mysql
  2. mysql> update user set authentication_string=password('123456') where user='root';
  3. mysql> flush privileges;
  4. mysql> exit;

密码修改后,最后把/etc/my.cnf中的skip-grant-tables注释掉,然后重启mysql

  1. [mysqld]
  2. #skip-grant-tables #新增这一行
  3. basedir=/home/flying/program/mysql-5.7.36
  1. $ service mysql restart
  2. $ mysql -uroot -p
  3. 输入新设置的密码登录成功

7. linux系统外通过客户端工具登录测试

7.1. 对外开放3306端口

  1. $ firewall-cmd --permanent --add-port=3306/tcp
  2. $ firewall-cmd --reload

7.2 通过客户端连接mysql

image.png
修改mysql root用户只允许本地连接为允许远程连接

  1. mysql> use mysql;
  2. mysql> update user set host='%' where user='root';
  3. mysql> flush privileges;
  4. mysql> select user,host from user;
  5. mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '123456' WITH GRANT OPTION;
  6. mysql> flush privileges;

再次重新连接
image.png
至此,mysql数据库安装成功