一、清理环境
1、检查是否已经安装过mysql,执行命令
rpm -qa | grep mysql
如果查出来有安装包,使用如下命令清理
rpm -e --nodeps `rpm -qa | grep mysql`
再次执行查询命令,查看是否删除
2、查询所有Mysql对应的文件夹
whereis mysql
mysql: /usr/bin/mysql /usr/include/mysql
find / -name mysql
/data/mysql
/data/mysql/mysql
删除相关目录或文件
rm -rf /usr/bin/mysql /usr/include/mysql /data/mysql /data/mysql/mysql
验证是否删除完毕
whereis mysql
find / -name mysql
3、检查mysql用户组和用户是否存在,如果没有,则创建
cat /etc/group | grep mysql
cat /etc/passwd |grep mysql
groupadd mysql
useradd -s /sbin/nologin -M -g mysql mysql
二、安装部署mysql-5.7
1.创建mysql目录
mkdir /root/soft
cd /root/soft
2.下载安装包并解压
wget https://cdn.mysql.com/archives/mysql-5.7/mysql-5.7.28-linux-glibc2.12-x86_64.tar.gz
tar -xf mysql-5.7.28-linux-glibc2.12-x86_64.tar.gz -C /opt/
mv /opt/mysql-5.7.28-linux-glibc2.12-x86_64 /opt/mysql5.7.28/
3.创建软连接到指定目录下并更名
ln -s /opt/mysql5.7.28/ /usr/local/mysql
4.安装mysql5.7的依赖包
yum install -y libaio-devel numactl
5.设置环境变量
echo 'export PATH=$PATH:/usr/local/mysql/bin' >> /etc/profile
source /etc/profile
mysql -V
mysql Ver 14.14 Distrib 5.7.43, for Linux (x86_64) using EditLine wrapper
6.创建数据目录,并授权
mkdir /usr/local/mysql/data
mkdir /usr/local/mysql/logs
chown -R mysql:mysql /opt/mysql5.7.28/
chmod -R 755 /usr/local/mysql
7.编译安装并初始化mysql
cd /usr/local/mysql/bin
./mysqld --initialize-insecure --user=mysql --datadir=/usr/local/mysql/data --basedir=/usr/local/mysql
8.编辑配置文件my.cnf,添加配置如下
cat >/etc/my.cnf <<'EOF'
[mysqld] #服务端读取的配置
port=3306
user=mysql
basedir=/usr/local/mysql # 应用目录
datadir=/usr/local/mysql/data # 数据目录
socket=/usr/local/mysql/mysql.sock # socket文件目录
server-id=100 # 主机id,必须要区别于其他机器
log_bin=/usr/local/mysql/logs/mysql-bin # binlog日志目录
character_set_server=utf8mb4 # 设置全局字符为utf8mb4
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
symbolic-links=0
innodb_file_per_table=1
#表名大小写不明感
lower_case_table_names=1
gtid-mode=ON # 启用GTID复制
enforce-gtid-consistency=true # 强制确保GTID一致性
log-slave-updates=ON # 开启让slave从库更新binlog日志
[mysql] #在本地机器执行mysql读取的配置
socket=/usr/local/mysql/mysql.sock
EOF
三、启动脚本
cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
启动服务
systemctl enable mysqld
systemctl restart mysqld
systemctl status mysqld
如果想前台启动
mysqld_safe --defaults-file=/etc/my.cnf
四、开放远程连接
mysql>use mysql;
mysql>select Host,User from user;
msyql>update user set user.Host='%' where user.User='root';
mysql>flush privileges;
五、修改密码
mysql>set password for root@localhost = password('yourpass');
Query OK, 0 rows affected, 1 warning (0.00 sec)