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

【MySQL学习】在RHEL7.3上使用yum安装MySQL5.7

程序员文章站 2022-09-11 21:45:35
一、环境 二、官方源 获取官方源: 安装: 安装之后, /etc/yum.repos.d/目录下会产生两个文件:mysql-community-source.repo、mysql-community.repo。 三、创建组和用户 四、安装MySQL 由于RHEL7.3中默认安装了Mariadb,安装 ......

一、环境

[root@rhel:~]#cat /etc/redhat-release 
Red Hat Enterprise Linux Server release 7.3 (Maipo)
[root@rhel:~]#uname -r
3.10.0-514.el7.x86_64    
[root@rhel:~]#getenforce
Disabled

二、官方源

获取官方源:

[root@rhel:/opt]#wget http://repo.mysql.com/mysql57-community-release-el7-9.noarch.rpm
--2018-03-15 15:25:41--  http://repo.mysql.com/mysql57-community-release-el7-9.noarch.rpm
Resolving repo.mysql.com (repo.mysql.com)... 23.55.208.125
Connecting to repo.mysql.com (repo.mysql.com)|23.55.208.125|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 9224 (9.0K) [application/x-redhat-package-manager]
Saving to: ‘mysql57-community-release-el7-9.noarch.rpm’

100%[================================>] 9,224       1.80KB/s   in 5.0s   

2018-03-15 15:25:47 (1.80 KB/s) - ‘mysql57-community-release-el7-9.noarch.rpm’ saved [9224/9224]

安装:

[root@rhel:/opt]#rpm -ivh mysql57-community-release-el7-9.noarch.rpm
warning: mysql57-community-release-el7-9.noarch.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY
Preparing...                          ################################# [100%]
Updating / installing...
   1:mysql57-community-release-el7-9  ################################# [100%]

安装之后, /etc/yum.repos.d/目录下会产生两个文件:mysql-community-source.repomysql-community.repo

三、创建组和用户

[root@rhel:~]#groupadd mysql
[root@rhel:~]#useradd -r -g mysql -p root mysql
[root@rhel:~]#usermod -s /sbin/nologin mysql

四、安装MySQL

由于RHEL7.3中默认安装了Mariadb,安装MySQL时会报错,所以先卸载Mariadb。

[root@rhel:~]#rpm -qa|grep mariadb
mariadb-libs-5.5.52-1.el7.x86_64
mariadb-5.5.52-1.el7.x86_64
mariadb-server-5.5.52-1.el7.x86_64

卸载:

[root@rhel:~]#rpm -e mariadb-libs-5.5.52-1.el7.x86_64
error: Failed dependencies:
    libmysqlclient.so.18()(64bit) is needed by (installed) perl-DBD-MySQL-4.023-5.el7.x86_64
    libmysqlclient.so.18()(64bit) is needed by (installed) qt-mysql-1:4.8.5-13.el7.x86_64
    libmysqlclient.so.18()(64bit) is needed by (installed) postfix-2:2.10.1-6.el7.x86_64
    libmysqlclient.so.18()(64bit) is needed by (installed) zabbix-server-mysql-4.0.0-1.1alpha4.el7.x86_64
    libmysqlclient.so.18(libmysqlclient_18)(64bit) is needed by (installed) perl-DBD-MySQL-4.023-5.el7.x86_64
    libmysqlclient.so.18(libmysqlclient_18)(64bit) is needed by (installed) qt-mysql-1:4.8.5-13.el7.x86_64
    libmysqlclient.so.18(libmysqlclient_18)(64bit) is needed by (installed) postfix-2:2.10.1-6.el7.x86_64
    libmysqlclient.so.18(libmysqlclient_18)(64bit) is needed by (installed) zabbix-server-mysql-4.0.0-1.1alpha4.el7.x86_64
    mariadb-libs(x86-64) = 1:5.5.52-1.el7 is needed by (installed) mariadb-1:5.5.52-1.el7.x86_64
    mariadb-libs(x86-64) = 1:5.5.52-1.el7 is needed by (installed) mariadb-server-1:5.5.52-1.el7.x86_64

使用--nodeps强制卸载:

