CentOS7安装配置mysql5.7 tar免安装版

in 编程
关注公众号【好便宜】( ID:haopianyi222 ),领红包啦~
阿里云,国内最大的云服务商,注册就送数千元优惠券:https://t.cn/AiQe5A0g
腾讯云,良心云,价格优惠: https://t.cn/AieHwwKl
搬瓦工,CN2 GIA 优质线路,搭梯子、海外建站推荐: https://t.cn/AieHwfX9

一、CentOS7.4系统自带mariadb

# 查看系统自带的Mariadb
[root@localhost~]# rpm -qa|grep mariadb
mariadb-libs-5.5.44-2.el7.centos.x86_64
# 卸载系统自带的Mariadb
[root@localhost ~]# rpm -e --nodeps mariadb-libs-5.5.44-2.el7.centos.x86_64
# 删除etc目录下的my.cnf
[root@localhost ~]# rm /etc/my.cnf

二、检查mysql是否存在

# 检查mysql是否存在

[root@localhost ~]# rpm -qa | grep mysql
[root@localhost~]# 


三、查看用户和组是否存在

1)检查mysql组合用户是否存在

# 检查mysql组和用户是否存在,如无则创建

[root@localhost~]# cat /etc/group | grep mysql 
[root@localhost~]# cat /etc/passwd | grep mysql 

# 查询全部用户(只是做记录,没必要执行) 

[root@localhost ~]# cat /etc/passwd|grep -v nologin|grep -v halt|grep -v shutdown|awk -F ":" '{print $1 "|" $3 "1" $4}' | more 
root|010
sync|510
lyt|100011000

2)若不存在,则创建mysql组和用户 

# 创建mysql用户组
[root@localhost~]# groupadd mysql
# 创建一个用户名为mysql的用户,并加入mysql用户组
[root@localhost~]# useradd -g mysql mysql
# 制定password 为mysql
[root@localhost~]# passwd mysql
Changing password for user mysql.
New password: 
BAD PASSWORD: The password is a palindrome
Retype new password: 
passwd: all authentication tokens updated successfully.

四、下载mysql的tar包

https://dev.mysql.com/downloads/mysql/5.7.html#downloads

 

五、上传第四步下载的mysql TAR包到

# 进入/usr/local/src文件夹
[root@localhost ~]# cd /usr/local/src/
# 上传mysql TAR包
# 解压tar文件
[root@localhost src]# tar xvf mysql-5.7.28-el7-x86_64.tar
# 解压mysql-5.7.28-el7-x86_64.tar.gz
[root@localhost src]# tar -zxvf mysql-5.7.28-el7-x86_64.tar.gz
# 解压后的文件移动到/usr/local文件夹
[root@localhost src]# mv mysql-5.7.28-el7-x86_64 /usr/local/mysql5.7

六、更改所属的组和用户

# 更改所属的组和用户
[root@localhost local]# chown -R mysql mysql5.7/
[root@localhost local]# chgrp -R mysql mysql5.7/
# 创建数据和日志存储目录
[root@localhost mysql5.7]# cd /data_disk/mysql
[root@localhost mysql]# mkdir data
[root@localhost mysql]# mkdir logs
# 修改所有者
[root@localhost mysql]# chown -R mysql:mysql data

七、在/etc下创建my.cnf文件

# 进入/etc文件夹下
[root@localhost mysql]# cd /etc
# 编辑my.cnf
[root@localhost etc]# vim my.cnf 

1)my.cnf添加如下内容:

[mysql]
# 设置mysql客户端默认字符集
default-character-set=utf8 

[mysqld]
# 设置3306端口
port = 3306 
# 设置mysql的安装目录
basedir=/usr/local/mysql5.7
# 设置mysql数据库的数据的存放目录
datadir=/data_disk/mysql/data
# 日志的相关配置
log-error=/data_disk/mysql/logs/error.log
# 不建议开日志,所有的操作都记录文件太大
# general-log=1
# log-output=FILE
# general-log-file=/data_disk/mysql/logs/mysql.log
slow_query_log=1
long_query_time=3
slow_query_log_file=/data_disk/mysql/logs/slowquery.log
# 允许最大连接数
max_connections=200
# 服务端使用的字符集默认为8比特编码的latin1字符集
character-set-server=utf8
# 创建新表时将使用的默认存储引擎
default-storage-engine=INNODB 
lower_case_table_names=1
max_allowed_packet=16M

