---
title: "Linux环境安装MySQL Server 5.7.21"
author: "Everyday都不同"
date: "2018-03-29T14:33:13Z"
tags: []
categories: ["行业应用"]
slug: "linux-mysql-server-5-7-21"
draft: false
lastmod: "2018-03-29T19:53:01Z"
id: 2414899
url: "https://raising.iteye.com/blog/2414899"
---

资料参考：一般是网络查询，但需要注意在MySQL官网上下载的版本，google的时候最好精确到版本，因为不同版本的安装方式可能有所不同，而这些“不同”就会造成一些“坑”！本文的版本是最新版本：mysql-5.7.21-linux-glibc2.12-x86\_64.tar.gz。故在网络上google的时候查找5.7.21版本的安装资料，不要随便一篇文章就拿来参考！

 

 先搭建一个虚拟机再玩：https://www.linuxidc.com/Linux/2017-04/143102.htm（虚拟机镜像最好使用Desktop版会比较少点坑。。）

 

安装过程大同小异，只不过Linux环境的差异会使你遇到各种意想不到的问题，下面仅针对我此次的安装过程来说明（具体问题具体解决）。

 

**1.  建一个存放上传mysql安装tar包的目录**

 

cd /home

 

root@ubuntu:/home# mkdir security

 

chmod 777 /home/security/

 

 

 

**2.  使用WinSCP工具上传mysql的安装tar包到/home/security目录**

 

 

**3.  解压该tar包，并将解压后的目录重命名为mysql**

 

tar -zxvf mysql-5.7.21-linux-glibc2.12-x86\_64.tar.gz

 

mv mysql-5.7.21-linux-glibc2.12-x86\_64 mysql

 

移动到/usr/local目录下：

 

mv mysql /usr/local

 

 

 

**4.  检查Linux系统之前是否装过mysql，若装过，需要改端口号或者将之前的服务停掉，相关目录删掉**

 

ps -ef|grep mysql

 

 

 

**5.  创建用户组mysql，创建用户mysql并将其添加到用户组mysql中，并赋予读写权限**

 

groupadd mysql

 

useradd -r -g mysql mysql

 

chown -R mysql mysql/

 

chgrp -R mysql mysql/

 

 

 

**6.  先检查有没有安装依赖的包libaio**

 

rpm –qa | grep libaio1

 

如果没有需要通过 apt-get install libaio1 libaio-dev来安装

 

注：这一步可能rpm命令也会没有，通过下面的步骤来进行(会耗费较长时间)：

 

 apt-get update

 

apt-get upgrade

 

apt-get install &lt;packagename&gt;

 

（ap-get是ubuntu下的一个软件安装方式，它是基于debain

 

 

 

yum是redhat、centos下的一个软件安装方式，它是基于Linux的，最好先搞清楚自己的Linux系统版本: cat /proc/version）

 

 

 

**7.  创建配置文件，保存并退出。**

 

vim /etc/my.cnf

 

\[client]

 

port = 3306

 

socket = /tmp/mysql.sock

 

 

 

\[mysqld]

 

port = 3306

 

character\_set\_server=utf8

 

init\_connect='SET NAMES utf8'

 

basedir=/usr/local/mysql

 

datadir=/usr/local/mysql/data

 

socket=/tmp/mysql.sock

 

log-error=/var/log/mysqld.log

 

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

 

#不区分大小写

 

lower\_case\_table\_names = 1

 

 

 

sql\_mode=STRICT\_TRANS\_TABLES,NO\_ZERO\_IN\_DATE,NO\_ZERO\_DATE,ERROR\_FOR\_DIVISION\_BY\_ZERO,NO\_AUTO\_CREATE\_USER,NO\_ENGINE\_SUBSTITUTION

 

 

 

max\_connections=5000

 

skip-grant-tables

 

default-time\_zone = '+8:00'

 

注：3306是默认端口，若之前有安装过mysql服务，则client和mysqld的port都需要改成其他端口如3307，否则初始化的时候会失败。

 

 

 

