一、清理环境
查看是否已安装
#rpm -qa |grep -i mysql
#rpm -qa |grep -i mariadb --(centos7默认系统自带的查看版本号并进行删除)
#rpm -e mariadb-libs-5.5.56-2.el7.x86_64 --删除mariadb
# yum remove mariadb-libs-5.5.56-2.el7.x86_64 -- 删除mariadb
# yum install perl perl-devel //安装依赖包,根据各系统环境而定
二、开始安装
#--上传mysql rpm安装包到服务器(安装包可以去oracle官网下载)
# rpm -ivh mysql-commercial-common-5.7.28-1.1.el7.x86_64.rpm
# rpm -ivh mysql-commercial-libs-5.7.28-1.1.el7.x86_64.rpm
# rpm -ivh mysql-commercial-libs-compat-5.7.28-1.1.el7.x86_64.rpm
# rpm -ivh mysql-commercial-client-5.7.28-1.1.el7.x86_64.rpm
# rpm -ivh mysql-commercial-server-5.7.28-1.1.el7.x86_64.rpm
-----or--下载安装MySQL5.7社区版-----------------
wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-community-common-5.7.28-1.el7.x86_64.rpm
wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-community-libs-5.7.28-1.el7.x86_64.rpm
wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-community-client-5.7.28-1.el7.x86_64.rpm
wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-community-server-5.7.28-1.el7.x86_64.rpm
rpm -ivh mysql-community-common-5.7.28-1.el7.x86_64.rpm
rpm -ivh mysql-community-libs-5.7.28-1.el7.x86_64.rpm
rpm -ivh mysql-community-client-5.7.28-1.el7.x86_64.rpm
rpm -ivh mysql-community-server-5.7.28-1.el7.x86_64.rpm
---------------------------------------------------------------
三、启动&开机自启
# systemctl enable mysqld
# systemctl start mysqld
# systemctl status mysqld
四、获取随机root密码
# grep password /var/log/mysqld.log //日志文件查找临时密码
# mysql -u root -p
Enter password:
五、修改密码
修改当前登录用户
ALTER USER USER() IDENTIFIED BY "Abc123@#$";
六、用户管理
创建新用户
mysql> use mysql;
mysql> create user "admin"@"%" identified by "Abc123@#$";
Query OK, 0 rows affected (0.00 sec)
授权
grant select ,insert,update on db1.t1 to "admin"@"%" identified by "Abc123@#$";
grant all privileges on db1.* to "admin"@"%" identified by "Abc123@#$";
grant all privileges on *.* to "admin"@"%" identified by "Abc123@#$";
取消授权
revoke all on db1.t1 from "admin"@"%";
revoke all on db1.* from "admin"@"%";
revoke all privileges on *.* from "admin"@"%";
3)设置用户 root 可以在任意 IP 下被访问
grant all privileges on *.* to root@"%" identified by "Abc123@#$";
七、my.cnf参数配置调整MySQL数据存储目录datadir、socket
# systemctl stop mysqld
# cp -rp /var/lib/mysql /data --复制数据文件到新的目录
# chown -R mysql:mysql /data/mysql
# vi /etc/my.cnf
-----my.cnf修改------
[client]
port=3306
#socket=/var/lib/mysql/mysql.sock
socket=/data/mysql/mysql.sock #修改socket目录
[mysqld]
port=3306
#datadir=/var/lib/mysql
datadir=/data/mysql #修改数据文件目录
#socket=/var/lib/mysql/mysql.sock
socket=/data/mysql/mysql.sock #修改socket目录
------end------
[root@Centos7 tools]# systemctl start mysqld
Job for mysqld.service failed because the control process exited with error code. See "systemctl status mysqld.service" and "journalctl -xe" for details.
[root@Centos7 tools]# /usr/bin/mysql start
ERROR 2002 (HY000): Can"t connect to local MySQL server through socket "/data/mysql/mysql.sock" (2)
调整目录后,MySQL无法启动了,目录权限处理过,还是启动不了
方法一、
临时关闭SELinux:
# setenforce 0
永久关闭SELinux:
# vi /etc/sysconfig/selinux
然后将配置SELinux=enforcing改为SELinux=disabled
# sestatus ##查看SELinux状态
# systemctl start mysqld --关闭SELinux后MySQL可以正常启动
######datadir、socket路径修改后因为SELinux安全问题导致无法启动,在网上找了几天也没有人有说清楚,说到需关闭SELinux就已经不错了。
其实真正的问题就是datadir、socket、log修改后的新路径文件夹权限只有MySQL用户和用户组,没有其他的用户,所以受SELinux影响无法写入导致启动失败。
方法二、
# ls -ldZ /data/mysql ##查看目录的SElinux 标签
# yum provides semanage ###安装
# yum -y install policycoreutils-python.x86_64 ###安装
# chcon -Rv -u system_u -t mysqld_db_t /data/mysql
# semanage fcontext -a -t mysqld_db_t "/data/mysql(/.*)?"
# restorecon -Rv /data/mysql
# ls -ldZ /data/mysql ##再查看目录的SElinux 标签
# systemctl start mysqld ##处理后,MySQL启动正常
八、防火墙端口开放
# firewall-cmd --zone=public --add-port=3306/tcp --permanent
# firewall-cmd --reload
九、通过客户端远程连接测试。OK完成
原文地址:https://www.cnblogs.com/cny2022/archive/2022/04/01/16083112.html