欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

在Linux 系统 centos 6上安装mysql-5.7.28 步骤

程序员文章站 2022-06-05 21:07:47
...

新购买了一个云服务器,上面没有安装mysql服务,今天从0开始安装mysql服务。

由于中间遇到不顺利的情节,所以把他记录下来,便于以后参考。

一、要下载的与安装的文件:

1)mysql-community-libs-5.7.28-1.el6.x86_64.rpm
https://cdn.mysql.com//Downloads/MySQL-5.7/mysql-community-libs-5.7.28-1.el6.x86_64.rpm

2)mysql-community-common-5.7.28-1.el6.x86_64.rpm
https://cdn.mysql.com//Downloads/MySQL-5.7/mysql-community-common-5.7.28-1.el6.x86_64.rpm

3)mysql-community-client-5.7.28-1.el6.x86_64.rpm
https://cdn.mysql.com//Downloads/MySQL-5.7/mysql-community-client-5.7.28-1.el6.x86_64.rpm

4)mysql-community-server-5.7.28-1.el6.x86_64.rpm
https://cdn.mysql.com//Downloads/MySQL-5.7/mysql-community-server-5.7.28-1.el6.x86_64.rpm

以上这些文件我已经添加了URL,不必访问Oracle网站,直接点击就可下载。

进入linux系统,执行如下命令进行下载:

cd /
mkdir mysql_install
cd mysql_install
wget https://cdn.mysql.com//Downloads/MySQL-5.7/mysql-community-client-5.7.28-1.el6.x86_64.rpm
wget https://cdn.mysql.com//Downloads/MySQL-5.7/mysql-community-libs-5.7.28-1.el6.x86_64.rpm
wget https://cdn.mysql.com//Downloads/MySQL-5.7/mysql-community-client-5.7.28-1.el6.x86_64.rpm
wget https://cdn.mysql.com//Downloads/MySQL-5.7/mysql-community-server-5.7.28-1.el6.x86_64.rpm

注意需要按顺序安装以上RPM包,安装顺序如下:

rpm -ivh mysql-community-common-5.7.28-1.el6.x86_64.rpm
rpm -ivh mysql-community-libs-5.7.28-1.el6.x86_64.rpm
rpm -ivh mysql-community-client-5.7.28-1.el6.x86_64.rpm
rpm -ivh mysql-community-server-5.7.28-1.el6.x86_64.rpm

在安装 执行 rpm -ivh mysql-community-common-5.7.28-1.el6.x86_64.rpm 时,出现如下错误:

