centos7环境MySql安装手册与备份总结

业精于勤而荒于嬉 行成于思而毁于随!

MySQL截止今天(2020年10月14日)尚未闭源,MariaDB数据库管理系统是MySQL的一个分支。

MySQL分为官方收费版,和官方发布的开源社区版,除特殊需求外,一般使用社区版即可。

各版本比较官方说明:https://www.mysql.com/products/

 

 社区版下载:https://dev.mysql.com/downloads/mysql/

 

文章有部分内容参考:https://blog.csdn.net/wudinaniya/article/details/81094578 在此非常感谢原作者

进入安装

解压后会有如下的文件结构

 

 

注意:安装之前需要检查下 机器上是否已经装用Mysql的环境,因为Centos会默认自带MariaDB,里面的组件略有差异,需要先卸载

查看旧版本MySql

rpm -qa | grep mysql
或
yum list installed|grep mysql

如果有请依次卸载掉

使用 rpm 命令安装MySql组件

第一步先安装: rpm -ivh mysql-community-common-5.7.26-1.el7.x86_64.rpm  注:ivh中, i-install安装;v-verbose进度条;h-hash哈希校验

之后安装 lib,client,server

至此安装完成

******注******

1> 执行 yum remove mysql-libs   命令后,会自动删除掉 /etc/ 下的 my.cnf 文件
2> 对于安装mysql组件,只有安装了 mysql-community-server-5.7.22-1.el7.x86_64.rpm 组件,才会:
a). 在 /etc/下生成 my.cnf 文件 和 my.cnf.d 文件夹

登录并创建MySql密码

systemctl start mysqld.service    #启动mysql
systemctl status mysqld.service  #查看mysql状态
systemctl stop mysqld.service   #关闭mysql

由于MySQL5.7.4之前的版本中默认是没有密码的,登录后直接回车就可以进入数据库,进而进行设置密码等操作。其后版本对密码等安全相关操作进行了一些改变,在安装过程中,会在安装日志中生成一个临时密码

grep 'temporary password' /var/log/mysqld.log  #查看临时密码

 

 

 

#修改密码
alter user root@localhost identified by 'password111'
#创建用户
create user dbback identified by 'dbback123';

 在5.6后,mysql内置密码增强机制,低强度密码会报错:

Step1: 更改策略,设置 validate_password_policy=0;

mysql> set global validate_password_policy=0;   
mysql> set global validate_password_length=1;

不管设置  validate_password_length=1,还是2,3,4 ,‘有效密码长度’这个参数的实际值都是4。超过4后设置是多少实际就是多少。

继续重设密码OK;

授权其他机器远程

如果是8版本

create user 'root'@'%' identified by '123456';
grant all privileges on *.* to 'root'@'%' with grant option;

8以下版本 查看当前授予过的权限

use mysql;
select user,host from user;

mysql> show grants;
grant all privileges on *.* to root@'%' identified by 'lkg@123'; 
mysql> flush privileges; #刷新权限,使设置生效, OK

 

查看mysql的配置文件

/usr/bin/mysql --verbose --help | grep -A 1 'Default options'

 

 

#查看显示所有数据库
show databases;
#查看当前使用的数据库
select database();
#查看数据库使用端口
show variables  like 'port';
#查看数据库编码
show variables like 'character%';
#查看表
show tables;
#查看数据库当前连接数,并发数。
show status like 'Threads%';
#查看数据库的最大连接数
show variables like '%max_connections%';

#查看数据文件存放路径
show variables like '%datadir%';

#查看版本

select version();

查看Mysql数据库引擎

show engines
show variables like 'default_storage_engine'

 

 Windows 修改Mysql数据库路径 我使用的版本是 MySQL Server 5.7

找到默认的配置文件路径:C:\ProgramData\MySQL\MySQL Server 5.7

 

 修改 my.ini配置文件,把路径改成你期望的路径后 重启MySQL服务,然后把原有的Data文件复制到对应的位置即可

注意 如果报错了,请看下Data的父级目录MySQL Server 5.7 是否有Network Service权限

 

 

 

 

  

 

 

里面设置数据库的最大连接数
[mysqld]
max_connections = 1000

 

Backup and Recovery(源自官网)

官方地址:https://dev.mysql.com/doc/refman/5.7/en/backup-types.html

Physical (Raw) Versus Logical Backups

Physical backups consist of raw copies of the directories and files that store database contents. This type of backup is suitable for large, important databases that need to be recovered quickly when problems occur.

Logical backups save information represented as logical database structure (CREATE DATABASECREATE TABLE statements) and content (INSERT statements or delimited-text files). This type of backup is suitable for smaller amounts of data where you might edit the data values or table structure, or recreate the data on a different machine architecture.

Online Versus Offline Backups

Online backups take place while the MySQL server is running so that the database information can be obtained from the server. Offline backups take place while the server is stopped. This distinction can also be described as hot” versus cold” backups; a warm” backup is one where the server remains running but locked against modifying data while you access database files externally.

 

#Mysql备份 mysqlbackup命令

文章参考:http://blog.itpub.net/12679300/viewspace-2145129/

#备份
mysqlbackup --user=root --password=123456# --backup-dir=D:\DBBack\ --with-timestamp  backup 

#还原 其实是生成一个Data文件夹,然后你再手动修改my.ini文件替换Data目录
mysqlbackup --backup-dir=D:\DBBack\2021-06-22_19-37-00 apply-log#因为在备份期间数据库还在读写,把这期间的log进行应用,达到数据的一致性
#执行还原
mysqlbackup
--datadir="D:\ProgramData\MySQL\Data" --backup-dir=D:\DBBack\2021-06-10_19-50-10 copy-back

 如果忘记密码 重置的方法

重置密码 可以看我另一个博客 https://blog.csdn.net/tao20grow/article/details/132279318

 

Mysql字符集推荐

  utf8mb4 是 MySQL 在 5.5.3 版本之后增加的一个新的字符集,它是 utf8 的超集,可以存储更多的类型比如 表情,有条件还是推荐使用 utf8mb4 

posted @ 2020-10-14 08:51  港城大白鲨  阅读(114)  评论(0编辑  收藏  举报