一、卸载MariaDB
由于CentOS7自带MariaDB,安装MySQL会产生冲突,所以先将MariaDB彻底删除
1、卸载mariadb,同时也卸载mariadb-server
[root@localhost ~]$ yum remove mariadb
2、发现在安装mariadb时作为依赖项的mariadb-libs没有被删除,将其卸载
[root@localhost ~]$ yum list installed | grep mariadb
[root@localhost ~]$ yum remove mariadb-libs
3、删除MariaDB所有文件和所有包含mysql的文件
[root@localhost ~]$ rm -rf /etc/my.cnf
[root@localhost ~]$ rm -rf $(find / -name mysql)
二、下载MySQL安装包
1、进入MySQL官网下载页
[官网链接](MySQL :: MySQL Downloads)
2、点击MySQL Community (GPL) Downloads »
MySQL Community (GPL) Downloads »
3、选择MySQL Community Server
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传
4、选择系统类型,并且查看早期版本
5、选择安装包下载
此处奉上我的同款安装包链接
6、若使用虚拟机,提前将文件上传至用户Downloads文件夹下
上传教程省略,请自行百度
/home/CentOS7/Downloads #用户Downloads文件夹路径
三、解压和准备
1、解压mysql安装包到/usr/local下面
[CentOS7@localhost Downloads]$ sudo tar -zxvf mysql-5.7.17-linux-glibc2.5-x86_64.tar.gz -C /usr/local
2、切换到/usr/local目录下
[CentOS7@localhost Downloads]$ cd /usr/local
3、mysql安装包名字太长,为了方便输入,我们更改一下文件夹的名字
[CentOS7@localhost local]$ sudo mv mysql-5.7.17-linux-glibc2.5-x86_64 mysql
4、添加mysql用户组和mysql用户
[CentOS7@localhost local]$ su #切换到root用户
[root@localhost local]# groupadd mysql #添加mysql用户组
[root@localhost local]# useradd -r -g mysql mysql #在mysql用户组里面添加一个mysql用户
5、在mysql下面创建data数据库文件目录
[root@localhost /]# cd /usr/local/mysql
[root@localhost mysql]# mkdir data
四、安装数据库
1、安装数据库
[root@localhost root]# cd /usr/local/mysql/bin
[root@localhost bin]# ./mysqld --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --initialize
输出信息如下:
2022-10-21T09:36:03.163355Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2022-10-21T09:36:03.508396Z 0 [Warning] InnoDB: New log files created, LSN=45790
2022-10-21T09:36:03.565490Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2022-10-21T09:36:03.628245Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: c7cee00a-5123-11ed-9bef-080027ba9b19.
2022-10-21T09:36:03.632011Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2022-10-21T09:36:03.633553Z 1 [Note] A temporary password is generated for root@localhost: fE%sr6t9vIb1
初始化成功。
牢记上面的随机密码, 如上fE%sr6t9vIb1, 下面我们修改密码时需要用到。
2、修改mysql目录中除了data目录外的其他所有目录和文件均修改为root用户所有。
[root@localhost mysql]# chown -R root .
[root@localhost mysql]# chown -R mysql data
3、配置启动文件,使得mysql可以通过执行/etc/init.d/mysql.server start启动
[root@localhost mysql]# cp support-files/mysql.server /etc/init.d/mysql
4、编辑目录/etc/init.d下的mysql文件,添加路径设置:
basedir=/usr/local/mysql
datadir=/usr/local/mysql/data
配置结束后重新启动
[root@localhost mysql]# reboot
5、启动MySQL服务
[root@localhost mysql]# systemctl start mysql
[root@localhost mysql]# systemctl stop mysql #这是关闭mysql服务的命令
#老版本可以使用下面的命令
[root@localhost mysql]# service mysql start
[root@localhost mysql]# service mysql stop #这是关闭mysql服务的命令
6、配置环境变量
[root@localhost mysql]# vim /etc/bashrc
在最后添加如下内容
export MYSQL_HOME=/usr/local/mysql
export PATH=$PATH:$MYSQL_HOME/bin
刷新变量
[root@localhost mysql]# source /etc/bashrc
7、使用命令进入mysql
[root@localhost mysql]# mysql -u root -p
Enter password:
这里输入我们刚才记下的密码%sr6t9vIb1,成功进入mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.17
Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
8、修改root用户密码方便使用
mysql> set password for 'root'@'localhost'=password('123456') #123456是修改后的密码,请自行替换
9、可以配置mysql服务开机自启动
[root@localhost root]# systemctl enable mysql
#老版本可以使用下面的命令
[root@localhost root]# chkconfig --add mysql
[root@localhost root]# chkconfig mysql on //重启后永久生效
# 上面命令不起作用可以使用如下命令
[root@localhost root]# cd /etc/init.d
[root@localhost init.d]# update-rc.d mysql defaults 99
至此我们就安装成功了!