1、卸载mariadb
查找
[alex@hadoop102 mysqlrpm5.7.26]$ rpm -qa | grep -i mariadb
mariadb-libs-5.5.56-2.el7.x86_64
卸载
[alex@hadoop102 mysqlrpm5.7.26]$ sudo rpm -e --nodeps mariadb-libs-5.5.56-2.el7.x86_64
PS:centos7.6自带的类mysql数据库是mariadb,会跟mysql冲突,要先删除
2、上传
[alex@hadoop102 software]$ wget \
http://dev.mysql.com/get/mysql-5.7.26-1.el7.x86_64.rpm-bundle.tar
[alex@hadoop102 software]$ ls -l
-rw-r--r-- 1 root root 530882560 Mar 4 11:07 mysql-5.7.26-1.el7.x86_64.rpm-bundle.tar
3、修改文件权限
[alex@hadoop102 software]$ sudo chown alex:alex mysql-5.7.26-1.el7.x86_64.rpm-bundle.tar
[alex@hadoop102 software]$ ls -l
-rw-r--r-- 1 alex alex 530882560 Mar 4 11:07 mysql-5.7.26-1.el7.x86_64.rpm-bundle.tar
4、解压
[alex@hadoop102 software]$ mkdir mysqlrpm5.7.26
[alex@hadoop102 software]$ tar -xvf mysql-5.7.26-1.el7.x86_64.rpm-bundle.tar -C mysqlrpm5.7.26/
5、赋予权限
[alex@hadoop102 software]$ cd mysqlrpm5.7.26/
[alex@hadoop102 mysqlrpm5.7.26]$ chmod 755 ./*
6、删除多余文件rpm包
[alex@hadoop102 mysqlrpm5.7.26]$ rm -rf \
mysql-community-test-5.7.26-1.el7.x86_64.rpm
[alex@hadoop102 mysqlrpm5.7.26]$ rm -rf mysql-community-embedded-*
[alex@hadoop102 mysqlrpm5.7.26]$ rm -rf \
mysql-community-devel-5.7.26-1.el7.x86_64.rpm
7、安装mysql
1)方式一
一条命令安装多个rpm包
[alex@hadoop102 mysqlrpm5.7.26]$ sudo rpm -Uvh --nodeps --force *.rpm
warning: mysql-community-client-5.7.26-1.el7.x86_64.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY
Preparing... ################################# [100%]
Updating / installing...
1:mysql-community-common-5.7.26-1.e################################# [ 20%]
2:mysql-community-libs-5.7.26-1.el7################################# [ 40%]
3:mysql-community-client-5.7.26-1.e################################# [ 60%]
4:mysql-community-server-5.7.26-1.e################################# [ 80%]
5:mysql-community-libs-compat-5.7.2################################# [100%]
2)方式二
[alex@hadoop102 mysqlrpm5.7.26]$ rpm -ivh mysql-community-common-5.7.26-1.el7.x86_64.rpm
[alex@hadoop102 mysqlrpm5.7.26]$ rpm -ivh mysql-community-libs-5.7.26-1.el7.x86_64.rpm
[alex@hadoop102 mysqlrpm5.7.26]$ rpm -ivh mysql-community-client-5.7.26-1.el7.x86_64.rpm
[alex@hadoop102 mysqlrpm5.7.26]$ rpm -ivh mysql-community-server-5.7.26-1.el7.x86_64.rpm
[alex@hadoop102 mysqlrpm5.7.26]$ rpm -ivh mysql-community-libs-compat-5.7.26-1.el7.x86_64.rpm
8、启动mysql
[alex@hadoop102 ~]$ sudo systemctl start mysqld.service
9、查看mysql默认密码
[alex@hadoop102 ~]$ grep "password" /var/log/mysqld.log
2022-03-04T03:26:44.148737Z 1 [Note] A temporary password is generated for root@localhost: watu1Hi>n>ky
10、登陆
[alex@hadoop102 ~]$ mysql -u'root' -p'watu1Hi>n>ky'
mysql>
11、设置mysql密码
1)配置密码安全策略
方式一
mysql> set global validate_password_policy=0;
方式二 需要重启mysql服务
[alex@hadoop102 ~]$ sudo vim /etc/my.cnf
global validate_password_policy=0
validate_password_policy=0
validate_password_length=1
port=3306
2)重置密码
mysql> set password=password('QWE!000000qwe!');
3)刷新
mysql> flush privileges;
12、配置mysql允许远程访问
1)查看user表结构
mysql> show databases;
mysql> use mysql;
mysql> show tables;
mysql> desc user;
2)查看user表内容
mysql> select user,host,authentication_string from user;
3)修改表内容
mysql> update user set host='%' where user='root' and host='localhost';
4)刷新
mysql> flush privileges;
5)关闭防火墙
[alex@hadoop102 ~]$ sudo systemctl stop firewalld.service
[alex@hadoop102 ~]$ sudo systemctl disable firewalld.service
13、mysql 数据定时备份
1)mysql_db_backup.sh
[alex@hadoop102 ~]$ vim mysql_db_backup.sh
#!/bin/bash
#备份目录
BACKUP=/opt/module/databack/mysql/db
#当前时间
DATETIME=$(date +%Y-%m-%d_%H%M%S)
echo $DATETIME
# 数据库地址
HOST=localhost
# 数据库用户名
DB_USER="root"
# 数据库密码
DB_PW='QWE!000000qwe!'
#备份的数据库名称
DATABASE='flink'
#创建备份目录,如果不存在,就创建
[ ! -d "${BACKUP}/${DATETIME}" ] && mkdir -p "${BACKUP}/${DATETIME}"
#备份数据库
mysqldump -u${DB_USER} -p${DB_PW} --host=${HOST} -q -R --databases $DATABASE | gzip > ${BACKUP}/${DATETIME}/${DATETIME}.sql.gz
#将文件处理成 tar.gz
cd $BACKUP
tar -czvf ${DATETIME}.tar.gz $DATETIME
#删除对应的备份目录
rm -rf ${BACKUP}/${DATETIME}
# 删除 10 天前的备份文件
find $BACKUP -atime +10 -name "*.tar.gz" -exec rm -rf {} \;
echo "备份数据库${DATABASE} 成功~"
2)crontab
[alex@hadoop102 ~]$ sudo crontab -e
30 2 * * * /usr/local/bin/mysql_db_backup.sh
14、mysql 忘记密码
1)修改 my.cnf 配置文件
[alex@hadoop102 ~]$ sudo vim /etc/my.cnf
skip-grant-tables
2)重启服务
[alex@hadoop102 ~]$ sudo systemctl restart mysqld.service
3)免密登陆
[alex@hadoop102 ~]$ mysql -u root -p