rpm -ivh mysql-community-common-5.7.28-1.el6.x86_64.rpm 
warning: mysql-community-common-5.7.28-1.el6.x86_64.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY
Preparing...                ########################################### [100%]
        file /usr/share/mysql/czech/errmsg.sys from install of mysql-community-common-5.7.28-1.el6.x86_64 conflicts with file from package mysql-libs-5.1.73-8.el6_8.x86_64
        file /usr/share/mysql/danish/errmsg.sys from install of mysql-community-common-5.7.28-1.el6.x86_64 conflicts with file from package mysql-libs-5.1.73-8.el6_8.x86_64
        file /usr/share/mysql/dutch/errmsg.sys from install of mysql-community-common-5.7.28-1.el6.x86_64 conflicts with file from package mysql-libs-5.1.73-8.el6_8.x86_64
        file /usr/share/mysql/english/errmsg.sys from install of mysql-community-common-5.7.28-1.el6.x86_64 conflicts with file from package mysql-libs-5.1.73-8.el6_8.x86_64
        file /usr/share/mysql/estonian/errmsg.sys from install of mysql-community-common-5.7.28-1.el6.x86_64 conflicts with file from package mysql-libs-5.1.73-8.el6_8.x86_64
        file /usr/share/mysql/french/errmsg.sys from install of mysql-community-common-5.7.28-1.el6.x86_64 conflicts with file from package mysql-libs-5.1.73-8.el6_8.x86_64
        file /usr/share/mysql/german/errmsg.sys from install of mysql-community-common-5.7.28-1.el6.x86_64 conflicts with file from package mysql-libs-5.1.73-8.el6_8.x86_64
        file /usr/share/mysql/greek/errmsg.sys from install of mysql-community-common-5.7.28-1.el6.x86_64 conflicts with file from package mysql-libs-5.1.73-8.el6_8.x86_64
        file /usr/share/mysql/hungarian/errmsg.sys from install of mysql-community-common-5.7.28-1.el6.x86_64 conflicts with file from package mysql-libs-5.1.73-8.el6_8.x86_64
        file /usr/share/mysql/italian/errmsg.sys from install of mysql-community-common-5.7.28-1.el6.x86_64 conflicts with file from package mysql-libs-5.1.73-8.el6_8.x86_64
        file /usr/share/mysql/japanese/errmsg.sys from install of mysql-community-common-5.7.28-1.el6.x86_64 conflicts with file from package mysql-libs-5.1.73-8.el6_8.x86_64
        file /usr/share/mysql/korean/errmsg.sys from install of mysql-community-common-5.7.28-1.el6.x86_64 conflicts with file from package mysql-libs-5.1.73-8.el6_8.x86_64
        file /usr/share/mysql/norwegian-ny/errmsg.sys from install of mysql-community-common-5.7.28-1.el6.x86_64 conflicts with file from package mysql-libs-5.1.73-8.el6_8.x86_64
        file /usr/share/mysql/norwegian/errmsg.sys from install of mysql-community-common-5.7.28-1.el6.x86_64 conflicts with file from package mysql-libs-5.1.73-8.el6_8.x86_64
        file /usr/share/mysql/polish/errmsg.sys from install of mysql-community-common-5.7.28-1.el6.x86_64 conflicts with file from package mysql-libs-5.1.73-8.el6_8.x86_64
        file /usr/share/mysql/portuguese/errmsg.sys from install of mysql-community-common-5.7.28-1.el6.x86_64 conflicts with file from package mysql-libs-5.1.73-8.el6_8.x86_64
        file /usr/share/mysql/romanian/errmsg.sys from install of mysql-community-common-5.7.28-1.el6.x86_64 conflicts with file from package mysql-libs-5.1.73-8.el6_8.x86_64
        file /usr/share/mysql/russian/errmsg.sys from install of mysql-community-common-5.7.28-1.el6.x86_64 conflicts with file from package mysql-libs-5.1.73-8.el6_8.x86_64
        file /usr/share/mysql/serbian/errmsg.sys from install of mysql-community-common-5.7.28-1.el6.x86_64 conflicts with file from package mysql-libs-5.1.73-8.el6_8.x86_64
        file /usr/share/mysql/slovak/errmsg.sys from install of mysql-community-common-5.7.28-1.el6.x86_64 conflicts with file from package mysql-libs-5.1.73-8.el6_8.x86_64
        file /usr/share/mysql/spanish/errmsg.sys from install of mysql-community-common-5.7.28-1.el6.x86_64 conflicts with file from package mysql-libs-5.1.73-8.el6_8.x86_64
        file /usr/share/mysql/swedish/errmsg.sys from install of mysql-community-common-5.7.28-1.el6.x86_64 conflicts with file from package mysql-libs-5.1.73-8.el6_8.x86_64
        file /usr/share/mysql/ukrainian/errmsg.sys from install of mysql-community-common-5.7.28-1.el6.x86_64 conflicts with file from package mysql-libs-5.1.73-8.el6_8.x86_64
        file /usr/share/mysql/charsets/Index.xml from install of mysql-community-common-5.7.28-1.el6.x86_64 conflicts with file from package mysql-libs-5.1.73-8.el6_8.x86_64
        file /usr/share/mysql/charsets/armscii8.xml from install of mysql-community-common-5.7.28-1.el6.x86_64 conflicts with file from package mysql-libs-5.1.73-8.el6_8.x86_64
        file /usr/share/mysql/charsets/ascii.xml from install of mysql-community-common-5.7.28-1.el6.x86_64 conflicts with file from package mysql-libs-5.1.73-8.el6_8.x86_64
        file /usr/share/mysql/charsets/cp1250.xml from install of mysql-community-common-5.7.28-1.el6.x86_64 conflicts with file from package mysql-libs-5.1.73-8.el6_8.x86_64
        file /usr/share/mysql/charsets/cp1251.xml from install of mysql-community-common-5.7.28-1.el6.x86_64 conflicts with file from package mysql-libs-5.1.73-8.el6_8.x86_64
        file /usr/share/mysql/charsets/cp1256.xml from install of mysql-community-common-5.7.28-1.el6.x86_64 conflicts with file from package mysql-libs-5.1.73-8.el6_8.x86_64
        file /usr/share/mysql/charsets/cp1257.xml from install of mysql-community-common-5.7.28-1.el6.x86_64 conflicts with file from package mysql-libs-5.1.73-8.el6_8.x86_64
        file /usr/share/mysql/charsets/cp850.xml from install of mysql-community-common-5.7.28-1.el6.x86_64 conflicts with file from package mysql-libs-5.1.73-8.el6_8.x86_64
        file /usr/share/mysql/charsets/cp852.xml from install of mysql-community-common-5.7.28-1.el6.x86_64 conflicts with file from package mysql-libs-5.1.73-8.el6_8.x86_64
        file /usr/share/mysql/charsets/cp866.xml from install of mysql-community-common-5.7.28-1.el6.x86_64 conflicts with file from package mysql-libs-5.1.73-8.el6_8.x86_64
        file /usr/share/mysql/charsets/dec8.xml from install of mysql-community-common-5.7.28-1.el6.x86_64 conflicts with file from package mysql-libs-5.1.73-8.el6_8.x86_64
        file /usr/share/mysql/charsets/geostd8.xml from install of mysql-community-common-5.7.28-1.el6.x86_64 conflicts with file from package mysql-libs-5.1.73-8.el6_8.x86_64
        file /usr/share/mysql/charsets/greek.xml from install of mysql-community-common-5.7.28-1.el6.x86_64 conflicts with file from package mysql-libs-5.1.73-8.el6_8.x86_64
        file /usr/share/mysql/charsets/hebrew.xml from install of mysql-community-common-5.7.28-1.el6.x86_64 conflicts with file from package mysql-libs-5.1.73-8.el6_8.x86_64
        file /usr/share/mysql/charsets/hp8.xml from install of mysql-community-common-5.7.28-1.el6.x86_64 conflicts with file from package mysql-libs-5.1.73-8.el6_8.x86_64
        file /usr/share/mysql/charsets/keybcs2.xml from install of mysql-community-common-5.7.28-1.el6.x86_64 conflicts with file from package mysql-libs-5.1.73-8.el6_8.x86_64
        file /usr/share/mysql/charsets/koi8r.xml from install of mysql-community-common-5.7.28-1.el6.x86_64 conflicts with file from package mysql-libs-5.1.73-8.el6_8.x86_64
        file /usr/share/mysql/charsets/koi8u.xml from install of mysql-community-common-5.7.28-1.el6.x86_64 conflicts with file from package mysql-libs-5.1.73-8.el6_8.x86_64
        file /usr/share/mysql/charsets/latin1.xml from install of mysql-community-common-5.7.28-1.el6.x86_64 conflicts with file from package mysql-libs-5.1.73-8.el6_8.x86_64
        file /usr/share/mysql/charsets/latin2.xml from install of mysql-community-common-5.7.28-1.el6.x86_64 conflicts with file from package mysql-libs-5.1.73-8.el6_8.x86_64
        file /usr/share/mysql/charsets/latin5.xml from install of mysql-community-common-5.7.28-1.el6.x86_64 conflicts with file from package mysql-libs-5.1.73-8.el6_8.x86_64
        file /usr/share/mysql/charsets/latin7.xml from install of mysql-community-common-5.7.28-1.el6.x86_64 conflicts with file from package mysql-libs-5.1.73-8.el6_8.x86_64
        file /usr/share/mysql/charsets/macce.xml from install of mysql-community-common-5.7.28-1.el6.x86_64 conflicts with file from package mysql-libs-5.1.73-8.el6_8.x86_64
        file /usr/share/mysql/charsets/macroman.xml from install of mysql-community-common-5.7.28-1.el6.x86_64 conflicts with file from package mysql-libs-5.1.73-8.el6_8.x86_64
        file /usr/share/mysql/charsets/swe7.xml from install of mysql-community-common-5.7.28-1.el6.x86_64 conflicts with file from package mysql-libs-5.1.73-8.el6_8.x86_64

