Centos7 安装 MySQL8 配置、授权、备份、开机启动、远程连接

001 系统环境

[root@localhost ~]# ll /etc/*centos*
-rw-r--r--. 1 root root 37 Nov 23  2020 /etc/centos-release
-rw-r--r--. 1 root root 51 Nov 23  2020 /etc/centos-release-upstream
[root@localhost ~]#
[root@localhost ~]# cat /etc/centos-release
CentOS Linux release 7.9.2009 (Core)
[root@localhost ~]#
[root@localhost ~]# uname -r
3.10.0-1160.42.2.el7.x86_64
[root@localhost ~]#
[root@localhost ~]# getconf LONG_BIT
64 

002 数据库下载

https://dev.mysql.com/downloads/mysql/
在这里插入图片描述

003 安装数据库

  • 解压并移动
# 解压包并移动
xz -d mysql-8.0.26-linux-glibc2.12-x86_64.tar.xz
tar xvf mysql-8.0.26-linux-glibc2.12-x86_64.tar
mv mysql-8.0.22-linux-glibc2.12-x86_64/ /usr/local/mysql
  • 创建软连接
ln -s /usr/local/mysql/bin/mysql /usr/bin/mysql
ln -s /usr/local/mysql/bin/mysqldump /usr/bin/mysqldump
  • 添加用户组和用户
groupadd mysql
useradd -r -g mysql mysql
  • 创建数据、日志、备份文件夹、修改权限
mkdir -p /data/mysql /logs/mysql /bak/mysql

chown -R mysql:mysql /usr/local/mysql/
chown -R mysql:mysql /data/mysql/
chown -R mysql:mysql /logs/mysql/
chmod -R 777 /usr/local/mysql/
chmod -R 777 /data/mysql/
chmod -R 777 /logs/mysql/

004 配置文件创建

  • 创建文件
vim /etc/my.cnf
  • 添加以下内容:
[mysqld]
basedir=/usr/local/mysql
datadir=/data/mysql
socket=/data/mysql/mysql.sock
log-error=/logs/mysql/error.log
port=3306
user=mysql
server-id=3306
symbolic-links=0
character-set-server=utf8mb4
collation-server=utf8mb4_general_ci
default-time-zone='+8:00'
 
max_connections=1000
max_connect_errors=1000
tmp_table_size=256M
max_heap_table_size=256M
 
log-bin=/data/mysql/mysql-bin
expire_logs_days=7
tmpdir=/tmp
 
# Disabling symbolic-links is recommended to prevent assorted security risks
# Settings user and group are ignored when systemd is used.
# If you need to run mysqld under a different user or group,
# customize your systemd unit file for mariadb according to the
# instructions in http://fedoraproject.org/wiki/Systemd
 
[mysqld_safe]
pid_file=/data/mysqld/mysqld.pid
log_error=/logs/mysql/error.log
 
#
# include all files from the config directory
#
[client]
socket=/data/mysql/mysql.sock
default-character-set=utf8mb4

005 初始化数据库

  • 初始化数据库
cd /usr/local/mysql/
./bin/mysqld --defaults-file=/etc/my.cnf --initialize
  • 启动数据库
./bin/mysqld --defaults-file=/etc/my.cnf &
  • 查看root临时密码
cat /logs/mysql/error.log | grep password
  • root登录
mysql -uroot -p
  • 修改root密码,并限制只能本地访问
ALTER USER 'root'@'localhost' IDENTIFIED BY '123456'; 

006 添加账号并授权

CREATE USER 'auser'@'%' IDENTIFIED BY '123456';
GRANT ALL PRIVILEGES ON *.* TO 'auser'@'%' WITH GRANT OPTION;
FLUSH PRIVILEGES;

# read only
CREATE USER 'ruser'@'%' IDENTIFIED BY '123456';
GRANT SELECT ON *.* TO 'ruser'@'%' WITH GRANT OPTION;
FLUSH PRIVILEGES;

# dump
CREATE USER 'duser'@'localhost' IDENTIFIED BY '123456';
GRANT SELECT, LOCK TABLES, PROCESS ON *.* TO 'duser'@'localhost' WITH GRANT OPTION;
FLUSH PRIVILEGES;

007 修改密码

ALTER USER 'auser'@'%' IDENTIFIED WITH MYSQL_NATIVE_PASSWORD BY '234567';
FLUSH PRIVILEGES;

008 自动备份

  • 创建备份脚本
vim /usr/bin/mysql_dump.sh
  • 内容如下:
#!/bin/bash
  
name=mysql_dump_`date '+%Y%m%d_%H%M'`.sql.gz
 
mysqldump -uroot -P3306 -p123456 -B dbname | gzip > /bak/db/$name
  • 脚本授权
chmod -R 755 /usr/bin/mysql_dump.sh
  • 添加到定时任务
crontab -e
 
20 1 * * * /usr/bin/mysql_dump.sh

009 添加服务并开机启动

cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysql
chkconfig --add mysql
chkconfig --level 2345 mysql on

010 远程访问

# 查看防火墙状态
firewall-cmd --state

# 开放端口
firewall-cmd --zone=public --add-port=3306/tcp --permanent

# 立即生效
firewall-cmd --reload

# 查看已开放端口
firewall-cmd --zone=public --list-ports
# 远程连接
mysql -hIP -u账号 -P端口 -p
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值