一、准备环境
1、检查当前环境是否安装MySQL,如果有则删除卸载
rpm -qa | grep mysql
rpm -qa |grep mariadb
yum remove mariadb-libs-5.5.64-1.el7.x86_64
2、检查my.cnf文件,如果有则删除
rm /etc/my.cnf
rm /etc/mysql/my.cnf
二、下载安装MySQL
1、从官网下载MySQL5.7.34
1. 官网地址:https://downloads.mysql.com/archives/community
2. MySQL5.7.34 安装包下载地址:
https://downloads.mysql.com/archives/get/p/23/file/mysql-5.7.34-linux-glibc2.12-x86_64.tar.gz
2、安装MySQL 5.7.34
1.创建mysql用户
groupadd mysql
useradd -r -g mysql -s /bin/false mysql
2.解压安装包
cd /usr/local
tar zxvf mysql-5.7.34-linux-glibc2.12-x86_64.tar.gz -C /usr/local
ln -s mysql-5.7.34-linux-glibc2.12-x86_64 mysql
cd mysql
3.创建run、logs路径
mkdir run && chown mysql.mysql run
mkdir logs && chown mysql.mysql logs
mkdir /data/mysql/data && chown mysql.mysql /data/mysql/data
4.初始化mysql
bin/mysqld --initialize --user=mysql --datadir=/data/mysql/data
[Note] A temporary password is generated for root@localhost: :qq:&XSwq6HL
5. 修改my.cnf配置文件
[mysqld]
port = 3306
socket = /usr/local/mysql/run/mysql.sock
pid_file = /usr/local/mysql/run/mysql.pid
datadir = /usr/local/mysql/data
character-set-server = utf8mb4
6.初次启动mysql,记录此命令返回的随机密码
bin/mysqld_safe --user=mysql &
7.创建启动脚本
cp support-files/mysql.server /etc/init.d/mysql.server
/etc/init.d/mysqld status
/etc/init.d/mysqld start
/etc/init.d/mysqld stop
/etc/init.d/mysqld restart
8.设置mysql环境变量
echo 'export PATH=$PATH:/usr/local/mysql/bin' >> /etc/profile
source /etc/profile
3、修改MySQL密码,创建用户
mysql -u root -p':qq:&XSwq6HL'
ALTER USER 'root'@'localhost' IDENTIFIED BY 'root-password';
CREATE USER 'root'@'127.0.0.1' IDENTIFIED BY 'root-password';
CREATE USER 'root'@'::1' IDENTIFIED BY 'root-password';
create user 'wayne'@'%' identified by '123';
grant select,update,insert,delete on test.* to 'wayne'@'%';
4、创建mysql配置文件
1.my.cnf 文件优先级
/etc/my.cnf #Global options
/etc/mysql/my.cnf #Global options
SYSCONFDIR/my.cnf #Global options
$MYSQL_HOME/my.cnf #Server-specific options (server only)
2./etc/mysql/my.cnf
[client]
user = root
password = 123456
port = 3306
socket = /usr/local/mysql/run/mysql.sock
[mysqld]
port = 3306
socket = /usr/local/mysql/run/mysql.sock
pid_file = /usr/local/mysql/run/mysql.pid
datadir = /usr/local/mysql/data
character-set-server = utf8mb4
5、重启mysql生效/etc/mysql/my.cnf
1.重启mysqld
/etc/init.d/mysqld restart
2.设置开机自启动mysqld
chkconfig --add mysqld
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。