[root@rhel:~]#rpm -e --nodeps mariadb-libs-5.5.52-1.el7.x86_64
[root@rhel:~]#rpm -e --nodeps mariadb-5.5.52-1.el7.x86_64
[root@rhel:~]#rpm -e --nodeps mariadb-server-5.5.52-1.el7.x86_64

安装MySQL:

[root@rhel:~]#yum install mysql-server
Loaded plugins: aliases, changelog, langpacks, ovl, product-id, search-
              : disabled-repos, subscription-manager, tmprepo, verify,
              : versionlock
This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register.
file:///mnt/cdrom/repodata/repomd.xml: [Errno 14] curl#37 - "Couldn't open file /mnt/cdrom/repodata/repomd.xml"
Trying other mirror.
mysql-connectors-community                         | 2.5 kB     00:00     
mysql-tools-community                              | 2.5 kB     00:00     
mysql57-community                                  | 2.5 kB     00:00     
zabbix                                             | 2.9 kB     00:00     
zabbix-non-supported                               |  951 B     00:00     
Resolving Dependencies
--> Running transaction check
---> Package mysql-community-server.x86_64 0:5.7.21-1.el7 will be installed
--> Processing Dependency: mysql-community-common(x86-64) = 5.7.21-1.el7 for package: mysql-community-server-5.7.21-1.el7.x86_64
--> Processing Dependency: mysql-community-client(x86-64) >= 5.7.9 for package: mysql-community-server-5.7.21-1.el7.x86_64
--> Running transaction check
---> Package mysql-community-client.x86_64 0:5.7.21-1.el7 will be installed
--> Processing Dependency: mysql-community-libs(x86-64) >= 5.7.9 for package: mysql-community-client-5.7.21-1.el7.x86_64
---> Package mysql-community-common.x86_64 0:5.7.21-1.el7 will be installed
--> Running transaction check
---> Package mysql-community-libs.x86_64 0:5.7.21-1.el7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

==========================================================================
 Package                 Arch    Version         Repository          Size
==========================================================================
Installing:
 mysql-community-server  x86_64  5.7.21-1.el7    mysql57-community  164 M
Installing for dependencies:
 mysql-community-client  x86_64  5.7.21-1.el7    mysql57-community   24 M
 mysql-community-common  x86_64  5.7.21-1.el7    mysql57-community  272 k
 mysql-community-libs    x86_64  5.7.21-1.el7    mysql57-community  2.1 M

Transaction Summary
==========================================================================
Install  1 Package (+3 Dependent packages)

Total size: 190 M
Total download size: 188 M
Installed size: 858 M
Is this ok [y/d/N]: y
Downloading packages:
No Presto metadata available for mysql57-community
warning: /var/cache/yum/x86_64/7Server/mysql57-community/packages/mysql-community-client-5.7.21-1.el7.x86_64.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY
Public key for mysql-community-client-5.7.21-1.el7.x86_64.rpm is not installed
(1/2): mysql-community-client-5.7.21-1.el7.x86_64.rp |  24 MB   00:05     
(2/2): mysql-community-server-5.7.21-1.el7.x86_64.rp | 164 MB   02:02     
--------------------------------------------------------------------------
Total                                        1.5 MB/s | 188 MB  02:02     
Retrieving key from file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
Importing GPG key 0x5072E1F5:
 Userid     : "MySQL Release Engineering <mysql-build@oss.oracle.com>"
 Fingerprint: a4a9 4068 76fc bd3c 4567 70c8 8c71 8d3b 5072 e1f5
 Package    : mysql57-community-release-el7-9.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
