CentOS安装MySQL5.7_第1张图片

1.下载MYSQL的rpm包


[root@VM_0_4_centos ~]# wget http://dev.mysql.com/get/mysql57-community-release-el7-8.noarch.rpm
--2019-05-18 11:41:52--  http://dev.mysql.com/get/mysql57-community-release-el7-8.noarch.rpm
Resolving dev.mysql.com (dev.mysql.com)... 137.254.60.11
Connecting to dev.mysql.com (dev.mysql.com)|137.254.60.11|:80... connected.
HTTP request sent, awaiting response... 301 Moved Permanently
Location: https://dev.mysql.com/get/mysql57-community-release-el7-8.noarch.rpm [following]
--2019-05-18 11:41:53--  https://dev.mysql.com/get/mysql57-community-release-el7-8.noarch.rpm
Connecting to dev.mysql.com (dev.mysql.com)|137.254.60.11|:443... connected.
HTTP request sent, awaiting response... 302 Found
Location: https://repo.mysql.com//mysql57-community-release-el7-8.noarch.rpm [following]
--2019-05-18 11:42:04--  https://repo.mysql.com//mysql57-community-release-el7-8.noarch.rpm
Resolving repo.mysql.com (repo.mysql.com)... 23.60.72.128
Connecting to repo.mysql.com (repo.mysql.com)|23.60.72.128|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 9116 (8.9K) [application/x-redhat-package-manager]
Saving to: ‘mysql57-community-release-el7-8.noarch.rpm’

100%[======================================>] 9,116       25.5KB/s   in 0.3s   

2019-05-18 11:42:07 (25.5 KB/s) - ‘mysql57-community-release-el7-8.noarch.rpm’ saved [9116/9116]


2.安装MySQL的yum源


[root@VM_0_4_centos ~]# yum localinstall mysql57-community-release-el7-8.noarch.rpm
Loaded plugins: fastestmirror, langpacks
Examining mysql57-community-release-el7-8.noarch.rpm: mysql57-community-release-el7-8.noarch
Marking mysql57-community-release-el7-8.noarch.rpm to be installed
Resolving Dependencies
--> Running transaction check
---> Package mysql57-community-release.noarch 0:el7-8 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

================================================================================
 Package             Arch   Version
                                  Repository                               Size
================================================================================
Installing:
 mysql57-community-release
                     noarch el7-8 /mysql57-community-release-el7-8.noarch 8.2 k

Transaction Summary
================================================================================
Install  1 Package

Total size: 8.2 k
Installed size: 8.2 k
Is this ok [y/d/N]: y
Downloading packages:
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Warning: RPMDB altered outside of yum.
  Installing : mysql57-community-release-el7-8.noarch                       1/1 
  Verifying  : mysql57-community-release-el7-8.noarch                       1/1 

Installed:
  mysql57-community-release.noarch 0:el7-8                                      

Complete!


3.检查MySQL的yum源是否安装成功


[root@VM_0_4_centos ~]# yum repolist enabled | grep "mysql.*-community.*"
mysql-connectors-community/x86_64    MySQL Connectors Community              10
mysql-tools-community/x86_64         MySQL Tools Community                    9
mysql57-community/x86_64             MySQL 5.7 Community Server              34


4.修改默认安装的MySQL版本


[root@VM_0_4_centos ~]# vim /etc/yum.repos.d/mysql-community.repo
[mysql-connectors-community]
name=MySQL Connectors Community
baseurl=http://repo.mysql.com/yum/mysql-connectors-community/el/7/$basearch/
enabled=1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql

[mysql-tools-community]
name=MySQL Tools Community
baseurl=http://repo.mysql.com/yum/mysql-tools-community/el/7/$basearch/
enabled=1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql

# Enable to use MySQL 5.5
[mysql55-community]
name=MySQL 5.5 Community Server
baseurl=http://repo.mysql.com/yum/mysql-5.5-community/el/7/$basearch/
enabled=0
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql

# Enable to use MySQL 5.6
[mysql56-community]
name=MySQL 5.6 Community Server
baseurl=http://repo.mysql.com/yum/mysql-5.6-community/el/7/$basearch/
enabled=0
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql

[mysql57-community]
name=MySQL 5.7 Community Server
baseurl=http://repo.mysql.com/yum/mysql-5.7-community/el/7/$basearch/
enabled=1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql

[mysql-tools-preview]
name=MySQL Tools Preview
baseurl=http://repo.mysql.com/yum/mysql-tools-preview/el/7/$basearch/
enabled=0
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql


5.安装MySQL