说明存在冲突,这个linux发布里有mysql的残留,执行以下命令,删除

yum -y remove mysql-libs-5.1.73-8.el6_8.x86_64

从 rpm -ivh mysql-community-common-5.7.28-1.el6.x86_64.rpm 重新执行,结果如下:

 rpm -ivh mysql-community-common-5.7.28-1.el6.x86_64.rpm 
warning: mysql-community-common-5.7.28-1.el6.x86_64.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY
Preparing...                ########################################### [100%]
   1:mysql-community-common ########################################### [100%]

rpm -ivh mysql-community-libs-5.7.28-1.el6.x86_64.rpm 
warning: mysql-community-libs-5.7.28-1.el6.x86_64.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY
Preparing...                ########################################### [100%]
   1:mysql-community-libs   ########################################### [100%]

rpm -ivh mysql-community-client-5.7.28-1.el6.x86_64.rpm 
warning: mysql-community-client-5.7.28-1.el6.x86_64.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY
Preparing...                ########################################### [100%]
   1:mysql-community-client ########################################### [100%]

rpm -ivh mysql-community-server-5.7.28-1.el6.x86_64.rpm 
warning: mysql-community-server-5.7.28-1.el6.x86_64.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY
Preparing...                ########################################### [100%]
   1:mysql-community-server ########################################### [100%]