Warning: RPMDB altered outside of yum.
** Found 10 pre-existing rpmdb problem(s), 'yum check' output follows:
PackageKit-1.0.7-6.el7.x86_64 has missing requires of PackageKit-backend
akonadi-mysql-1.9.2-4.el7.x86_64 has missing requires of mariadb-server
perl-DBD-MySQL-4.023-5.el7.x86_64 has missing requires of libmysqlclient.so.18()(64bit)
perl-DBD-MySQL-4.023-5.el7.x86_64 has missing requires of libmysqlclient.so.18(libmysqlclient_18)(64bit)
2:postfix-2.10.1-6.el7.x86_64 has missing requires of libmysqlclient.so.18()(64bit)
2:postfix-2.10.1-6.el7.x86_64 has missing requires of libmysqlclient.so.18(libmysqlclient_18)(64bit)
1:qt-mysql-4.8.5-13.el7.x86_64 has missing requires of libmysqlclient.so.18()(64bit)
1:qt-mysql-4.8.5-13.el7.x86_64 has missing requires of libmysqlclient.so.18(libmysqlclient_18)(64bit)
zabbix-server-mysql-4.0.0-1.1alpha4.el7.x86_64 has missing requires of libmysqlclient.so.18()(64bit)
zabbix-server-mysql-4.0.0-1.1alpha4.el7.x86_64 has missing requires of libmysqlclient.so.18(libmysqlclient_18)(64bit)
  Installing : mysql-community-common-5.7.21-1.el7.x86_64             1/4 
  Installing : mysql-community-libs-5.7.21-1.el7.x86_64               2/4 
  Installing : mysql-community-client-5.7.21-1.el7.x86_64             3/4 
  Installing : mysql-community-server-5.7.21-1.el7.x86_64                                                                                                          4/4 
  Verifying  : mysql-community-server-5.7.21-1.el7.x86_64                                                                                                          1/4 
  Verifying  : mysql-community-common-5.7.21-1.el7.x86_64                                                                                                          2/4 
  Verifying  : mysql-community-libs-5.7.21-1.el7.x86_64                                                                                                            3/4 
  Verifying  : mysql-community-client-5.7.21-1.el7.x86_64                                                                                                          4/4 

Installed:
  mysql-community-server.x86_64 0:5.7.21-1.el7                                                                                                                         

Dependency Installed:
  mysql-community-client.x86_64 0:5.7.21-1.el7            mysql-community-common.x86_64 0:5.7.21-1.el7            mysql-community-libs.x86_64 0:5.7.21-1.el7           

Complete!

 

五、初始化mysql

使用mysqld --initialize初始化mysql

 --initialize :初始化时,会生成一个root用户和一个随机的密码,并且密码设置为过期,需要立即修改密码。确保数据库目录与文件的所有者为mysql账户,以便在运行mysqld服务时对这些目录和文件有读取与写入权限。以root身份运行mysqld服务,需指定--user=mysql选项

     注意本例没有指定user选项,所以后面启动mysql时出现报错,需要修改相关文件系统权限才可以正常启动,使用下面的命令初始化会避免这一问题(未验证,待定)

#mysqld --initialize --user=mysql

--initialize-insecure :另外,可以使用该配置初始化,但这种方式会生成一个root用户,并且密码是空(第一次登录密码空即可),同样需要立即修改密码。可以使用mysql -u root --skip-password直接登录mysql。

本例采用第一种:

[root@rhel:~]#mysqld --initialize

初始化后,会生成一个root用户(mysql的用户,非系统的root用户)和一个随机密码。

[root@rhel:~]#which mysqld
/usr/sbin/mysqld
[root@rhel:~]#grep 'temporary password' /var/log/mysqld.log
2018-03-16T01:10:40.569111Z 1 [Note] A temporary password is generated for root@localhost: _iBhf7suG,go

六、启动mysql,查看mysql的状态并设置开机启动

[root@rhel:~]#systemctl start mysqld 
Redirecting to /bin/systemctl start  mysqld.service
Job for mysqld.service failed because the control process exited with error code. See "systemctl status mysqld.service" and "journalctl -xe" for details.

无法启动,需要修改/var/lib/mysql的权限(由于初始化时没有指定用户--user=mysql,未验证,待定)

[root@rhel:~]#chown mysql:mysql -R /var/lib/mysql

再次启动,成功

[root@rhel:~]#systemctl start mysqld
Redirecting to /bin/systemctl start  mysqld.service

查看mysql状态

[root@rhel:~]#systemctl status mysqld
Redirecting to /bin/systemctl status  mysqld.service
● mysqld.service - MySQL Server
   Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor 
   Active: active (running) since Fri 2018-03-16 09:37:07 CST; 1h 21min ago
     Docs: man:mysqld(8)
           http://dev.mysql.com/doc/refman/en/using-systemd.html
  Process: 16216 ExecStart=/usr/sbin/mysqld --daemonize --pid-file=/var/run
  Process: 16194 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, sta
 Main PID: 16219 (mysqld)
   CGroup: /system.slice/mysqld.service
           └─16219 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/

