Centos7.6安装MySQL5.7.32解压版
2020-12-10 本文已影响0人
Rick_Jen
下载MySQL linux版
安装MySQL
解压到/usr/local目录下
tar -zxvf mysql-5.7.32-linux-glibc2.12-x86_64.tar.gz -C /usr/local/
重命名mysql
cd /usr/local
mv mysql-5.7.32-linux-glibc2.12-x86_64/ mysql
安装依赖库
cd mysql
yum install -y libaio
yum -y install numactl
创建mysql用户与用户组
cd mysql
mkdir data
groupadd mysql
useradd -r -s /sbin/nologin -g mysql mysql -d /usr/local/mysql/
创建mysql相关目录与授权(需存在mysql用户与用户组)
mkdir /usr/local/mysql/data /usr/local/mysql/log
mkdir -p /var/lib/mysql/
mkdir -p /var/run/mysqld/
touch /usr/local/mysql/log/mysqld.log
chmod 666 /usr/local/mysql/log/mysqld.log
chown -R mysql:mysql /usr/local/mysql
chown -R mysql:mysql /var/run/mysqld/
chown -R mysql:mysql /var/lib/mysql/
初始化命令
cd /usr/local/mysql/bin
./mysqld --user=mysql --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data/
初始化中如果碰见这个问题
/pic:mw://bb3282da93ee203a683e8e0447f899c7
删除已经初始化的数据,重新初始化
rm -rf /usr/local/mysql/data
./mysql_install_db --user=mysql --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data/
./mysqld --user=mysql --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data/
查看安装成功后的数据库root密码
cd /root/
cat .mysql_secret
输出内容
# Password set for user 'root@localhost' at 2020-10-30 16:59:57
gKcu4Awy1ZYa 这个是密码
杀死当前mysql进程
netstat -apn | grep 3306
输出
tcp6 0 0 :::3306 :::* LISTEN 17927/./mysqld
kill -9 17927
修改基本配置
vim /usr/local/mysql/support-files/mysql.server
将basedir与datadir的值按照如下修改后保存并退出
basedir=/usr/local/mysql
datadir=/usr/local/mysql/data
mysqld_pid_file_path=/var/run/mysqld/mysqld.pid
为脚本创建软链接
ln -s /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
ln -s /usr/local/mysql/bin/mysql /usr/bin/mysql
添加基本my.cnf配置
cd /etc/
vim /etc/my.cnf
my.cnf文件
[mysqld]
basedir=/usr/local/mysql
datadir=/usr/local/mysql/data
socket=/tmp/mysql.sock
user=mysql
port=33068
character-set-server = utf8mb4
collation-server = utf8mb4_unicode_ci
init_connect='SET NAMES utf8mb4'
symbolic-links=0
max_connections=200
default-storage-engine=INNODB
lower_case_table_names=1
max_allowed_packet=32M
explicit_defaults_for_timestamp=true
[mysqld_safe]
log-error=/usr/local/mysql/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
修改mysqld执行权限
chmod 755 /etc/rc.d/init.d/mysqld
设置mysql开机启动
chkconfig mysqld on
启动MySQL
service mysqld start
service mysqld stop
service mysqld restart
修改密码 参考文章链接
centos8需要安装
yum install libncurses*
mysql -u root -p
输入刚刚的密码gKcu4Awy1ZYa
alter user 'root'@'localhost' identified by '你的密码';
密码设置的难一点,不然会报太简单了。
修改密码另一种方式
set password for 'root'=password('p@s5w0rd');
还有一种 建议使用这种
update user set authentication_string = password("p@s5w0rd") where user="root";
允许root账户IP登录
use mysql;
update user set host = '%' where user = 'root';
FLUSH PRIVILEGES;
允许远程访问
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'root' WITH GRANT OPTION;
FLUSH PRIVILEGES;
退出MySQL
exit;
备份
备份前准备
# 进入mysql安装目录
cd /usr/local/mysql
# 进入bin下会发现有一个mysqldump 这个就是数据库备份用到的
cd bin/
ls
# 执行手动备份
# 新建备份目录
cd /usr/local
mkdir mysql-backups
cd mysql-backups
# 执行手动备份命令
/usr/local/mysql/bin/mysqldump -u root -p 数据库名 > /usr/local/mysql-backups/数据库名.sql
使用shell脚本备份
新建shell脚本-备份所有的数据库
#!/bin/bash
#备份目录
BACKUP_FILEDIR=/usr/local/mysql-backups/
#当前时间
DATE=$(date +%Y%m%d)
####备份####
mysqldump --defaults-extra-file=/etc/my.cnf --all-databases | gzip > $BACKUP_FILEDIR/$DATE.sql.gz
echo $DATE"备份完成"
新建shell脚本-删除7天前的备份文件
#!/bin/bash
#备份目录
BACKUP_FILEDIR=/usr/local/mysql-backups
find $BACKUP_FILEDIR -mtime +7 -name "*.gz" -exec rm -rf {} \;
echo "执行清理七天前备份文件完成"
手动备份
# 进入数据库备份目录
cd /usr/local/mysql-backups
# 创建shell脚本
vim mysql.sh
# 将上面shell脚本内容CV到服务器上,注意填上数据库名字
# Esc 保存退出
:wq
# 尝试执行shell脚本
sh /usr/local/mysql-backups/mysql.sh
# 执行报错了 原因是因为数据库有密码了
mysqldump: Got error: 1045: Access denied for user 'root'@'localhost' (using password: NO) when trying to connect
# 那就解决一下
# 编辑my.cnf文件
vim /etc/my.cnf
# 填写并修改 mysqldump下的参数是给导出的命令使用的
[mysqldump]
max_allowed_packet = 400M
host=localhost
user=root
password='password'
# 再次执行脚本,在/usr/local/mysql-backups下就会出现一个压缩文件
# 删除7天前的备份
vim mysql_clean.sh
# 填写并修改 上面的清理脚本
自动备份
定时任务配置
#添加定时任务
crontab -e
#加入
#每天1点执行备份操作
00 1 * * * sh /usr/local/mysql-backups/mysql.sh
#每天2点执行清理7天前备份操作
00 2 * * * sh /usr/local/mysql-backups/mysql_clean.sh