Debian 9上安装MySQL
<p>在本教程中,我们将向您展示如何在MySQL Apt Repository的Debian 9机器上安装和保护MySQL。</p><p>如果您的应用程序没有任何特定要求,您应该坚持使用Debian 9中的默认数据库系统MariaDB。</p>
<p> </p>
<h3 id="list0">配置MySQL的软件仓库</h3>
<p>要将MySQL APT存储库添加到系统,请转到存储库下载页面并下载最新的发行包:</p>
<div class="cnblogs_code">
<pre>wget http:<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">repo.mysql.com/mysql-apt-config_0.8.10-1_all.deb</span></pre>
</div>
<p> </p>
<p>下载完成后,使用以下命令安装发行包:</p>
<div class="cnblogs_code">
<pre>sudo dpkg -i mysql-apt-config_0.<span style="color: rgba(128, 0, 128, 1)">8.10</span>-1_all.deb</pre>
</div>
<p>您将看到配置菜单,您可以在其中选择要安装的MySQL版本。</p>
<p><img src="https://img2020.cnblogs.com/blog/1458112/202112/1458112-20211213133340741-1475749091.png" alt="" loading="lazy"></p>
<p> </p>
<p> </p>
<p> MySQL 8.0是默认选择的,如果你想安装另一个版本的MySQL,请选择MySQL Server&Cluster(当前选择:mysql-8.0)并选择你喜欢的MySQL版本</p>
<p> </p>
<p>我们要安装MySQL 8.0版。 选择最后一个选项确定,然后按Enter键(如上图所示)以保存配置。</p>
<h3 id="list1">安装MySQL</h3>
<p>在Debian 9服务器上安装MySQL之前,首先使用以下命令更新软件包列表:</p>
<div class="cnblogs_code">
<pre>sudo apt update</pre>
</div>
<p> </p>
<p>更新软件包列表后,运行以下命令在Debian服务器上安装MySQL:</p>
<div class="cnblogs_code">
<pre>sudo apt install mysql-server</pre>
</div>
<p>安装程序将要求您设置MySQL root密码。 现在不要设置密码(留空),我们将在下一节中进行设置。</p>
<p>接下来,您将看到一条消息,通知您有关新的MySQL 8身份验证的信息。</p>
<p>在选择默认的mysql 8身份验证插件之前,请确保您的应用程序支持它。</p>
<p><img src="https://img2020.cnblogs.com/blog/1458112/202112/1458112-20211213133533898-977326241.png" alt="" loading="lazy"></p>
<p> </p>
<p> </p>
<p> </p>
<h3 id="list2">验证MySQL安装</h3>
<p>安装完成后,MySQL服务将自动启动。</p>
<p>我们可以通过输入以下内容来检查MySQL服务状态</p>
<div class="cnblogs_code">
<pre>sudo systemctl status mysql</pre>
</div>
<p>输出内容:</p>
<div class="cnblogs_code">
<pre>● mysql.service -<span style="color: rgba(0, 0, 0, 1)"> MySQL Community Server
Loaded: loaded (</span>/lib/systemd/system/<span style="color: rgba(0, 0, 0, 1)">mysql.service; enabled; vendor preset:
Active: active (running) since Thu </span><span style="color: rgba(128, 0, 128, 1)">2018</span>-<span style="color: rgba(128, 0, 128, 1)">08</span>-<span style="color: rgba(128, 0, 128, 1)">02</span> <span style="color: rgba(128, 0, 128, 1)">17</span>:<span style="color: rgba(128, 0, 128, 1)">22</span>:<span style="color: rgba(128, 0, 128, 1)">18</span><span style="color: rgba(0, 0, 0, 1)"> UTC; 18s ago
Docs: man:mysqld(</span><span style="color: rgba(128, 0, 128, 1)">8</span><span style="color: rgba(0, 0, 0, 1)">)
http:</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">dev.mysql.com/doc/refman/en/using-systemd.html</span>
Process: <span style="color: rgba(128, 0, 128, 1)">14797</span> ExecStartPre=/usr/share/mysql-<span style="color: rgba(128, 0, 128, 1)">8.0</span>/mysql-systemd-<span style="color: rgba(0, 0, 0, 1)">start pre (co
Main PID: </span><span style="color: rgba(128, 0, 128, 1)">14832</span><span style="color: rgba(0, 0, 0, 1)"> (mysqld)
Status: </span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">SERVER_OPERATING</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">
Tasks: </span><span style="color: rgba(128, 0, 128, 1)">37</span> (limit: <span style="color: rgba(128, 0, 128, 1)">4915</span><span style="color: rgba(0, 0, 0, 1)">)
CGroup: </span>/system.slice/<span style="color: rgba(0, 0, 0, 1)">mysql.service
└─</span><span style="color: rgba(128, 0, 128, 1)">14832</span> /usr/sbin/mysqld</pre>
</div>
<p> </p>
<h3 id="list3">保护MySQL</h3>
<p>运行mysql_secure_installation命令设置root密码并提高MySQL安装的安全性:</p>
<div class="cnblogs_code">
<pre>sudo mysql_secure_installation</pre>
</div>
<p>输出内容:</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 0, 1)">Securing the MySQL server deployment.
Connecting to MySQL </span><span style="color: rgba(0, 0, 255, 1)">using</span><span style="color: rgba(0, 0, 0, 1)"> a blank password.
VALIDATE PASSWORD COMPONENT can be used to test passwords
and improve security. It checks the strength of password
and allows the users to </span><span style="color: rgba(0, 0, 255, 1)">set</span><span style="color: rgba(0, 0, 0, 1)"> only those passwords which are
secure enough. Would you like to setup VALIDATE PASSWORD component</span>?<span style="color: rgba(0, 0, 0, 1)">
Press y</span>|Y <span style="color: rgba(0, 0, 255, 1)">for</span> Yes, any other key <span style="color: rgba(0, 0, 255, 1)">for</span> No: </pre>
</div>
<p>系统将要求您配置VALIDATE PASSWORD PLUGIN,用于测试MySQL用户密码的强度。 密码验证策略有三个级别,低,中和强。 如果您不想设置验证密码插件,请按ENTER。</p>
<p>输出内容:</p>
<div class="cnblogs_code">
<pre>Please <span style="color: rgba(0, 0, 255, 1)">set</span> the password <span style="color: rgba(0, 0, 255, 1)">for</span><span style="color: rgba(0, 0, 0, 1)"> root here.
New password:
Re</span>-enter <span style="color: rgba(0, 0, 255, 1)">new</span> password:</pre>
</div>
<p> </p>
<p>在下一个提示中,将要求您为MySQL root用户设置密码。</p>
<p>输出内容:</p>
<div class="cnblogs_code">
<pre>By <span style="color: rgba(0, 0, 255, 1)">default</span><span style="color: rgba(0, 0, 0, 1)">, a MySQL installation has an anonymous user,
allowing anyone to log into MySQL without having to have
a user account created </span><span style="color: rgba(0, 0, 255, 1)">for</span> them. This <span style="color: rgba(0, 0, 255, 1)">is</span> intended only <span style="color: rgba(0, 0, 255, 1)">for</span><span style="color: rgba(0, 0, 0, 1)">
testing, and to make the installation go a bit smoother.
You should remove them before moving into a production
environment.
Remove anonymous users</span>? (Press y|Y <span style="color: rgba(0, 0, 255, 1)">for</span> Yes, any other key <span style="color: rgba(0, 0, 255, 1)">for</span><span style="color: rgba(0, 0, 0, 1)"> No) : y
Success.
Normally, root should only be allowed to connect </span><span style="color: rgba(0, 0, 255, 1)">from</span>
<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">localhost</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">. This ensures that someone cannot guess at
the root password </span><span style="color: rgba(0, 0, 255, 1)">from</span><span style="color: rgba(0, 0, 0, 1)"> the network.
Disallow root login remotely</span>? (Press y|Y <span style="color: rgba(0, 0, 255, 1)">for</span> Yes, any other key <span style="color: rgba(0, 0, 255, 1)">for</span><span style="color: rgba(0, 0, 0, 1)"> No) : y
Success.
By </span><span style="color: rgba(0, 0, 255, 1)">default</span>, MySQL comes with a database named <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">test</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)"> that
anyone can access. This </span><span style="color: rgba(0, 0, 255, 1)">is</span> also intended only <span style="color: rgba(0, 0, 255, 1)">for</span><span style="color: rgba(0, 0, 0, 1)"> testing,
and should be removed before moving into a production
environment.
Remove test database and access to it</span>? (Press y|Y <span style="color: rgba(0, 0, 255, 1)">for</span> Yes, any other key <span style="color: rgba(0, 0, 255, 1)">for</span><span style="color: rgba(0, 0, 0, 1)"> No) : y
</span>-<span style="color: rgba(0, 0, 0, 1)"> Dropping test database...
Success.
</span>-<span style="color: rgba(0, 0, 0, 1)"> 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</span>? (Press y|Y <span style="color: rgba(0, 0, 255, 1)">for</span> Yes, any other key <span style="color: rgba(0, 0, 255, 1)">for</span><span style="color: rgba(0, 0, 0, 1)"> No) : y
Success.
All done</span>! </pre>
</div>
<p>设置root密码后,脚本还会要求您删除匿名用户,限制root用户访问本地计算机并删除测试数据库。 你应该对所有问题回答“是”(是)。</p>
<p> </p>
<h3 id="list4">从命令行连接到MySQL</h3>
<p>要通过终端与MySQL交互,我们将使用MySQL客户端作为MySQL服务器包的依赖项。</p>
<p>以root用户身份登录MySQL服务器:</p>
<div class="cnblogs_code">
<pre>mysql -u root -p</pre>
</div>
<p>系统将提示您输入运行mysql_secure_installation脚本时先前设置的root密码。</p>
<p>输入密码后,您将看到mysql shell,如下所示:</p>
<p>输出内容:</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 0, 1)">Welcome to the MySQL monitor.Commands end with ; or \g.
Your MySQL connection id </span><span style="color: rgba(0, 0, 255, 1)">is</span> <span style="color: rgba(128, 0, 128, 1)">10</span><span style="color: rgba(0, 0, 0, 1)">
Server version: </span><span style="color: rgba(128, 0, 128, 1)">8.0</span>.<span style="color: rgba(128, 0, 128, 1)">12</span> MySQL Community Server -<span style="color: rgba(0, 0, 0, 1)"> GPL
Copyright (c) </span><span style="color: rgba(128, 0, 128, 1)">2000</span>, <span style="color: rgba(128, 0, 128, 1)">2018</span>, Oracle and/<span style="color: rgba(0, 0, 0, 1)">or its affiliates. All rights reserved.
Oracle </span><span style="color: rgba(0, 0, 255, 1)">is</span> a registered trademark of Oracle Corporation and/<span style="color: rgba(0, 0, 0, 1)">or its
affiliates. Other names may be trademarks of their respective
owners.
Type </span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">help;</span><span style="color: rgba(128, 0, 0, 1)">'</span> or <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">\h</span><span style="color: rgba(128, 0, 0, 1)">'</span> <span style="color: rgba(0, 0, 255, 1)">for</span> help. Type <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">\c</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)"> to clear the current input statement.
mysql</span>></pre>
</div>
<h3> </h3>
<h3>开启访问权限</h3>
<div class="cnblogs_code">
<pre>mysql><span style="color: rgba(0, 0, 0, 1)">use mysql
mysql</span>>update user <span style="color: rgba(0, 0, 255, 1)">set</span> plugin=<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">mysql_native_password</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">;
mysql</span>>flush privileges;</pre>
</div>
<p> </p>
<h3 id="list5">创建一个数据库</h3>
<p>连接到MySQL shell后,可以通过键入以下命令来创建新数据库:</p>
<div class="cnblogs_code">
<pre>mysql ><span style="color: rgba(0, 0, 0, 1)"> CREATE DATABASE new_database;
Query OK, </span><span style="color: rgba(128, 0, 128, 1)">1</span> row affected (<span style="color: rgba(128, 0, 128, 1)">0.00</span> sec)</pre>
</div>
<p> </p>
<h3 id="list6">创建数据库表</h3>
<p>现在我们已经创建了一个数据库,我们可以创建一个表来存储一些数据。</p>
<p>在运行用于创建表的SQL语句之前,我们需要连接到数据库:</p>
<div class="cnblogs_code">
<pre>mysql > use new_database;</pre>
</div>
<p> </p>
<p>在这个例子中,我们将创建一个名为contacts的简单表,其中包含三个字段:id,name和email:</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 0, 1)">CREATE TABLE contacts (
id INT PRIMARY KEY,
name VARCHAR(</span><span style="color: rgba(128, 0, 128, 1)">30</span><span style="color: rgba(0, 0, 0, 1)">),
email VARCHAR(</span><span style="color: rgba(128, 0, 128, 1)">30</span><span style="color: rgba(0, 0, 0, 1)">)
);
输出:
Query OK, </span><span style="color: rgba(128, 0, 128, 1)">1</span> row affected (<span style="color: rgba(128, 0, 128, 1)">0.00</span> sec)</pre>
</div>
<p> </p>
<h3 id="list7">总结</h3>
<p>在本教程中,我们向您展示了如何在Debian 9服务器上安装和保护MySQL服务器。 我们还向您展示了如何连接到MySQL shell以及如何创建新的数据库和表。</p>
<p> </p><br><br>
来源:https://www.cnblogs.com/zhengsongsong/p/15682954.html
頁:
[1]