本文主要是介绍linux 完整安装 mysql 5.7 及半路遇到的问题,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
1、下载 (mysql-5.7.31-linux-glibc2.12-x86_64.tar.gz)
下载mysql:官网下载略慢,可以在CSDN快速下载
2、解压,改名
[root@localhost mysql]# tar -zxfv mysql-5.7.31-linux-glibc2.12-x86_64.tar.gz -C /usr/local
[root@localhost mysql]# mv mysql-5.7.31-linux-glibc2.12-x86_64/ mysql-5.7.31
3、创建data文件夹
[root@localhost mysql]# cd mysql-5.7.31/
[root@localhost mysql]# mkdir data
4、查看是否有mysql用户,如果提示不存在,则创建mysql用户
[root@localhost mysql]# groups mysql
[root@localhost mysql]# groupadd mysql
[root@localhost mysql]# useradd -r -s /sbin/nologin -g mysql mysql -d /usr/local/mysql-5.7.31
5、给mysql用户赋权限
[root@localhost mysql]# chown -R mysql ./
[root@localhost mysql]# chgrp -R mysql ./
[root@localhost mysql]# chown -R mysql:mysql data
6、配置参数
bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql-5.7.31 --datadir=/usr/local/mysql-5.7.31/data
注意:此处会生成一个密码,记得要保存好密码
7、生成mysql密钥对
[root@localhost mysql]# bin/mysql_ssl_rsa_setup --datadir=/usr/local/mysql-5.7.31/data/mysql
8、复制server文件到指定目录
[root@localhost mysql]# cd support-files/
[root@localhost mysql]# cp mysql.server /etc/init.d/mysql
9、更改server文件中安装目录
[root@localhost mysql]# vi /etc/init.d/mysql
将红框内的内容修改为安装目录地址:
9、此时启动mysql服务可能报错
启动mysql服务指令:
[root@localhost mysql]# /etc/init.d/mysql start
# service mysql start # mysql服务名就是 /etc/init.d/mysql 中的mysql
# service mysql stop # mysql服务名就是 /etc/init.d/mysql 中的mysql
# service mysql status # mysql服务名就是 /etc/init.d/mysql 中的mysql
报错如下,原因是没有mariadb和mariadb.log。
[root@localhost mysql]# /etc/init.d/mysql start
Starting MySQL.2020-02-13T05:45:18.227723Z mysqld_safe error: log-error set to '/var/log/mariadb/mariadb.log', however file don't exists. Create writable for user 'mysql'.
ERROR! The server quit without updating PID file (/var/lib/mysql/bogon.pid).
创建日志目录及日志文件即可:
[root@localhost mysql]# cd /var/log/
[root@localhost mysql]# mkdir mariadb
[root@localhost mysql]# cd mariadb/
[root@localhost mysql]# touch mariadb.log
10、此时启动mysql服务可能报错:
[root@localhost mysql]# /etc/init.d/mysql start
Starting MySQL.2020-02-13T05:47:39.907062Z mysqld_safe Directory '/var/lib/mysql' for UNIX socket file don't exists.
ERROR! The server quit without updating PID file (/var/lib/mysql/bogon.pid).
修改my.cnf中的配置即可,将mysqld_safe下的配置注释:
11、此时启动mysql服务会显示启动成功
12、必要时可以创建软连接,启动mysql
[root@localhost mysql]# ln -s /usr/local/mysql-5.7.31/bin/mysql /usr/bin/
13、使用root用户登录mysql,密码就是刚才生成的密码
[root@localhost mysql]# mysql -hlocalhost -uroot -pMC=O3YI>ZEh?Amo
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.31 MySQL Community Server (GPL)
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MySQL [(none)]>
如果忘记root密码,按如下方法可以进行重置:
① 先修改/etc/my.cnf下配置,添加红框内配置
[root@localhost mysql]# vi /etc/my.cnf
② 重启mysql
[root@localhost mysql]# service mysql restart
③ 登录 mysql -uroot 直接回车
选择数据库
MySQL [(none)]> use mysql; 记得用 ; 或者\g 结尾
下面修改密码:
以前的版本我们用的是以下修改
MySQL [(none)]> update user set password=password('root') where user='root';
但是在5.7版本中不存在password字段,所有我们要用以下修改进行重置密码
MySQL [(none)]> update user set authentication_string=password('123456') where user='root';
执行
MySQL [(none)]> flush privileges;
④ 退出mysql
MySQL [(none)]> \q;
⑤ 将最开始修改的配置文件my.cnf中的skip-grant-tables删除
⑥ 重启mysql,重置完成
14、更改root账户密码,创建远程链接
此时已经进入mysql语句界面,输入行显示mysql>,注意sql语句使用分号结尾
MySQL [(none)]> set password=password('123456');
MySQL [(none)]> grant all privileges on *.* to 'root'@'%' identified by 'root';
MySQL [(none)]> flush privileges;
MySQL [(none)]> show databases;
MySQL [(none)]> \q
15、通过navicat或者其他工具远程链接
可能存在无法连接的情况,需要关闭防火墙和SELINUX
# 防火墙关闭
[root@localhost mysql]# systemctl stop firewalld.service
[root@localhost mysql]# systemctl disable firewalld.service
# 关闭selinux
[root@localhost mysql]# setenforce 0
[root@localhost mysql]# sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
16、设置开机自启
将myslq的server文件加入到自启动列表
[root@localhost mysql]# chkconfig --add /etc/init.d/mysql
查看自启列表
[root@localhost mysql]# chkconfig --list
Note: This output shows SysV services only and does not include native
systemd services. SysV configuration data might be overridden by native
systemd configuration.
If you want to list systemd services use 'systemctl list-unit-files'.
To see services enabled on particular target use
'systemctl list-dependencies [target]'.
mysql 0:off 1:off 2:on 3:on 4:on 5:on 6:off
netconsole 0:off 1:off 2:off 3:off 4:off 5:off 6:off
network 0:off 1:off 2:on 3:on 4:on 5:on 6:off
设置自启成功。
-----------------------------------至此mysql5.7 成功完成安装。
这篇关于linux 完整安装 mysql 5.7 及半路遇到的问题的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!