目录
给 创建的/usr/local/mysql/data 和 /var/run/mysqld 目录赋予权限
打开/usr/local/mysql/my.cnf配置文件,删除skip_grant_tables
检查是否装有mariadb
rpm -qa | grep mariadb
卸载mariadb
rpm -e mariadb-libs-5.5.56-2.el7.x86_64 --nodeps
解压MySQL安装包到安装目录并重命名为MySQL
tar zxvf /h3cu/mysql-5.7.35-linux-glibc2.12-x86_64.tar.gz -C /usr/local/
cd /usr/local
mv mysql-5.7.35-linux-glibc2.12-x86_64 mysql
创建软链接
ln -s /usr/local/mysql-5.7.35-linux-glibc2.12-x86_64 /usr/local/mysql
创建一个目录负责存储MySQL的数据和生成数据库的连接文件
mkdir -p /usr/local/mysql/data
mkdir -p /var/run/mysqld
创建mysql用户组和用户
groupadd mysql
useradd -r -g mysql mysql
给 创建的/usr/local/mysql/data 和 /var/run/mysqld 目录赋予权限
chown mysql:mysql -R /usr/local/mysql/data/
chown mysql:mysql -R /var/run/mysqld
创建一个my.cnf文件并写入以下内容
vim /usr/local/mysql/my.cnf
[mysqld]
bind-address=0.0.0.0
port=3306
user=mysql
basedir=/usr/local/mysql/
datadir=/usr/local/mysql/data/
socket=/var/run/mysqld/mysqld.sock
log-error=/usr/local/mysql/data/mysql.err
pid-file=/usr/local/mysql/data/mysql.pid
character_set_server=utf8mb4
symbolic-links=0
explicit_defaults_for_timestamp=true
default_authentication_plugin=mysql_native_password
#解决Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)错误
[client]
port=3306
socket=/var/run/mysqld/mysqld.sock
配置环境变量
vim /root/.bash_profile
#mysql
export MYSQL_HOME=/usr/local/mysql
PATH=$MYSQL_HOME/bin:$PATH:$HOME/bin
使环境变量立即生效
source /root/.bash_profile
将MySQL进程放入系统进程中
cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
安装MySQL
mysqld --user=mysql --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data/ --initialize
启动MySQL
service mysqld start
出现这个就是启动成功
进入MySQL
mysql -u root
接下来可能会报错,报错的话在my.cnf文件中加上
skip_grant_tables
为root用户设置密码
flush privileges;
alter user 'root'@'localhost' identified by '123456';
打开/usr/local/mysql/my.cnf配置文件,删除skip_grant_tables
登录MySQL设置远程登录
mysql -u root -p
use mysql;
update user set host = '%' where user = 'root';
select host, user from user;
重启MySQL
service mysqld restart