八、进入mysql文件夹,并安装mysql

[root@localhost bin]# ./mysqld --initialize --user=mysql --basedir=/usr/local/mysql5.7 --datadir=/data_disk/mysql/data

九、设置开机启动

创建软连接

ln -s /usr/local/mysql5.7/bin/mysqld /usr/bin/mysqld
ln -s /usr/local/mysql5.7/bin/mysqld /usr/sbin/mysqld

ln -s /usr/local/mysql5.7/bin/mysql /usr/bin/mysql
ln -s /usr/local/mysql5.7/bin/mysql /usr/sbin/mysql

添加开机启动服务

[root@localhost bin]# vim /etc/systemd/system/mysqld.service

复制粘贴以下内容:

[Unit]
Description=MySQL Server
Documentation=man:mysqld(8)
Documentation=http://dev.mysql.com/doc/refman/en/using-systemd.html
After=network.target
After=syslog.target

[Install]
WantedBy=multi-user.target
[Service]
User=mysql
Group=mysql
Type=forking
PIDFile=/data_disk/mysql/mysqld.pid
# Disable service start and stop timeout logic of systemd for mysqld service.
TimeoutSec=0
# Execute pre and post scripts as root
PermissionsStartOnly=true
# Start main service
ExecStart=/usr/sbin/mysqld --daemonize --pid-file=/data_disk/mysql/mysqld.pid $MYSQLD_OPTS
# Sets open_files_limit
LimitNOFILE = 5000
Restart=on-failure
RestartPreventExitStatus=1
PrivateTmp=false

注意:ExecStart配置成自己的路径 

启动mysql服务

[root@localhost logs]# systemctl start mysqld
[root@localhost logs]# systemctl status mysqld
● mysqld.service - MySQL Server
   Loaded: loaded (/etc/systemd/system/mysqld.service; disabled; vendor preset: disabled)
   Active: active (running) since Fri 2019-11-15 18:08:33 CST; 5min ago
     Docs: man:mysqld(8)
           http://dev.mysql.com/doc/refman/en/using-systemd.html
  Process: 10770 ExecStart=/usr/sbin/mysqld --daemonize --pid-file=/data_disk/mysql/mysqld.pid $MYSQLD_OPTS (code=exited, status=0/SUCCESS)
 Main PID: 10772 (mysqld)
   CGroup: /system.slice/mysqld.service
           └─10772 /usr/sbin/mysqld --daemonize --pid-file=/data_disk/mysql/mysqld.pid

Nov 15 18:08:33 localhost.localdomain systemd[1]: Starting MySQL Server...
Nov 15 18:08:33 localhost.localdomain systemd[1]: Started MySQL Server.

如果启动失败,首先应该查看/data_disk/mysql/logs目录下的错误日志

,如果没有特别明显的,可以是selinux或者权限的问题

永久关闭selinux

[root@localhost logs]# vim /etc/selinux/config

设置SELINUX=disabled 

十、重置root密码查看随机生成的密码

[root@localhost logs]# vim /data_disk/mysql/logs/error.log

flush privileges

关注公众号【好便宜】( ID:haopianyi222 ),领红包啦~
阿里云,国内最大的云服务商,注册就送数千元优惠券:https://t.cn/AiQe5A0g
腾讯云,良心云,价格优惠: https://t.cn/AieHwwKl
搬瓦工,CN2 GIA 优质线路,搭梯子、海外建站推荐: https://t.cn/AieHwfX9
扫一扫关注公众号添加购物返利助手,领红包
Comments are closed.

推荐使用阿里云服务器

超多优惠券

服务器最低一折,一年不到100!

朕已阅去看看