一,安装环境(最小化安装)
[root@mysql57 ~]# cat /etc/redhat-release
CentOS Linux release 7.7.1908 (Core)
[root@mysql57 ~]# uname -r
3.10.0-1062.el7.x86_64
二,关闭SELinux和防火墙
[root@mysql57 ~]# setenforce 0 #临时关闭SELinux
[root@mysql57 ~]# sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config #永久关闭SELinux 关机重启后生效
三,MySQL5.7版本安装过程
1.配置yum源
[root@mysql57 ~]# cd /etc/yum.repos.d/
[root@mysql57 yum.repos.d]# ls | grep -v CentOS-Base.repo |xargs rm -rf
[root@mysql57 yum.repos.d]# mv CentOS-Base.repo CentOS-Base.repo.backup #备份
[root@mysql57 yum.repos.d]# curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo #下载新的 CentOS-Base.repo 到 /etc/yum.repos.d/
[root@mysql57 yum.repos.d]# yum clean all #清空yum缓存
[root@mysql57 yum.repos.d]# yum makecache # 运行 yum makecache 生成缓存
2.常用软件安装
[root@mysql57 ~]# yum -y install bash-completion vim net-tools tree lrzsz wget nskookup bind-utils zip unzip lsof
3.安装扩展libaio依赖包
[root@mysql57 ~]# yum -y install libaio libaio-devel
4.创建MySQL用户组和用户
[root@mysql57 ~]# groupadd mysql
[root@mysql57 ~]# useradd -g mysql -s /sbin/nologin mysql -M #创建不可登入用户,不创建家目录
5.解压mysql二进制包并安装
[root@mysql57 ~]# tar -zxvf mysql-5.7.28-linux-glibc2.12-x86_64.tar.gz
[root@mysql57 ~]# mv mysql-5.7.28-linux-glibc2.12-x86_64 /usr/local/mysql
[root@mysql57 ~]# cd /usr/local/mysql
[root@mysql57 mysql]# ls
bin docs include lib LICENSE man README share support-files
[root@mysql57 mysql]# mkdir /usr/local/mysql/{data,log} #创建数据库和日志目录
[root@mysql57 ~]# touch /usr/local/mysql/log/error.log
[root@mysql57 ~]# touch /usr/local/mysql/mysql.sock
[root@mysql57 ~]# touch /usr/local/mysql/mysql.pid
[root@mysql57 mysql]# chown -R mysql:mysql /usr/local/mysql/ #设置mysql目录访问权限,用户组
[root@mysql57 mysql]# rpm -qa | grep -E ‘mariadb|mysql' #检查操作系统是否自带mysql和mariadb
mariadb-libs-5.5.64-1.el7.x86_64
[root@mysql57 mysql]# rpm -e --nodeps mariadb-libs-5.5.64-1.el7.x86_64
[root@mysql57 mysql]# vim /etc/my.cnf
[mysql]
default-character-set = utf8mb4
socket = /usr/local/mysql/mysql.sock
[mysqld]
user = mysql
port = 3306
server_id = 3306
basedir = /usr/local/mysql
datadir = /usr/local/mysql/data
socket = /usr/local/mysql/mysql.sock
log_bin = /usr/local/mysql/log/mysql-bin
log_bin_index = /usr/local/mysql/log/binlog.index
expire-logs-days = 7
max-binlog-size = 1G
binlog_format = row
binlog_rows_query_log_events = 1
character-set-server = utf8mb4
default-storage-engine = InnoDB
innodb_buffer_pool_size = 1024M
#default_password_lifetime = 90
explicit_defaults_for_timestamp = 1
general_log = 0
general_log_file = /usr/local/mysql/log/general.err
slow_query_log = 1
slow_query_log_file = /usr/local/mysql/log/slow.log
long_query_time = 10
secure_file_priv=''
#default-authentication-plugin=mysql_native_password
[mysqld_safe]
log-error = /usr/local/mysql/log/error.log
pid-file = /usr/local/mysql/mysql.pid
[root@mysql57 bin]# ./mysqld --defaults-file=/etc/my.cnf --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data/ --user=mysql --initialize
注: --defaults-file:指定MySQL配置文件路径 --user: 指定账户 --basedir:指定MySQL安装的绝对路径 --datadir:指定MySQL数据存放的绝对路径 --initialize:初始化随机密码,初始化后会在命令提示符的最后给出一个临时密码,一定记住将此密码记录下来。
四,启动数据库
1.配置环境变量
[root@mysql57 bin]# echo "export PATH=$PATH:/usr/local/mysql/bin" >> /etc/profile
[root@mysql57 bin]# source /etc/profile #配置立即生效
2.启动MySQL数据库
方法(一)
[root@mysql57 ~]# mysqld_safe --defaults-file=/etc/my.cnf &
[root@mysql57 ~]# ps -ef | grep mysql #查看mysql进程,验证是否启动成功
[root@mysql57 ~]# lsof -i :3306 #查看监听端口
方法(二) 配置MySQL服务并设置开机启动
[root@mysql57 ~]# ln -sv /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
[root@mysql57 ~]# chkconfig --add mysqld
[root@mysql57 ~]# chkconfig --list | grep mysqld
[root@mysql57 ~]# service mysqld start (status|stop|restart)
[root@mysql57 ~]# echo "/etc/init.d/mysqld start" >> /etc/rc.local #配置开机启动
[root@mysql57 ~]# chmod +x /etc/rc.local #赋予执行权限
方法(三) 配置systemctl启动和停止
[root@mysql57 ~]# vim /usr/lib/systemd/system/mysql.service
[Unit]Description=MYSQL Server
Documentation=man:mysqld(8)
Documentation=http://dev.mysql.com/doc/refman/en/using-systemd.html
After=network.target
After=syslog.target
[Install]
WantedBy=multi-user.target
[Service]
user=mysql
Group=mysql
ExecStart=/usr/local/mysql/bin/mysqld --defaults-file=/etc/my.cnf
LimitNOFILE = 65536
LimitNPORC = 65536
[root@mysql57 ~]# chmod +x /usr/lib/systemd/system/mysql.service
[root@mysql57 ~]# systemctl daemon-reload
[root@mysql57 ~]# systemctl status mysqld
[root@mysql57 ~]# systemctl start mysqld
[root@mysql57 ~]# systemctl enable mysqld
[root@mysql57 ~]# systemctl is-enabled mysqld
[root@mysql57 ~]# systemctl stop mysqld
[root@mysql57 ~]# mysql -u root -p #使用初始化临时密码进入数据库之后,修改数据库root密码,设置永不过期。
mysql> alter user 'root'@'localhost' identified by 'root' password expire never;
mysql> create user 'root'@'%' identified by 'root' password expire never; #创建一个远程root管理用户
mysql> grant all privileges on *.* to 'root'@'%' identified by 'root' with grant option; #授权
mysql> flush privileges;
mysql> show grants for ‘root’@‘%’;
mysql> show databases;
mysql> select version();
mysql> select user,host,authentication_string from mysql.user;
mysql> show global variables like "basedir";
mysql> show global variables like "datadir";
mysql> show variables like "log_error";
五,关闭数据库
[root@mysql57 ~]# mysqladmin -uroot -proot shutdown -S /usr/local/mysql/mysql.sock #正常关闭方式,非正常关闭就需要kill掉MySQL的进程了。
报错信息:error: 'Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)'Check that mysqld is running and that the socket: '/tmp/mysql.sock' exists!
解决办法:[root@mysql57 ~]# ln -sv /usr/local/mysql/mysql.sock /tmp/mysql.sock
附:MySQL5.7.28二进制包官网下载链接:https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.28-linux-glibc2.12-x86_64.tar.gz
六,知识拓展
注: 配置文件/etc/my.cnf secure_file_priv 参数文件解析
mysql> show variables like "secure_file_priv";
secure_file_priv 为 NULL 时,表示限制mysqld不允许导入或导出。
secure_file_priv 为 /tmp 时,表示限制mysqld只能在/tmp目录中执行导入导出,其他目录不能执行。
secure_file_priv 没有值时,表示不限制mysqld在任意目录的导入导出