[root@VM_0_4_centos ~]# yum install mysql-community-server
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
Resolving Dependencies
--> Running transaction check
---> Package mysql-community-server.x86_64 0:5.7.26-1.el7 will be installed
--> Processing Dependency: mysql-community-common(x86-64) = 5.7.26-1.el7 for package: mysql-community-server-5.7.26-1.el7.x86_64
--> Processing Dependency: mysql-community-client(x86-64) >= 5.7.9 for package: mysql-community-server-5.7.26-1.el7.x86_64
--> Processing Dependency: libnuma.so.1(libnuma_1.2)(64bit) for package: mysql-community-server-5.7.26-1.el7.x86_64
--> Processing Dependency: libnuma.so.1(libnuma_1.1)(64bit) for package: mysql-community-server-5.7.26-1.el7.x86_64
--> Processing Dependency: libnuma.so.1()(64bit) for package: mysql-community-server-5.7.26-1.el7.x86_64
--> Running transaction check
---> Package mysql-community-client.x86_64 0:5.7.26-1.el7 will be installed
--> Processing Dependency: mysql-community-libs(x86-64) >= 5.7.9 for package: mysql-community-client-5.7.26-1.el7.x86_64
---> Package mysql-community-common.x86_64 0:5.7.26-1.el7 will be installed
---> Package numactl-libs.x86_64 0:2.0.9-7.el7 will be installed
--> Running transaction check
---> Package mariadb-libs.x86_64 1:5.5.52-1.el7 will be obsoleted
--> Processing Dependency: libmysqlclient.so.18()(64bit) for package: 2:postfix-2.10.1-6.el7.x86_64
--> Processing Dependency: libmysqlclient.so.18(libmysqlclient_18)(64bit) for package: 2:postfix-2.10.1-6.el7.x86_64
---> Package mysql-community-libs.x86_64 0:5.7.26-1.el7 will be obsoleting
--> Running transaction check
---> Package mysql-community-libs-compat.x86_64 0:5.7.26-1.el7 will be obsoleting
---> Package postfix.x86_64 2:2.10.1-6.el7 will be updated
---> Package postfix.x86_64 2:2.10.1-7.el7 will be an update
--> Finished Dependency Resolution

Dependencies Resolved

================================================================================
 Package                      Arch    Version          Repository          Size
================================================================================
Installing:
 mysql-community-libs         x86_64  5.7.26-1.el7     mysql57-community  2.2 M
     replacing  mariadb-libs.x86_64 1:5.5.52-1.el7
 mysql-community-libs-compat  x86_64  5.7.26-1.el7     mysql57-community  2.0 M
     replacing  mariadb-libs.x86_64 1:5.5.52-1.el7
 mysql-community-server       x86_64  5.7.26-1.el7     mysql57-community  166 M
Installing for dependencies:
 mysql-community-client       x86_64  5.7.26-1.el7     mysql57-community   24 M
 mysql-community-common       x86_64  5.7.26-1.el7     mysql57-community  274 k
 numactl-libs                 x86_64  2.0.9-7.el7      os                  29 k
Updating for dependencies:
 postfix                      x86_64  2:2.10.1-7.el7   os                 2.4 M

Transaction Summary
================================================================================
Install  3 Packages (+3 Dependent packages)
Upgrade             ( 1 Dependent package)

Total download size: 197 M
Is this ok [y/d/N]: y
Downloading packages:
Delta RPMs disabled because /usr/bin/applydeltarpm not installed.
warning: /var/cache/yum/x86_64/7/mysql57-community/packages/mysql-community-common-5.7.26-1.el7.x86_64.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY
Public key for mysql-community-common-5.7.26-1.el7.x86_64.rpm is not installed
(1/7): mysql-community-common-5.7.26-1.el7.x86_64.rpm      | 274 kB   00:04     
(2/7): mysql-community-client-5.7.26-1.el7.x86_64.rpm      |  24 MB   00:22     
(3/7): mysql-community-libs-compat-5.7.26-1.el7.x86_64.rpm | 2.0 MB   00:02     
(4/7): numactl-libs-2.0.9-7.el7.x86_64.rpm                 |  29 kB   00:00     
(5/7): mysql-community-libs-5.7.26-1.el7.x86_64.rpm        | 2.2 MB   00:20     
(6/7): postfix-2.10.1-7.el7.x86_64.rpm                     | 2.4 MB   00:00     
(7/7): mysql-community-server-5.7.26-1.el7.x86_64.rpm      | 166 MB   01:10     
--------------------------------------------------------------------------------
Total                                              2.1 MB/s | 197 MB  01:35     
Retrieving key from file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
Importing GPG key 0x5072E1F5:
 Userid     : "MySQL Release Engineering "
 Fingerprint: a4a9 4068 76fc bd3c 4567 70c8 8c71 8d3b 5072 e1f5
 Package    : mysql57-community-release-el7-8.noarch (installed)
 From       : /etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
