如何在 Linux 上安装 MySQL

举报
Tiamo_T 发表于 2021/10/07 13:47:01 2021/10/07
【摘要】 大多数 Linux 发行版都带有 MySQL。如果你想使用 MySQL,我的建议是你下载最新版本的 MySQL 并自行安装。稍后您可以在它可用时将其升级到最新版本。在本文中,我将解释如何在 Linux 平台上安装最新的免费社区版 MySQL。1.下载MySQL最新的稳定版本从mysql.com下载 mySQL 。请下载适合您的 Linux 平台的 MySQL 社区版。我下载了“Red Hat...

大多数 Linux 发行版都带有 MySQL。如果你想使用 MySQL,我的建议是你下载最新版本的 MySQL 并自行安装。稍后您可以在它可用时将其升级到最新版本。在本文中,我将解释如何在 Linux 平台上安装最新的免费社区版 MySQL。

1.下载MySQL最新的稳定版本

mysql.com下载 mySQL 。请下载适合您的 Linux 平台的 MySQL 社区版。我下载了“Red Hat Enterprise Linux 5 RPM (x86)”。确保从下载页面下载 MySQL 服务器、客户端和“头文件和库”。

  • MySQL-client-community-5.1.25-0.rhel5.i386.rpm
  • MySQL-server-community-5.1.25-0.rhel5.i386.rpm
  • MySQL-devel-community-5.1.25-0.rhel5.i386.rpm


2. 删除 Linux 发行版附带的现有默认 MySQL

不要在某些应用程序正在使用 MySQL 数据库的系统上执行此操作。

[local-host]# rpm -qa | grep -i mysql
mysql-5.0.22-2.1.0.1
mysqlclient10-3.23.58-4.RHEL4.1

[local-host]# rpm -e mysql --nodeps
warning: /etc/my.cnf saved as /etc/my.cnf.rpmsave
[local-host]# rpm -e mysqlclient10

3.安装下载的MySQL包

安装 MySQL 服务器和客户端包,如下所示。

[local-host]# rpm -ivh MySQL-server-community-5.1.25-0.rhel5.i386.rpm MySQL-client-community-5.1.25-0.rhel5.i386.rpm
Preparing...                ########################################### [100%]
1:MySQL-client-community ########################################### [ 50%]
2:MySQL-server-community ########################################### [100%]

这还将显示以下输出并自动启动 MySQL 守护程序。

PLEASE REMEMBER TO SET A PASSWORD FOR THE MySQL root USER !
To do so, start the server, then issue the following commands:
/usr/bin/mysqladmin -u root password 'new-password'
/usr/bin/mysqladmin -u root -h medica2 password 'new-password'

Alternatively you can run:
/usr/bin/mysql_secure_installation
which will also give you the option of removing the test
databases and anonymous user created by default.  This is
strongly recommended for production servers.
See the manual for more instructions.
Please report any problems with the /usr/bin/mysqlbug script!
The latest information about MySQL is available at http://www.mysql.com/
Support MySQL by buying support/licenses from http://shop.mysql.com/

Starting MySQL.[  OK  ]
Giving mysqld 2 seconds to start

安装 MySQL-devel 包中的“Header and Libraries”。


[local-host]# rpm -ivh MySQL-devel-community-5.1.25-0.rhel5.i386.rpm
Preparing...                ########################################### [100%]
1:MySQL-devel-community  ########################################### [100%]

注意:当我在 Linux 系统上使用 MySQL 选项从源代码编译 PHP 时,它失败并出现以下错误。安装 MySQL-devel-community 包修复了从源代码安装 PHP 时的这个问题。

configure: error: Cannot find MySQL header files under yes.
Note that the MySQL client library is not bundled anymore!

4. 在 MySQL 上执行安装后安全活动。

您至少应该为 root 用户设置一个密码,如下所示:

[local-user]# /usr/bin/mysqladmin -u root password 'My2Secure$Password'

最好的选择是运行 mysql_secure_installation 脚本,它将处理 MySQL 上所有典型的安全相关项目,如下所示。在高层次上,它执行以下项目:

  • 更改根密码
  • 删除匿名用户
  • 禁止从远程机器进行 root 登录
  • 删除默认示例测试数据库
[local-host]# /usr/bin/mysql_secure_installation

NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MySQL
SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!
In order to log into MySQL to secure it, we'll need the current
password for the root user.  If you've just installed MySQL, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.

Enter current password for root (enter for none):
OK, successfully used password, moving on...

Setting the root password ensures that nobody can log into the MySQL
root user without the proper authorisation.
You already have a root password set, so you can safely answer 'n'.
Change the root password? [Y/n] Y
New password:
Re-enter new password:
Password updated successfully!
Reloading privilege tables..
... Success!
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? [Y/n] 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? [Y/n] 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? [Y/n] 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? [Y/n] Y
... Success!
Cleaning up...
All done!  If you've completed all of the above steps, your MySQL
installation should now be secure.
Thanks for using MySQL!

5. 验证 MySQL 安装:

您可以通过执行 mysql -V 来检查 MySQL 安装的版本,如下所示:

[local-host]# mysql -V
mysql  Ver 14.14 Distrib 5.1.25-rc, for redhat-linux-gnu (i686) using readline 5.1

使用root用户连接MySQL数据库并确保连接成功。

[local-host]# mysql -u root -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 13
Server version: 5.1.25-rc-community MySQL Community Server (GPL)

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

mysql>

按照以下步骤停止和启动 MySQL

[local-host]# service mysql status
MySQL running (12588)                                      [  OK  ]
[local-host]# service mysql stop
Shutting down MySQL.                                       [  OK  ]
[local-host]# service mysql start
Starting MySQL.                                            [  OK  ]
【版权声明】本文为华为云社区用户原创内容,转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息, 否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。