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

000 目录

  • 001 系统环境
  • 002 数据库下载
  • 003 安装数据库
  • 004 配置文件创建
  • 005 初始化数据库
  • 006 添加账号并授权
  • 007 修改密码
  • 008 自动备份
  • 009 添加服务并开机启动
  • 010 远程访问

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/
Centos7 安装 MySQL8 配置、授权、备份、开机启动、远程连接_第1张图片

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 /usr/local/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

你可能感兴趣的:(数据库,运维,mysql,数据库,linux)