MySQL是一个开源的免费关系数据库管理系统(RDBMS),在GNU(通用公共许可证)下发布。通过提供对每个创建的数据库的多用户访问,它可以在任意一台服务器上运行多个数据库。
步骤1:添加MySQL Yum Repository
我们将使用官方的MySQL Yum软件库,它将提供RPM包安装最新版本的MySQL服务器,客户端,MySQL实用程序,MySQL工作台,连接器/ODBC,连接器/Python的RHEL/CentOS 8/7/6/和Fedora 28-30。
如果已经使用第三方分发的RPM包安装了MySQL,那么我建议您使用MySQL Yum Repository升级或替换安装的MySQL包。
现在下载并将以下MySQL Yum库添加到各自的Linux发行系统的库列表中,以安装最新版本的MySQL(即2018年7月27日发布的8.0)
1 2 | --------------- On RHEL/CentOS 8 --------------- # wget https://repo.mysql.com/mysql80-community-release-el8-1.noarch.rpm |
1 2 | --------------- On RHEL/CentOS 7 --------------- # wget https://repo.mysql.com/mysql80-community-release-el7-1.noarch.rpm |
1 2 | --------------- On RHEL/CentOS 6 --------------- # wget https://dev.mysql.com/get/mysql80-community-release-el6-1.noarch.rpm |
1 2 | --------------- On Fedora 30 --------------- # wget https://dev.mysql.com/get/mysql80-community-release-fc30-1.noarch.rpm |
1 2 | --------------- On Fedora 29 --------------- # wget https://dev.mysql.com/get/mysql80-community-release-fc29-1.noarch.rpm |
1 2 | --------------- On Fedora 28 --------------- # wget https://dev.mysql.com/get/mysql80-community-release-fc28-1.noarch.rpm |
在为您的Linux平台下载包之后,现在使用以下命令安装下载的包。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | --------------- On RHEL/CentOS 8 --------------- # yum localinstall mysql80-community-release-el8-1.noarch.rpm --------------- On RHEL/CentOS 7 --------------- # yum localinstall mysql80-community-release-el7-1.noarch.rpm --------------- On RHEL/CentOS 6 --------------- # yum localinstall mysql80-community-release-el6-1.noarch.rpm --------------- On Fedora 30 --------------- # dnf localinstall mysql80-community-release-fc30-1.noarch.rpm --------------- On Fedora 29 --------------- # dnf localinstall mysql80-community-release-fc29-1.noarch.rpm --------------- On Fedora 28 --------------- # yum localinstall mysql80-community-release-fc28-1.noarch.rpm |
上面的安装命令将MySQL Yum存储库添加到系统存储库列表中,并下载GnuPG密钥来验证包的完整性。
可以使用以下命令验证MySQL Yum存储库是否已成功添加。
1 2 | # yum repolist enabled | grep "mysql.*-community.*" # dnf repolist enabled | grep "mysql.*-community.*" [On Fedora versions] |
步骤2:安装最新的MySQL版本
使用以下命令安装最新版本的MySQL(当前为8.0)。
1 2 3 4 | # yum install mysql # yum install mysql-server # dnf install mysql # dnf install mysql-server [On Fedora versions] |
上面的命令安装MySQL服务器MySQL -community-server, MySQL-community-client, MySQL-community-common和MySQL -community-libs所需的所有包。
步骤3:安装MySQL版本系列
你也可以使用不同的MySQL社区服务器的子库来安装不同的MySQL版本。默认情况下,最近的MySQL系列(当前的MySQL 8.0)的子存储库是激活的,所有其他版本(例如MySQL 5)的子存储库是激活的。X系列)默认为禁用。
要从特定的子库安装特定的版本,你可以使用yum-config-manager或dnf config-manager使用——enable或——disable选项,如下所示
1 2 | # yum-config-manager --disable mysql57-community # yum-config-manager --enable mysql56-community |
1 2 3 | ------------------ Fedora Versions ------------------ # dnf config-manager --disable mysql57-community # dnf config-manager --enable mysql56-community |
步骤4:启动MySQL服务器
MySQL安装成功后,使用以下命令启动MySQL服务器
1 2 3 | systemctl start mysqld #开启数据库mysql systemctl status mysqld #查看数据库mysql状态 systemctl enable mysqld #开机启动数据库 |
启动后查看 systemctl status mysqld
1 2 3 4 5 6 7 8 9 10 11 | Redirecting to /bin/systemctl status mysqld.service mysqld.service - MySQL Server Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled) Active: active (running) since Thu 2015-10-29 05:15:19 EDT; 4min 5s ago Process: 5314 ExecStart=/usr/sbin/mysqld --daemonize $MYSQLD_OPTS (code=exited, status=0/SUCCESS) Process: 5298 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS) Main PID: 5317 (mysqld) CGroup: /system.slice/mysqld.service └─5317 /usr/sbin/mysqld --daemonize Oct 29 05:15:19 localhost.localdomain systemd[1]: Started MySQL Server. |
现在,使用下面的命令来验证安装的MySQL版本。
1 2 3 | # mysql --version mysql Ver 8.0.12 for Linux on x86_64 (MySQL Community Server - GPL) |
步骤5:确保MySQL安装安全
mysql_secure_installation命令允许您执行一些重要的设置,例如设置root密码、删除匿名用户、删除root登录,等等,从而确保MySQL安装的安全。
注意:MySQL 8.0及以上版本安装后会在/var/log/mysqld.log中产生临时随机密码。
运行MySQL secure命令前,请先使用下面的命令查看密码。
1 | # grep 'temporary password' /var/log/mysqld.log |
一旦你知道密码,你现在可以运行以下命令来保护你的MySQL安装。
1 | # mysql_secure_installation |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 | Securing the MySQL server deployment. Enter password for user root: Enter New Root Password VALIDATE PASSWORD PLUGIN can be used to test passwords and improve security. It checks the strength of password and allows the users to set only those passwords which are secure enough. Would you like to setup VALIDATE PASSWORD plugin? Press y|Y for Yes, any other key for No: y There are three levels of password validation policy: LOW Length >= 8 MEDIUM Length >= 8, numeric, mixed case, and special characters STRONG Length >= 8, numeric, mixed case, special characters and dictionary file Please enter 0 = LOW, 1 = MEDIUM and 2 = STRONG: 2 Using existing password for root. Estimated strength of the password: 50 Change the password for root ? ((Press y|Y for Yes, any other key for No) : y New password: Set New MySQL Password Re-enter new password: Re-enter New MySQL Password Estimated strength of the password: 100 Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) : y 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) : y Success. 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) : y - Dropping test database... Success. - Removing privileges on test database... Success. 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! |
步骤6:连接MySQL服务器
通过提供用户名和密码连接到新安装的MySQL服务器。
1 | # mysql -u root -p |
1 2 3 4 5 6 7 8 9 10 11 12 | Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 19 Server version: 8.0.1 MySQL Community Server (GPL) Copyright (c) 2000, 2015, 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> |
步骤7:如果将来想更新MySQL,请使用用Yum更新MySQL
除了新安装,您还可以在以下命令的帮助下对MySQL产品和组件进行更新。
1 2 | # yum update mysql-server # dnf update mysql-server [On Fedora versions] |
当MySQL有新的更新时,它会自动安装它们,如果没有,你会得到一个消息说没有包标记为更新。
你已经成功安装了MySQL 8.0。如果你在安装时有任何困难,请使用我们的评论部分寻求解决方案。