**8.  初始化数据库（比较关键的一步）**

 

#手动编辑一下日志文件（用来记录初始密码以及启动日志的），什么也不用写，直接保存退出

 

cd /var/log/

 

 

 

vim mysqld.log

 

：wq

 

 

 

chmod 777 mysqld.log

 

chown mysql:mysql mysqld.log

 

 

 

初始化命令：/usr/local/mysql/bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --lc\_messages\_dir=/usr/local/mysql/share --lc\_messages=en\_US

 

 

 

**9.  查看初始密码**

 

cat /var/log/mysqld.log |grep 'root@localhost'

 

如：2018-03-28T12:50:49.180634Z 1 \[Note] A temporary password is generated for root@localhost: xj7fWyj(NU!u

 

 

 

**10.启动mysql服务**

 

cd /var/run/

 

 

 

mkdir mysqld

 

 

 

chmod 777 mysqld

 

 

 

cd mysqld

 

 

 

vim mysqld.pid

 

 

 

chmod 777 mysqld.pid

 

 

 

chown mysql:mysql mysqld.pid

 

 

 

/usr/local/mysql/support-files/mysql.server start

 

 

 

/usr/local/mysql/bin/mysql -uroot –p’ xj7fWyj(NU!u’（换成第9步的初始密码）

 

**注：-p跟密码之间没有空格，这是个坑！网上好多都有空格.**

 

可以看到：

 

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

 

Welcome to the MySQL monitor.  Commands end with ; or \\g.

 

Your MySQL connection id is 2

 

Server version: 5.7.21 MySQL Community Server (GPL)

 

 

 

Copyright (c) 2000, 2018, 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&gt;

 

 

 

**11.修改root用户初始密码**

 

由于上面的密码太复杂，修改自己定义的密码，注意5.7.21版本的sql语句是：

 

mysql&gt; use mysql;

 

Reading table information for completion of table and column names

 

You can turn off this feature to get a quicker startup with -A

 

 

 

Database changed

 

mysql&gt; update user set authentication\_string=PASSWORD('123456') where User='root';

 

Query OK, 1 row affected, 1 warning (0.05 sec)

 

Rows matched: 1  Changed: 1  Warnings: 1

 

 

 

**12.设置开机自启动**

 

cd /usr/local/mysql/support-files

 

 

 

cp mysql.server /etc/init.d/mysqld

 

 

 

chkconfig --add mysqld

 

注：这里chkconfig命令可能又没有。。这样做：

 

apt-get install sysv-rc-conf

 

cp /usr/sbin/sysv-rc-conf /usr/sbin/chkconfig

 

chkconfig mysqld on

 

之后检查下：

 

chkconfig –list可发现mysql的服务：

 

mysqld       2:on       3:on    4:on    5:on

 

说明成功了。

 

 

 

**13.检查mysql服务是否安装成功：**

 

ps -ef|grep mysql

 

发现：

 

root      29429      1  0 05:55 pts/17   00:00:00 /bin/sh /usr/local/mysql/bin/mysqld\_safe --datadir=/usr/local/mysql/data --pid-file=/var/run/mysqld/mysqld.pid

 

mysql     29707  29429  0 05:55 pts/17   00:00:01 /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=/var/log/mysqld.log --pid-file=/var/run/mysqld/mysqld.pid --socket=/tmp/mysql.sock --port=3306

 

root      29776  13540  0 05:56 pts/17   00:00:00 /usr/local/mysql/bin/mysql -uroot -px xxxxxxxxxx

 

root      30907  28335  0 06:10 pts/19   00:00:00 grep --color=auto mysql

 

以及3306端口是否开启成功，被MySQL占用：

 

lsof -i:3306

 

结果：

 

COMMAND   PID  USER   FD   TYPE DEVICE SIZE/OFF NODE NAME

 

mysqld  29707 mysql   19u  IPv6 293124      0t0  TCP \*:mysql (LISTEN)

 

 

 

接下来就可以用navicat连接工具试试了！（目前只有root用户，创建其他账号的过程略）