至此,mysql所用到的文件安装成功!

安装完成后,mysql安装程序会创建mysql这个系统帐号,如果不存在,请用如下命令创建mysql用户:

groupadd mysql
useradd -r -g mysql mysql

二、修改配置文件

mysql的服务安装成功后,
服务器程序文件位置在 :   /usr/sbin/mysqld,
同时配置文件位置在: /etc/my.cnf,
客户端程序位置在:  /usr/bin/mysql

修改配置文件,输入如下命令:

vi /etc/my.cnf

修改后的配置文件如下:

# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.7/en/server-configuration-defaults.html

#
# 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
[client]
socket=/home/mysql-5.7.28/mysql.sock
[mysqld]
explicit_defaults_for_timestamp=true
datadir=/home/mysql-5.7.28/data
socket=/home/mysql-5.7.28/mysql.sock
user=mysql
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0

log-error=/home/mysql-5.7.28/log/mysqld.log
pid-file=/home/mysql-5.7.28/mysqld.pid

my.cnf文件原来有内容,我将配置文件当中的目录一律改为 /home/mysql-5.7.28,这样便于以后升级或安装多个版本的mysql服务。注意,要设置好mysql客户端的socket,如下:

[client]
socket=/home/mysql-5.7.28/mysql.sock

如果未添加以上配置,则用mysql客户端时会提示找不到socket文件。

 

然后创建/home/mysql-5.7.28目录,命令如下:

cd /home
mkdir mysql-5.7.28
mkdir mysql-5.7.28/data
mkdir mysql-5.7.28/log
chown -R mysql:mysql mysql-5.7.28

三、首次启动mysqld服务,执行初始化

在命令好键入如下命令(不能用systemctl命令,CentOS7以上才支持,智能用service命令): 

service mysqld start

这个命令将执行初始化数据库操作,创建初始数据库。

执行完成,如果没有错误,会创建一个root用户,并设置一个临时口令,必须修改这个口令才能对mysql数据库进行下一步操作,请修改root用户的口令,用如下命令查看这个临时口令:

cat /home/mysql-5.7.28/log/mysqld.log | grep password

查看的结果如下:

2019-10-30T11:29:10.750559Z 0 [Note] Shutting down plugin 'sha256_password'
2019-10-30T11:29:10.750562Z 0 [Note] Shutting down plugin 'mysql_native_password'
2019-10-30T11:46:55.703659Z 1 [Note] A temporary password is generated for [email protected]: ftW61Ogr=IRM
2019-10-30T11:47:59.235604Z 2 [Note] Access denied for user 'UNKNOWN_MYSQL_USER'@'localhost' (using password: NO)
2019-10-30T11:48:05.209678Z 0 [Note] Shutting down plugin 'validate_password'
2019-10-30T11:48:06.523413Z 0 [Note] Shutting down plugin 'sha256_password'
2019-10-30T11:48:06.523416Z 0 [Note] Shutting down plugin 'mysql_native_password'

字符串:ftW61Ogr=IRM 就是临时口令。

在命令行里键入如下mysql客户端:

mysql -u root -p

输入如上临时口令;成功后将进入mysql命令环境。如下所示,即表示安装mysql成功。

Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.28

Copyright (c) 2000, 2019, 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命令行环境里执行: show databases; 将会看到如下提示:

You must reset your password using ALTER USER statement before executing this statement.

必须重置mysql的root用户口令,才能进一步操作。

由于mysql安装完成后初始化对命令的校验级别比较高,修改命令不会成功的,因此用如下命令修改命令的校验级别:

set global validate_password_policy=LOW;

然后用如下命令修改mysql的root口令:

ALTER USER 'root'@'localhost' IDENTIFIED BY '[email protected]';

root用户的口令修改为 [email protected],提示如下:

Query OK, 0 rows affected (0.00 sec)

再次敲入:show databases; 出现如下结果:

 show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.00 sec)

至此,mysql数据库全部安装成功,就可以用PHP,java来访问了。