Centos7.5 mysql安装(新、更专业)

安装环境

系统环境:centos7.5
软件下载目录:/software
软件安装目录:/opt
软件版本:mysql-5.7.25-linux-glibc2.12-x86_64.tar.gz

一、下载mysql安装包

官网下载地址:
https://dev.mysql.com/downloads/mysql/5.7.html#downloads
操作系统选择:Linux-Generic
OS的版本选择:Linux-Generic(glibc 2.12)(x86,64-bit)
在这里插入图片描述

二、检查Centos7.5是否已经安装mysql

第一步:卸载系统自带的Mariadb

[root@cicd~]# rpm -qa|grep mariadb
mariadb-libs-5.5.60-1.el7_5.x86_64
[root@cicd ~]# rpm -e --nodeps mariadb-libs-5.5.60-1.el7_5.x86_64

第二步:检查mysql是否存在

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

安装mysql

第一步:创建压缩包存放路径

[root@cicd ~]# mkdir -p /home/bak/software

第二步:将mysql的压缩包放在/home/bak/software,并解压缩

[root@cicd software]# tar -zxvf mysql-5.7.25-linux-glibc2.12-x86_64.tar.gz

第三步:检查mysql用户组和mysql用户是否存在

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

不存在则创建,并检查是否创建成功:

[root@cicd ~]# groupadd mysql
[root@cicd ~]# cat /etc/group |grep mysql
[root@cicd ~]# useradd  -g mysql mysql -s /sbin/nologin
[root@cicd ~]# cat /etc/passwd|grep mysql

第四步:将安装包转移到/opt

[root@cicd ~]# cd /opt
[root@cicd opt]# mv /home/bak/software/mysql-5.7.25-linux-glibc2.12-x86_64 .

重命名:

[root@cicd opt]# mv mysql-5.7.25-linux-glibc2.12-x86_64 mysql-5.7.25

创建软连接:

[root@cicd opt]# ln -s mysql-5.7.25 mysql

创建/opt/mysql/data文件夹:

[root@cicd opt]# cd /opt/mysql
[root@cicd mysql]# mkdir data

修改data文件夹的权限:

[root@cicd mysql]# chown -R mysql:mysql data
[root@cicd mysql]# chmod -R 750 data

第五步:修改配置文件my.cnf

[client]
socket=/var/lib/mysql/mysql.sock
[mysql]
socket=/var/lib/mysql/mysql.sock
default-character-set=utf8
[mysqld]
#skip-grant-tables
basedir=/opt/mysql
datadir=/opt/mysql/data
socket=/var/lib/mysql/mysql.sock
max_connections=200
# 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
character-set-server=utf8
[mysqld_safe]
log-error=/opt/mysql/log/mariadb.log
pid-file=/var/run/mysql/mysql.pid
socket=/var/lib/mysql/mysql.sock
#
# include all files from the config directory
#
#!includedir /etc/my.cnf.d

**注意:**如果系统中没有一下目录或者文件,建议先创建,否则会导致mysql启动失败:

/opt/mysql/log/mariadb.log不存在,就创建

cd /opt/mysql
mkdir log
chown -R mysql:mysql log
chmod -R 750 log
cd log/
touch mariadb.log
chown mysql:mysql mariadb.log

/var/lib/mysql/不存在,就创建

cd /var/lib
mkdir mysql
chown -R mysql:mysql mysql
chmod -R 750 mysql

/var/run/mysql/不存在,就创建

cd /var/run/
mkdir mysql
chown -R mysql:mysql mysql
chmod 750 mysql

获取初始密码,启动mysql

第一步:初始化密码

[root@cicd mysql]# bin/mysqld --initialize  --user=mysql --basedir=/opt/mysql --datadir=/opt/mysql/data

运行上面的命令,可能会提示找不到"my.cnf.d"文件,这是只需把配置文件my.cnf中的"!includedir /etc/my.cnf.d"注释掉,即可。

第二步:安全启动mysql

[root@cicd mysql]#bin/mysqld_safe --user=mysql&
[root@cicd mysql]#bin/mysql -uroot -p

输入初始化密码(第八步获取的密码)

第三步:修改密码

mysql> use mysql;
mysql> set PASSWORD=password('新密码');
mysql> flush privileges;
mysql> quit

在这里插入图片描述

第四步:重启mysql

[root@cicd mysql]# bin/mysqladmin -uroot -p shutdown

在这里插入图片描述
第五步:忘记初始化密码

1.跳过MySQL的密码认证过程

#修改/etc/my.cnf文件
[root@cicd ~]# vim /etc/my.cnf

在[mysqld]后面任意一行添加“skip-grant-tables”用来跳过密码验证的过程

2.重启mysql

[root@cicd mysql]# bin/mysqladmin -uroot -p shutdown

3.重启之后输入mysql,进入mysql数据库
在这里插入图片描述
4.修改密码

值得注意的一点是,password(‘新密码’)中的password是密文模式,不加password系统识别不了密码

mysql> use mysql;
mysql> update user set authentication_string=password('新密码') where user='root';
mysql> flush privileges;
mysql> quit

5.编辑my.cnf文件

编辑my.cnf文件删掉skip-grant-tables 这一行,然后重启MySQL,否则MySQL仍能免密码登录

配置服务自启动

第一步:将mysql.server

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

第二步:修改脚本

 [root@cicd ~l]#cd /etc/init.d
 [root@cicd init.d]# vi mysql.server 

如下图所示,一共三处:
在这里插入图片描述
在这里插入图片描述
然后依次以以下三种方式对mysql进行启动、关闭、查看状态:

/etc/init.d/mysql.server stop
/etc/init.d/mysql.server start
/etc/init.d/mysql.server status

service mysql.server stop
service mysql.server start
service mysql.server status

systemctl start mysql
systemctl stop mysql
systemctl status mysql

猜你喜欢

转载自blog.csdn.net/weixin_43840640/article/details/88565877