CentOS7通过rpm安装MySQL5.7.30

下载MySQL的rpm安装包

CentOS7通过rpm安装MySQL5.7.30_第1张图片
CentOS7通过rpm安装MySQL5.7.30_第2张图片
CentOS7通过rpm安装MySQL5.7.30_第3张图片

安装wget

yum install –y wget

下载mysql-5.7.30-1.el7.x86_64.rpm-bundle.tar

wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.30-1.el7.x86_64.rpm-bundle.tar

若下载失败(下载成功此步跳过),可安装rz从本地上传mysql-5.7.30-1.el7.x86_64.rpm-bundle.tar

yum -y install lrzsz

安装依赖

#先检查有没有安装过mysql或mariadb 
rpm -qa |grep mysql rpm -qa |grep mariadb
#我有个mariadb-libs-5.5.64-1.el7.x86_64,把它给卸载了 
rpm -e --nodeps mariadb-libs-5.5.64-1.el7.x86_64
#安装libaio、perl、net-tools库,要不就会报错 balabala... 
yum -y install libaio perl net-tools

安装mysql

#建个目录存放解压文件

mkdir mysql-5.7.30

#解压缩

tar -xvf mysql-5.7.30-1.el7.x86_64.rpm-bundle.tar -C mysql-5.7.30

#进入目录准备安装

cd mysql-5.7.30

#开始安装,-ivh 其中i表示安装,v表示显示安装过程,h表示显示进度

rpm -ivh mysql-community-common-5.7.30-1.el7.x86_64.rpm
rpm -ivh mysql-community-libs-5.7.30-1.el7.x86_64.rpm
rpm -ivh mysql-community-client-5.7.30-1.el7.x86_64.rpm
rpm -ivh mysql-community-server-5.7.30-1.el7.x86_64.rpm

注:强制安装–force --nodeps,增加两个参数

启动mysql

启动mysqld服务

systemctl start mysqld

下面列出其余service命令(不用运行)

#查看mysqld服务状态

service  mysqld status

#停止mysqld服务

service mysqld stop

#重新启动mysqld服务

service mysqld restart

#将mysql添加到开机启动列表

chkconfig --add mysqld

#设置开机启动项

chkconfig mysqld on

#配置mysqld开机不自动启动

chkconfig mysqld off

登录mysql修改密码

#查询生成的临时密码

grep "password" /var/log/mysqld.log

#登录mysql

mysql -uroot -p

#Enter password: (输入查询到的临时密码)
#因为是本地测试环境,所以想把密码改的简单点
#查询密码校验配置的系统变量

SHOW VARIABLES LIKE 'validate_password%';

#设置密码校验策略(0 or LOW),要不密码太LOW不让你过

set global validate_password_policy=0;

#设置密码校验长度,要不密码太短不让你过

set global validate_password_length=4;

#这密码有够LOW了,但我就是想要

set password = password("1234");

#可以退出,试试用新密码重新登录

quit

配置my.cnf

#打开配置文件

vi /etc/my.cnf
#For advice on how to change settings please see
#http://dev.mysql.com/doc/refman/5.7/en/server-configuration-defaults.html

[mysqld]
#
#Remove leading # and set to the amount of RAM for the most important data
#cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
#innodb_buffer_pool_size = 128M
#
#Remove leading # to turn on a very important data integrity option: logging
#changes to the binary log between backups.
#log_bin
#
#Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock

# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0

log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid

# 因为是本地测试环境,所以库能用就好了,我就不详细说明了(下篇在详细说明),大家可以参照下方官方建议去配置
# http://dev.mysql.com/doc/refman/5.7/en/server-configuration-defaults.html
# 另外列举下vi编辑器命令
# i(按i键进入INSERT模式,可开始编辑)
# Esc(退出编辑模式)
# :wq(保存退出)
# :q(退出)
# :q!(不保存强制退出)

# 重启服务生效(另需注意:配置内路径需要自行创建目录及赋予权限)
service mysqld restart

配置远程连接

# 用新密码登进去试试
mysql -uroot -p
# 授权 所有权限(all),所有数据库(*.*) 给 用户名(root),任何主机(%),密码(yourpassword)
grant all privileges on *.* to 'root'@'%' identified by 'yourpassword' with grant option;
# 刷新权限使其立即生效
flush privileges;
# 退出MySQL
quit

# 关闭防火墙
service iptables restart

mysql数据库备份与还原

备份MySQL数据库的命令

mysqldump -hhostname -uusername -ppassword databasename > backupfile.sql

(例:mysqldump -h 122.51.176.153 -uwang -p1992S@ sw_account --default-character-set=gbk --opt -Q -R --skip-lock-tables>testbackup.sql)

备份MySQL数据库为带删除表的格式,能够让该备份覆盖已有数据库而不需要手动删除原有数据库。

mysqldump -–add-drop-table -uusername -ppassword databasename > backupfile.sql

直接将MySQL数据库压缩备份

mysqldump -hhostname -uusername -ppassword databasename | gzip > backupfile.sql.gz

备份MySQL数据库某个(些)表

mysqldump -hhostname -uusername -ppassword databasename specific_table1 specific_table2 > backupfile.sql

同时备份多个MySQL数据库

mysqldump -hhostname -uusername -ppassword –databases databasename1 databasename2 databasename3 > multibackupfile.sql

仅备份数据库结构

mysqldump –no-data –databases databasename1 databasename2 databasename3 > structurebackupfile.sql

备份服务器上所有数据库

mysqldump –all-databases > allbackupfile.sql

还原MySql数据库的命令

数据库还原有三种方式:source命令、mysql、gunzip命令

source 命令

进入mysql数据库控制台,

mysql -u root -p
mysql>use 数据库

然后使用source命令,后面参数为脚本文件(如这里用到的.sql)

mysql>source /home/work/db/bkdb.sql

mysql

mysql -hhostname -uusername -ppassword databasename < backupfile.sql

gunzip

gunzip < backupfile.sql.gz | mysql -uusername -ppassword databasename

你可能感兴趣的:(linux,mysql,mysql,linux)