环境:centos7.3
mysql:5.7

  1. 数据库安装
# 下载 mysql5.7 源安装包
wget http://dev.mysql.com/get/mysql57-community-release-el7-8.noarch.rpm
# 安装 mysql 源
yum localinstall -y mysql57-community-release-el7-8.noarch.rpm
# 安装 mysql 服务
yum install mysql-community-server

  1. mysql 服务管理
# 启动 mysql 数据库
systemctl start mysqld.service
# 设置 mysql 的开机启动
systemctl enable mysqld.service
# 重启 mysql 服务
systemctl restart mysqld.service
# 查看 mysql 的服务状态
systemctl status mysqld.service
  1. 初始密码
    mysql 安装完成后是有初始密码的,不过这个密码是随机的,用户名是默认的 root,这个密码在 mysql 的日志文件中。
    centos7.3 安装完 mysql 后 log 文件的默认地址在:
/var/log/mysqld.log

mysql 的配置文件默认在:

/etc/my.cnf

可以从 mysql 的配置文件下查看到一些有用信息:

vim 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

可以从配置文件中找到数据文件的存放目录:datadir=/var/lib/mysql
日志文件地址:log-error=/var/log/mysqld.log
pid 文件:pid-file=/var/run/mysqld/mysqld.pid
前面一大堆的铺垫,就为了要说 mysql 的初始密码存放在了 log 文件中。

vim /var/log/mysqld.log
……
2019-01-28T09:56:33.184635Z 1 [Note] A temporary password is generated for root@localhost: T_h>H&giP5OA
……

找到大致内容是这样的一行代码,最后的关键词是:password is generated for root@localhost: T_h>H&giP5OA,这最后的内容就是密码了。结下来就是改了。

# 通过默认的用户名、密码登录数据库 
mysql -u root -p
# 输入密码
T_h>H&giP5OA #密码输入时不显示的,不要因为没有看见而以为没有输入成功
# 改密码
ALTER USER 'root'@'localhost' IDENTIFIED BY '新密码';

注意新密码的策略:最好是密码复杂度高一点,有大小写、数字和特殊字符的混编。好像从高版本开始,这是一个必须的策略,但没有查证具体是从哪个版本开始的。改好后就可以使用新密码登录和使用数据库了。