数据库
首页 > 数据库> > centos7, centos8 安装mysql5.7

centos7, centos8 安装mysql5.7

作者:互联网

1. 下载mysql软件包

  我们可以先去官网查看我们应该下载哪个版本

  网址: https://downloads.mysql.com/archives/community/

  然后到镜像网站去下载,速度会快很多,例如,清华大学镜像网站,下载5.7版本的软件包:

  wget https://mirrors.tuna.tsinghua.edu.cn/mysql/downloads/MySQL-5.7/mysql-5.7.33-el7-x86_64.tar.gz      

2. 解压

  

cd /feng
tar -zxvf mysql-5.7.33-el7-x86_64.tar.gz

# 解压目录过长,可以建个软链接,或者改名字
ln -s mysql-5.7.33-el7-x86_64 ./mysql

 

3. 配置

  出于安全考虑,应该避免用root用户启动mysql,所以要创建一个mysql的启动用户

groupadd dbAdmin
## -r 代表建立一个系统用户,不会创建家目录,-g 指定用户组
## 建议使用man 命令查看 命令详情
useradd -r -g dbAdmin dbAdmin

  

  创建mysql 数据目录,赋予dbAdmin 用户权限

mkdir -p /feng/data/mysql
chown -R dbAdmin:dbAdmin /feng/data/mysql

  

  第二步 解压的安装包,也要赋予权限

  

 

 

   编写配置文件: vi /etc/my.cnf

[mysqld]
bind-address=0.0.0.0
port=3306
user=dbAdmin
basedir=/feng/mysql
datadir=/feng/data/mysql
socket=/feng/mysql/mysql.sock
pid-file=/feng/mysql/mysqld.pid
log-error=/var/log/mysqld.log
#character config
character_set_server=utf8mb4
symbolic-links=0
explicit_defaults_for_timestamp=true


[client]
port=3306
socket=/feng/mysql/mysql.sock

  创建上述配置中的文件, 授予dbAdmin 权限

touch /feng/mysql/mysql.sock
touch /feng/mysql/mysqld.pid
touch /var/log/mysqld.log

  

4. 初始化

cd /feng/mysql/bin
./mysqld --defaults-file=/etc/my.cnf --basedir=/feng/mysql/ --datadir=/feng/data/mysql --user=dbAdmin --initialize

  初始化成功后,会有初始密码在日志文件里

cat /var/log/mysqld.log |grep "password"
2021-02-21T02:07:12.865619Z 1 [Note] A temporary password is generated for root@localhost: jrwb>b?AX18l

 

5. 启动

bin/mysqld_safe --defaults-file=/etc/my.cnf --user=dbAdmin &

 

6. 客户端连接

bin/mysql -u root -p

  遇到以下报错及解决方法:

  

 

 

 

  登录成功后。修改密码:

  

 SET PASSWORD FOR 'root'@'localhost' = PASSWORD('yourpasswd');

 

7 其他

  

Failed to restart mysqld.service: Unit mysqld.service not found.
[root@localhost mysql]# service mysqld restart
Redirecting to /bin/systemctl restart mysqld.service
Failed to restart mysqld.service: Unit mysqld.service not found.

[root@localhost mysql]# cp support-files/mysql.server /etc/init.d/mysql

 

 使用DataGrip 连接时报错如下,说明mysql 没有允许其他客户端连接,只能localhost连接

 [HY000][1130] null,  message from server: "Host '192.168.31.62' is not allowed to connect to this MySQL server".

  

 

 解决如下:

update user set host = '%' where user = 'root';

 

标签:feng,dbAdmin,log,--,mysql5.7,centos7,mysqld,mysql,centos8
来源: https://www.cnblogs.com/fhb2011/p/14444436.html