1、下载软件

下载地址

1
# wget http://mirrors.sohu.com/mysql/MySQL-5.7/mysql-5.7.17-linux-glibc2.5-x86_64.tar.gz

2、解压

1
tar -zxvf mysql-5.7.21-linux-glibc2.12-x86_64.tar.gz

3、复制解压后的mysql目录到指定位置

1
cp -r mysql-5.7.21-linux-glibc2.12-x86_64 /usr/local/mysql

4、添加用户和组

1
2
[root@sing mysql]# groupadd mysql 
[root@sing mysql]# useradd -r -g mysql mysql

5、修改mysql文件所属的用户和用户组

1
2
[root@sing local]# chown -R mysql .
[root@sing local]# chgrp -R mysql .

6、配置启动文件

到support-files目录下,复制my-default.cnf 到 /etc/my.cnf (mysqld启动时自动读取)

1
2
[root@sing local]# cd mysql/support-files/
[root@sing support-files]# cp my-default.cnf /etc/my.cnf

7、初始化数据库

bin目录下执行mysqld命令初始化:

1
2
3
4
[root@sing mysql]# bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data
...
...
2018-04-11T15:28:45.680829Z 1 [Note] A temporary password is generated for root@localhost: 1r_MSfsZe<6l

要注意:

  • 最后一行返回的1r_MSfsZe<6l为第一次登录的临时密码

8、给数据库加密

1
bin/mysql_ssl_rsa_setup  --datadir=/usr/local/mysql/data

9、启动mysql(为了不让进程卡主,可在启动mysql的命令后加上&代表此进程在后台运行)

1
[root@sing mysql]# mysqld_safe --user=mysql &

10、检查

1
2
3
4
[root@sing mysql]# ps -ef|grep mysql
root 31925 27705 0 00:21 pts/2 00:00:00 /bin/sh /usr/local/mysql//bin/mysqld_safe --user=mysql
mysql 32016 31925 0 00:21 pts/2 00:00:00 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=/usr/local/mysql/data/sing.err --pid-file=/usr/local/mysql/data/sing.pid
root 32046 27705 0 00:22 pts/2 00:00:00 grep --color=auto mysql

发现有以上进程便代表启动成功。

11、修改data目录的用户

1
[root@sing mysql]# chown -R mysql data

12、复制mysql.server 到/etc/init.d/ 目录下

去到mysql下的support-files下,执行命令:

1
[root@sing support-files]# cp mysql.server /etc/init.d/mysql.server

则下次启动mysql服务就不需要去到去到bin目录通过bin/mysqld_safe –user=mysql &启动了,而直接执行/etc/init.d/mysql.server start就可以开启进程

1
2
[root@sing /]# /etc/init.d/mysql.server start
Starting MySQL. [ 确定 ]

13、设置成开机自启动

1
2
3
4
[root@sing bin]# chkconfig --add mysql.server
[root@sing bin]# chkconfig --list |grep mysql

mysql.server 0:关 1:关 2:开 3:开 4:开 5:开 6:关

14、修改 /etc/init.d/mysql.server 参数

1
vi /etc/init.d/mysql.server

给与2个目录位置

1
2
basedir=/usr/local/mysql  
datadir=/usr/local/mysql/data

15、进入客户端

登录

1
2
3
4
5
6
7
8
9
10
11
12
13
[root@sing /]# mysql -uroot -p
Enter password: ### 输入之前的临时密码
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.17 MySQL Community Server (GPL)

Copyright (c) 2000, 2016, 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.

修改密码

1
set password=password('新密码');

16、设置远程访问

1
mysql>grant all privileges on *.* to 远程访问用户名@'%' identified by '用户密码';

此时就可以使用远程机器进行访问

17、配置环境变量

为了方便操作,配置环境变量还是有必要的

1
vi /etc/profile

添加:

1
export PATH=/usr/local/mysql/bin:$PATH     # 使全局生效