1.安装MySQL的话会和MariaDB的文件冲突,所以需要先卸载掉MariaDB。查看是否安装mariadb
rpm -qa | grep mariadb
2. 卸载mariadb
rpm -e --nodeps 查看到的文件名
3.下载MySQL安装包
MySQL官网下载地址:
MySQL :: Download MySQL Community Serverhttps://dev.mysql.com/downloads/mysql/
把安装包解压,然后上传到 /usr/local目录下,并且重命名为mysql:
4.创建存储数据的data目录
mkdir /usr/local/mysql/data
5.创建用户组
groupadd mysql
6.创建用户
-r:创建系统用户
-g:指定用户组
useradd -r -g mysql mysql
7.修改目录所属用户和用户组
chown -R mysql:mysql /usr/local/mysql
8.修改权限
chmod -R 755 /usr/local/mysql
9.进入MySQL的bin目录
cd /usr/local/mysql/bin
10.初始化
./mysqld --initialize --user=mysql --datadir=/usr/local/mysql/data --basedir=/usr/local/mysql
初始化完成最后面会打印一个随机密码,后面会用到。
11.修改配置文件
vim /etc/my.cnf
配置文件修改为以下内容:
[client]
port = 3306
socket = /usr/local/mysql/data/mysql.sock
default-character-set = utf8mb4[mysql]
default-character-set = utf8mb4[mysqld]
character-set-client-handshake = FALSE
character-set-server = utf8mb4
collation-server = utf8mb4_general_ci
init_connect = 'SET NAMES utf8mb4'
port = 3306
socket = /usr/local/mysql/data/mysql.sock
basedir= /usr/local/mysql
datadir = /usr/local/mysql/data
12.修改 /etc/my.cnf 的权限
chmod 755 /etc/my.cnf
13.启动MySQL
/usr/local/mysql/support-files/mysql.server start
14.创建软链接,并重启MySQL
ln -s /usr/local/mysql/support-files/mysql.server /etc/init.d/mysql
ln -s /usr/local/mysql/bin/mysql /usr/bin/mysql
ln -s /usr/local/mysql/mysql.sock /var/mysql.sock
systemctl restart mysql
15.登录并修改密码
mysql -uroot -p初始化随机密码
alter user 'root'@'localhost' identified by '密码';
16.允许远程连接
mysql>use mysql;
msyql>update user set user.Host='%' where user.User='root';
mysql>flush privileges; //刷新权限
17.设置开机自启动。将服务文件复制到 /etc/init.d下,并重命名为mysqld
cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
18.修改权限
chmod 755 /etc/init.d/mysqld
19.添加服务
chkconfig --add mysqld