Mar 16 09:37:05 rhel systemd[1]: Starting MySQL Server...
Mar 16 09:37:07 rhel systemd[1]: Started MySQL Server.

设置开机启动

[root@rhel:~]#systemctl enable mysqld

其实质是,增加由/usr/lib/systemd/system/到/etc/systemd/system/multi-user.target.wants/下的软链接

ln -s '/usr/lib/systemd/system/mysqld.service' '/etc/systemd/system/multi-user.target.wants/mysqld.service'
 
使用命令  #systemctl disable mysqld  可以取消开机自动启动,实质是删除/etc/systemd/system/multi-user.target.wants/下的mysqld.service软连接

七、修改mysql的root密码

首先,修改mysql的root用户密码,因为安全配置向导里需要输入密码,而随机生成的密码比较复杂,所以设置新密码方便输入。

共有两种修改方式:

第一种,使用mysqladmin修改密码

  1.如果 mysql的root用户现在没有密码,你希望的密码修改为123456,那么命令是:

# mysqladmin -u root password 123456 

  2.如果mysql的root现在有密码(123456),那么修改密码为abcdef的命令是:

  # mysqladmin -u root -p password abcdef

注意,命令回车后会提示旧密码,输入旧密码123456之后命令完成,密码修改成功。

[root@rhel:~]#mysqladmin -u root -p password abcdef
Enter password: 
mysqladmin: [Warning] Using a password on the command line interface can be insecure.
Warning: Since password will be sent to server in plain text, use ssl connection to ensure password safety.

 第二种,若是忘记密码,可以通过以下方式重置mysql的root用户密码

      编辑/etc/my.cnf文件, 在 [mysqld] 中加上一行跳过权限限制(skip-grant-tables)

root@rhel:~]#vi /etc/my.cnf

[mysqld]

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

skip-grant-tables

      保存退出,重启mysql服务

[root@rhel:~]#systemctl restart mysqld

     然后登录,修改密码字段,刷新权限

[root@rhel:~]#mysql -uroot -p
Enter password:                ###密码为空,直接回车即可
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> 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
mysql> update user set authentication_string=password('123456') where user='root';      
mysql> flush privileges;

退出,然后编辑/etc/my.cnf文件, 在 [mysqld] 中删掉一行跳过权限限制(skip-grant-tables)

root@rhel:~]#vi /etc/my.cnf
[mysqld]

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

skip-grant-tables       #######删除此行

保存退出,重启mysql服务

[root@rhel:~]#systemctl restart mysqld

 登录mysql,若提示需要再次修改密码,则根据提示修改,若无此提示直接连至数据库。

[root@rhel:~]#mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.21

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> 

八、MySQL安全配置向导mysql_secure_installation

 

[root@rhel:~]#mysql_secure_installation

Securing the MySQL server deployment.

Enter password for user root: 
The 'validate_password' plugin is installed on the server.
The subsequent steps will run with the existing configuration
of the plugin.
Using existing password for root.

Estimated strength of the password: 25 
Change the password for root ? ((Press y|Y for Yes, any other key for No) : n

 ... skipping.
By default, a MySQL installation has an anonymous user,
allowing anyone to log into MySQL without having to have
a user account created for them. This is intended only for
testing, and to make the installation go a bit smoother.
You should remove them before moving into a production
environment.

Remove anonymous users? (Press y|Y for Yes, any other key for No) : y     ######是否移除匿名用户
Success.


Normally, root should only be allowed to connect from
'localhost'. This ensures that someone cannot guess at
the root password from the network.

Disallow root login remotely? (Press y|Y for Yes, any other key for No) : n     ##############是否禁止mysql的root用户远程登录,测试环境,所以允许

 ... skipping.
By default, MySQL comes with a database named 'test' that
anyone can access. This is also intended only for testing,
and should be removed before moving into a production
environment.


Remove test database and access to it? (Press y|Y for Yes, any other key for No) : n ########是否移除test数据库,测试环境,保留

 ... skipping.
Reloading the privilege tables will ensure that all changes
made so far will take effect immediately.

Reload privilege tables now? (Press y|Y for Yes, any other key for No) : y       #######是否重新加载权限表
Success.

All done! 

 

至此,安装完成。