哈特中尉's Blog

不会写代码的司机不是好厨师!

centos7安装mysql5.7

查看已安装的mysql

1
yum list installed | grep mysql

卸载已经安装

1
yum -y remove mysql-libs.x86_64

配置yum源

  • 下载mysql源安装包

    1
    wget https://dev.mysql.com/get/mysql57-community-release-el7-11.noarch.rpm
  • 安装mysql源

    1
    yum localinstall mysql57-community-release-el7-11.noarch.rpm
  • 检查mysql源是否安装成功

    1
    yum repolist enabled | grep "mysql.*-community.*"
  • 查看yum库上的mysql版本信息
    yum list | grep mysql yum -y list mysql*

安装MySQL

1
yum -y install mysql-community-server

或者

1
yum -y install mysql-server mysql mysql-devel

启动MySQL服务

1
systemctl start mysqld

查看MySQL的启动状态

1
systemctl status mysqld

开机启动

1
2
systemctl enable mysqld
systemctl daemon-reload

修改root默认密码

mysql安装完成之后,在/var/log/mysqld.log文件中给root生成了一个默认密码。
通过下面的方式找到root默认密码,然后登录mysql进行修改:

1
2
3
grep 'temporary password' /var/log/mysqld.log
mysql -uroot -p
ALTER USER 'root'@'localhost' IDENTIFIED BY 'root@2017!';

或者

1
set password for 'root'@'localhost'=password('root@2017!');

注意:mysql5.7默认安装了密码安全检查插件(validate_password),默认密码检查策略要求密码必须包含:大小写字母、数字和特殊符号,并且长度不能少于8位。否则会提示ERROR 1819 (HY000): Your password does not satisfy the current policy requirements错误.

忘记root密码解决办法

vim /etc/my.cnf
在[mysqld]的段中加上一句:skip-grant-tables
例如:

1
2
3
4
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
skip-grant-tables

重新启动mysqld
service mysqld restart
登录Mysql
直接输入mysql

依次执行下面的语句

1
2
3
4
5
6
7
8
9
use mysql ;

set global read_only=0; --关掉新主库的只读属性
flush privileges ;

set password for 'root'@'localhost'=password('root@2017!');

set global read_only=1; --(读写属相)
flush privileges;

1.还原配置文件
2.重启mysql服务

查看密码策略

1
show variables like '%password%';
  • validate_password_policy:密码策略,默认为MEDIUM策略
  • validate_password_dictionary_file:密码策略文件,策略为STRONG才需要
  • validate_password_length:密码最少长度
  • validate_password_mixed_case_count:大小写字符长度,至少1个
  • validate_password_number_count :数字至少1个
  • validate_password_special_char_count:特殊字符至少1个
    上述参数是默认策略MEDIUM的密码检查规则。

共有以下几种密码策略:
策略 检查规则

修改密码策略

/etc/my.cnf文件添加validate_password_policy配置,指定密码策略

选择0(LOW),1(MEDIUM),2(STRONG)其中一种,选择2需要提供密码字典文件

1
validate_password_policy=0

如果不需要密码策略,添加my.cnf文件中添加如下配置禁用即可:

1
validate_password = off

重新启动mysql服务使配置生效:

1
systemctl restart mysqld

添加远程登录用户

默认只允许root帐户在本地登录,如果要在其它机器上连接mysql,必须修改root允许远程连接,或者添加一个允许远程连接的帐户,为了安全起见,我添加一个新的帐户:

1
GRANT ALL PRIVILEGES ON *.* TO 'yangxin'@'%' IDENTIFIED BY 'Yangxin0917!' WITH GRANT OPTION;

配置默认编码为utf8

修改/etc/my.cnf配置文件,在[mysqld]下添加编码配置,如下所示:

1
2
3
[mysqld]
character_set_server=utf8
init_connect='SET NAMES utf8'

默认配置文件路径

  • 配置文件:/etc/my.cnf
  • 日志文件:/var/log//var/log/mysqld.log
  • 服务启动脚本:/usr/lib/systemd/system/mysqld.service
  • socket文件:/var/run/mysqld/mysqld.pid

查看my.cnf位置

查看mysqld命令位置

1
which mysqld

查看默认的配置文件加载顺序

1
/usr/sbin/mysqld --verbose --help | grep -A 1 'Default options'

显示结果:

1
2
Default options are read from the following files in the given order:
/etc/my.cnf /etc/mysql/my.cnf /usr/etc/my.cnf ~/.my.cnf

查看mysql默认读取my.cnf目录

1
mysql --help | grep 'my.cnf'

结果:

1
2
order of preference, my.cnf, $MYSQL_TCP_PORT,
/etc/my.cnf /etc/mysql/my.cnf /usr/etc/my.cnf ~/.my.cnf

查看mysql全局变量

未登录mysql时候,shell命令:

1
mysqladmin variables -p

登录mysql的时候,mysql命令:

1
show global variables;

mysql备份

备份命令
参照:http://www.cnblogs.com/xcxc/archive/2013/01/30/2882840.html

1
2
mysqldump -h127.0.0.1 -uroot -proot data11 > backupfile.sql
// ip地址 账号root 密码root 数据库data111 生成的sql文件:backupfile.sql

备份成功,但是出现如下警告,原因是命令行写明文密码不安全。

1
mysqldump: [Warning] Using a password on the command line interface can be insecure.

解决办法:

1
vi /etc/mysql/my.cnf

增加mysqldump的用户和密码,则无需在命令行指定

1
2
3
[mysqldump]
user=dumper
password=dumper@2017!

创建专门的备份用户(可选)

1
2
3
4
5
create user dumper@'127.0.0.1' identified by 'dumper@2017!';
grant select on tempdb.* to dumper@'127.0.0.1';
grant show view on tempdb.* to dumper@'127.0.0.1';
grant lock tables on tempdb.* to dumper@'127.0.0.1';
grant trigger on tempdb.* to dumper@'127.0.0.1';

mysql备份文件还原

1
2
mysql -uroot -proot data11 < backupfile.sql
// 账号root 密码root 数据库data111 还原的sql文件:backupfile.sql

修改默认3306端口

  • vi /etc/my.cnf
  • 配置如下
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    [client]
    port=3417
    [mysqld]
    port=3417
    datadir=/var/lib/mysql
    socket=/var/lib/mysql/mysql.sock
    symbolic-links=0
    log-error=/var/log/mysqld.log
    pid-file=/var/run/mysqld/mysqld.pid
    validate_password=off
    bind-address=127.0.0.1
  • 重启mysql
    systemctl restart mysqld
  • 重启失败
    关闭SELinux:setenforce 0 ,再次重启。

实用sql语句

1
2
/***生成2-20之间随机整数**/
select FLOOR(2 + (RAND() * 20));

http://mirrors.163.com/mysql/Downloads/MySQL-5.7/mysql-5.7.38-1.el7.x86_64.rpm-bundle.tar

https://mirrors.tuna.tsinghua.edu.cn/mysql/downloads/MySQL-5.7/mysql-5.7.38-1.el7.x86_64.rpm-bundle.tar