Is this ok [y/N]: y
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : mysql-community-common-5.7.26-1.el7.x86_64                   1/9 
  Installing : mysql-community-libs-5.7.26-1.el7.x86_64                     2/9 
  Installing : mysql-community-client-5.7.26-1.el7.x86_64                   3/9 
  Installing : mysql-community-libs-compat-5.7.26-1.el7.x86_64              4/9 
  Installing : numactl-libs-2.0.9-7.el7.x86_64                              5/9 
  Installing : mysql-community-server-5.7.26-1.el7.x86_64                   6/9 
  Updating   : 2:postfix-2.10.1-7.el7.x86_64                                7/9 
  Cleanup    : 2:postfix-2.10.1-6.el7.x86_64                                8/9 
  Erasing    : 1:mariadb-libs-5.5.52-1.el7.x86_64                           9/9 
  Verifying  : mysql-community-server-5.7.26-1.el7.x86_64                   1/9 
  Verifying  : numactl-libs-2.0.9-7.el7.x86_64                              2/9 
  Verifying  : mysql-community-client-5.7.26-1.el7.x86_64                   3/9 
  Verifying  : 2:postfix-2.10.1-7.el7.x86_64                                4/9 
  Verifying  : mysql-community-common-5.7.26-1.el7.x86_64                   5/9 
  Verifying  : mysql-community-libs-compat-5.7.26-1.el7.x86_64              6/9 
  Verifying  : mysql-community-libs-5.7.26-1.el7.x86_64                     7/9 
  Verifying  : 1:mariadb-libs-5.5.52-1.el7.x86_64                           8/9 
  Verifying  : 2:postfix-2.10.1-6.el7.x86_64                                9/9 

Installed:
  mysql-community-libs.x86_64 0:5.7.26-1.el7                                    
  mysql-community-libs-compat.x86_64 0:5.7.26-1.el7                             
  mysql-community-server.x86_64 0:5.7.26-1.el7                                  

Dependency Installed:
  mysql-community-client.x86_64 0:5.7.26-1.el7                                  
  mysql-community-common.x86_64 0:5.7.26-1.el7                                  
  numactl-libs.x86_64 0:2.0.9-7.el7                                             

Dependency Updated:
  postfix.x86_64 2:2.10.1-7.el7                                                 

Replaced:
  mariadb-libs.x86_64 1:5.5.52-1.el7                                            

Complete!


6.启动MySQL并查看MySQL运行状态


[root@VM_0_4_centos ~]# systemctl start mysqld
[root@VM_0_4_centos ~]# systemctl status mysqld
● mysqld.service - MySQL Server
   Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
   Active: active (running) since Sat 2019-05-18 11:55:59 CST; 15s ago
     Docs: man:mysqld(8)
           http://dev.mysql.com/doc/refman/en/using-systemd.html
  Process: 3231 ExecStart=/usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid $MYSQLD_OPTS (code=exited, status=0/SUCCESS)
  Process: 3156 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)
 Main PID: 3235 (mysqld)
   CGroup: /system.slice/mysqld.service
           └─3235 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mys...

May 18 11:55:52 VM_0_4_centos systemd[1]: Starting MySQL Server...
May 18 11:55:59 VM_0_4_centos systemd[1]: Started MySQL Server.


7.设置MySQL开机启动


[root@VM_0_4_centos ~]# systemctl enable mysqld
[root@VM_0_4_centos ~]# systemctl daemon-reload


8.查看MySQL安装的默认密码。

mysql安装完成之后,在/var/log/mysqld.log文件中给root生成了一个默认密码。通过下面的方式找到root默认密码,然后登录mysql进行修改:


[root@VM_0_4_centos ~]# grep 'temporary password' /var/log/mysqld.log
2019-05-18T03:55:53.402018Z 1 [Note] A temporary password is generated for root@localhost: keIh:7roUido


9.修改MySQL密码


[root@VM_0_4_centos ~]# mysql -uroot -pkeIh:7roUido
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.26

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> use mysql
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'MyNewPassWord@666';
Query OK, 0 rows affected (0.00 sec)


10.修改MySQL的密码策略:


[root@VM_0_4_centos ~]# 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

[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
validate_password_policy=2
## 重启MySQL服务器
[root@VM_0_4_centos ~]# systemctl restart mysqld


10.设置MySQL远程连接


[root@VM_0_4_centos ~]# mysql -uroot -pYourPassword
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.26 MySQL Community Server (GPL)

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> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'YourPassword' WITH GRANT OPTION;                   
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)


11.设置MySQL默认的字符编码为UTF-8


# 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
validate_password_policy=2
character_set_server=utf8
init_connect='SET NAMES utf8'


12.CentOS下安装MySQL默认的配置文件位置

默认配置文件路径: 

配置文件:/etc/my.cnf 

日志文件:/var/log//var/log/mysqld.log 

服务启动脚本:/usr/lib/systemd/system/mysqld.service 

socket文件:/var/run/mysqld/mysqld.pid