安装环境准备
操作系统: Oracle Linux Server 6.5
IP 地址: 192.168.6.176
版本信息: MySQL Community Server 5.7.18
下载MySQL数据库
访问MySQL网站
MySQL官网下载
根据操作系统类型和版本下载相应的安装包
注:此处下载的是RPM格式的安装包,通过RPM方式进行安装。
需要下载的安装包:
mysql-community-server-5.7.18-1.el6.x86_64.rpm
mysql-community-common-5.7.18-1.el6.x86_64.rpm
mysql-community-client-5.7.18-1.el6.x86_64.rpm
mysql-community-libs-5.7.18-1.el6.x86_64.rpm
下载安装包
安装MySQL数据库
在Linux操作系统下执行命令进行安装。
[root@DB-Server-Node1 ~]# rpm -ivh mysql-community-server-5.7.18-1.el6.x86_64.rpm mysql-community-common-5.7.18-1.el6.x86_64.rpm mysql-community-client-5.7.18-1.el6.x86_64.rpm mysql-community-libs-5.7.18-1.el6.x86_64.rpm
warning: mysql-community-client-5.7.18-1.el6.x86_64.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY
Preparing... ########################################### [100%]
1:mysql-community-common ########################################### [ 25%]
2:mysql-community-libs ########################################### [ 50%]
3:mysql-community-client ########################################### [ 75%]
4:mysql-community-server ########################################### [100%]
配置MySQL数据库
修改/etc/my.cnf配置文件, 指定数据存放位置
注: 修改MySQL数据库的默认数据存储路径, 需要关闭SELinux, 否则会出现服务启动不了,并出现Can't create directory的错误信息。
[root@DB-Server-Node1 ~]# vi /etc/my.cnf
[mysqld]
datadir=/data/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
创建数据目录,并修改目录所有者
[root@DB-Server-Node1 ~]# mkdir -p /data/mysql
[root@DB-Server-Node1 data]# cd /data
[root@DB-Server-Node1 data]# chown -R mysql.mysql mysql
[root@DB-Server-Node1 data]# ll
total 20
drwx------. 2 root root 16384 Jul 14 10:46 lost+found
drwxr-xr-x. 2 mysql mysql 4096 Jul 14 14:43 mysql
启动MySQL服务
[root@DB-Server-Node1 mysql]# service mysqld start
Initializing MySQL database: [ OK ]
Installing validate password plugin: [ OK ]
Starting mysqld: [ OK ]
注意: 在初次启动mysql服务时, 请先确保是否已经关闭selinux, 否则在服务启动时会出现Can’t create test file xxx-lower-test的错误信息, 可以通过临时关闭selinux命令setenforce 0或永久关闭selinux(修改/etc/sysconfig/selinux配置文件)来禁用selinux。
修改数据库默认密码
(1). 通过MySQL生成的临时密码来修改
MySQL 5.7在启动时会生成一个初始化密码,而在之前的版本首次登陆不需要登录。可以通过生成的默认密码登录数据库,然后再修改root的密码;
方法 1:
[root@DB-Server-Node1 mysql]# cat /root/.mysql_secret
# Password set for user 'root@localhost' at 2015-04-22 22:13:23
?G5W&tz1z.cN
注: 在Linux操作系统下有可能没有.mysql_secret文件, 可以采用以下的方法来查询MySQL的临时密码。
方法2:
[root@DB-Server-Node1 mysql]# grep 'temporary password' /var/log/mysqld.log
2015-04-23T03:04:55.113219Z 1 [Note] A temporary password is generated for root@localhost: kqiffRrpa2,:
(2). 通过MySQL安全模式来修改密码
另外还可以通过以下的步骤来修改root用户的密码。
先停止MySQL服务
```
[root@DB-Server-Node1 data]# service mysqld stop
Stopping mysqld: [ OK ]
```
使用安全模式进入MySQL
```
[root@DB-Server-Node1 data]# mysqld_safe --skip-grant-tables
2017-06-14T07:06:38.506327Z mysqld_safe Logging to '/var/log/mysqld.log'.
2017-06-14T07:06:38.548109Z mysqld_safe Starting mysqld daemon with databases from /data/mysql
```
此处,需要新打开一个SSH连接
```
[root@DB-Server-Node1 ~]# mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.18 MySQL Community Server (GPL)
Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>use mysql
Database Changed
mysql>update user set authentication_string=password("123456") where user='root' and host='localhost';
Query OK, 1 row affected, 1 warning (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 1
mysql>flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql>exit
```
修改完后, 退出安全模式, 并重启服务
测试MySQL数据库访问
[root@DB-Server-Node1 mysql]# mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.18
Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
登录成功, 修改用户密码成功。
注: MySQL 5.7版本在用户首次登录时提示用户修改密码, 需要通过set PASSWORD=PASSWORD("新密码");语句来设置新密码, 如果不修改则无法对数据库进行操作。
mysql> GRANT ALL PRIVILEGES ON *.* TO root@'%' IDENTIFIED BY 'Sczq@db123' WITH GRANT
OPTION;
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing
this statement.
mysql> set PASSWORD=PASSWORD("123456");
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> grant all privileges on *.* to root@'%' identified by '123456' with grant option;
Query OK, 0 rows affected, 1 warning (0.00 sec)