debian linux 安装 mysql (远程仓库方式安装)

在本教程中,我们将向您展示如何在MySQL Apt Repository的Debian 9机器上安装和保护MySQL。 如果您的应用程序没有任何特定要求,您应该坚持使用Debian 9中的默认数据库系统MariaDB。

配置MySQL的软件仓库

要将MySQL APT存储库添加到系统,请转到存储库下载页面并下载最新的发行包:

wget http://repo.mysql.com/mysql-apt-config_0.8.10-1_all.deb

下载完成后,使用以下命令安装发行包:

sudo dpkg -i mysql-apt-config_0.8.10-1_all.deb

您将看到配置菜单,您可以在其中选择要安装的MySQL版本。

debian linux 安装 mysql (远程仓库方式安装)_第1张图片MySQL版本选中

MySQL 8.0是默认选择的,如果你想安装另一个版本的MySQL,请选择MySQL Server&Cluster(当前选择:mysql-8.0)并选择你喜欢的MySQL版本

我们要安装MySQL 8.0版。 选择最后一个选项确定,然后按Enter键(如上图所示)以保存配置。

Tip:在撰写本文时,最新版本的MySQL是8.0版。 如果您不确定选择哪个版本,请参阅您要在服务器上部署的应用程序的文档。

 

安装MySQL

在Debian 9服务器上安装MySQL之前,首先使用以下命令更新软件包列表:

sudo apt update

更新软件包列表后,运行以下命令在Debian服务器上安装MySQL:

sudo apt install mysql-server

安装程序将要求您设置MySQL root密码。 现在不要设置密码(留空),我们将在下一节中进行设置。 接下来,您将看到一条消息,通知您有关新的MySQL 8身份验证的信息。 在选择默认的mysql 8身份验证插件之前,请确保您的应用程序支持它。

debian linux 安装 mysql (远程仓库方式安装)_第2张图片密码配置

验证MySQL安装

安装完成后,MySQL服务将自动启动。

我们可以通过输入以下内容来检查MySQL服务状态

sudo systemctl status mysql

输出内容:

● mysql.service - MySQL Community Server
   Loaded: loaded (/lib/systemd/system/mysql.service; enabled; vendor preset: 
   Active: active (running) since Thu 2018-08-02 17:22:18 UTC; 18s ago
     Docs: man:mysqld(8)
           http://dev.mysql.com/doc/refman/en/using-systemd.html
  Process: 14797 ExecStartPre=/usr/share/mysql-8.0/mysql-systemd-start pre (co
 Main PID: 14832 (mysqld)
   Status: "SERVER_OPERATING"
    Tasks: 37 (limit: 4915)
   CGroup: /system.slice/mysql.service
           └─14832 /usr/sbin/mysqld

 

 

保护MySQL

运行mysql_secure_installation命令设置root密码并提高MySQL安装的安全性:

sudo mysql_secure_installation

输出内容:

Securing the MySQL server deployment.

Connecting to MySQL using 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 set only those passwords which are
secure enough. Would you like to setup VALIDATE PASSWORD component?

Press y|Y for Yes, any other key for No: 

系统将要求您配置VALIDATE PASSWORD PLUGIN,用于测试MySQL用户密码的强度。 密码验证策略有三个级别,低,中和强。 如果您不想设置验证密码插件,请按ENTER。

输出内容:

Please set the password for root here.

New password: 

Re-enter new password:

在下一个提示中,将要求您为MySQL root用户设置密码。

输出内容:

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! 

设置root密码后,脚本还会要求您删除匿名用户,限制root用户访问本地计算机并删除测试数据库。 你应该对所有问题回答“是”(是)。

从命令行连接到MySQL

要通过终端与MySQL交互,我们将使用MySQL客户端作为MySQL服务器包的依赖项。

以root用户身份登录MySQL服务器:

mysql -u root -p

系统将提示您输入运行mysql_secure_installation脚本时先前设置的root密码。

输入密码后,您将看到mysql shell,如下所示:

输出内容:

Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 10
Server version: 8.0.12 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>

 

创建一个数据库

连接到MySQL shell后,可以通过键入以下命令来创建新数据库:

mysql > CREATE DATABASE new_database;
Query OK, 1 row affected (0.00 sec)

 

创建数据库表

现在我们已经创建了一个数据库,我们可以创建一个表来存储一些数据。

在运行用于创建表的SQL语句之前,我们需要连接到数据库:

mysql > use new_database;

在这个例子中,我们将创建一个名为contacts的简单表,其中包含三个字段:id,name和email:

CREATE TABLE contacts (
  id INT PRIMARY KEY,
  name VARCHAR(30),
  email VARCHAR(30)
);
输出:
Query OK, 1 row affected (0.00 sec)

 

总结

在本教程中,我们向您展示了如何在Debian 9服务器上安装和保护MySQL服务器。 我们还向您展示了如何连接到MySQL shell以及如何创建新的数据库和表。

装完我出错:不用密码或者直接(mysql登录,我惊呆了),解决如下:

ubuntu mysql5.7 解决不用密码也能登录

登录mysql mysql -u root -p 或 mysql 

use mysql; 

update user set authentication_string=PASSWORD("密码") where user='root';

update user set plugin="mysql_native_password";(plugin密码验证的插件)

flush privileges;

quit; 

/etc/init.d/mysql restart;

mysql -u root -p 密码;

最后我想用用navicat连接远程连接mysql,我改了 host=% 和 plugin="mysql_native_password"(navicat支持的密码验证插件),但还不行。原来

我们连接远程服务器的mysql,如果出现问题,很大问题会出在服务器的端口和授权问题

首先我们通过

①:netstat -an|grep 3306

来查看mysql默认的端口3306是否开启,允许哪个ip使用,如果你发现,前面有127.0.0.1,就说明,3306端口只能本机ip使用

所以,我们需要

②:打开mysql配置文件vi /etc/mysql/mysql.conf.d/mysqld.cnf

将bind-address = 127.0.0.1注销​(修改后重启生效,/etc/init.d/mysql restart;)

③:进入mysql,对远程用户进行授权,

grant all privileges on *.* to 'root'@'%' identified by 'xxxxxx';

这里的root 是你远程登录的用户,xxxxxx是你登录使用的密码,然后可以在mysql数据 表中查看到你这个用户已经被添加到user表中

最后改密方式+1

  用mysqladmin 
格式:mysqladmin -u用户名 -p旧密码 password 新密码 
例子:mysqladmin -uroot -p123456 password 123 

另外:阿里云上的密码忘了。好多网上命令不行(mysql8.0以上真坑,只能用alter)。最后用笨方法解决的。

1 跳过验证,在vim /etc/my.cnf添加skip-grant-tables,重启。

2 把密码置空,在删除 skip-grant-tables,重启。

3 在ALTER USER ‘root’@’localhost’ IDENTIFIED BY ‘新密码’,生效命令:flush privileges;

可能要修改密码策略5.7和5.8如下:

  set global validate_password_policy=0;变为 set global validate_password.policy=0 ;

  密码最小长度(6):

  set global validate_password_length=6; 变为 set global validate_password.length=6

 

你可能感兴趣的:(随便看看,易有料)