简体   繁体   English

Digital Ocean / Nginx / MySQL / MVC 部署

[英]Digital Ocean / Nginx / MySQL / MVC deployment

I am trying to deploy and host a website through a Digital Ocean droplet using Nginx, MySQL and dotnet-core.我正在尝试使用 Nginx、MySQL 和 dotnet-core 通过 Digital Ocean 小滴部署和托管网站。 I was able to get the entire project over to the Digital Ocean droplet, but now I am having trouble updating the databases and get "An error occurred using the connection to database '' on server 'localhost'" & 'Access denied for user 'root'@'localhost'.我能够将整个项目转移到 Digital Ocean 小滴,但现在我在更新数据库时遇到问题,并收到“使用连接到服务器 'localhost' 上的数据库'' 时发生错误”&'用户拒绝访问'根'@'本地主机'。 I believe I didn't transfer over the user secrets correctly but I'm honestly not sure.我相信我没有正确转移用户机密,但老实说我不确定。 I transfered the entire project via GIT so I had to manually add all connection strings with the command 'dotnet user-secrets set "SecretKey" "SecretValue"'.我通过 GIT 传输了整个项目,因此我必须使用命令 'dotnet user-secrets set "SecretKey" "SecretValue"' 手动添加所有连接字符串。 I've already installed dotnet-core, MySQL, Nginx and all that good stuff on my digital ocean droplet.我已经在我的数字海洋液滴上安装了 dotnet-core、MySQL、Nginx 和所有好东西。

Thanks in advance!!提前致谢!!

'Access denied for user 'root'@'localhost' - I had the same issue on a Linux VM. 'Access denied for user 'root'@'localhost' - 我在 Linux VM 上遇到了同样的问题。 In my case the database user had access denied while connecting to the database.在我的情况下,数据库用户在连接到数据库时访问被拒绝。

Check if the database user account that you are using to connect to the Mysql database has proper access rights set.检查用于连接到Mysql数据库的数据库用户帐户是否设置了正确的访问权限。 Connect to the mysql database from shell and run the following query:从 shell 连接到 mysql 数据库并运行以下查询:

select user, host from mysql.user;

The output would be something like this:输出将是这样的:

+----------+-----------+
| user     | host      |
+----------+-----------+
| cron     | %         |
| custom   | %         |
| abc      | %         |
| slave    | %         |
| root     | 127.0.0.1 |
| root     | ::1       |
|          | localhost |
| cron     | localhost |
| abcuser  | localhost |
| root     | localhost |
|          | aaaaabox  |
| root     | aaaaabox  |
+----------+-----------+

As you can see the root has access right on localhost, ::1, 127.0.0.1 .如您所见, root拥有对localhost, ::1, 127.0.0.1 的访问权限。 Which are all local domains.这些都是本地域。

In your case if any of those are missing then use the following command to add the missing ones:在您的情况下,如果缺少其中任何一个,请使用以下命令添加缺少的:

GRANT ALL PRIVILEGES ON DBNAME.* TO 'root'@'localhost';

Where DBNAME is the name of your Database, root is the DB user name, localhost is the domain.其中DBNAME是数据库的名称, root是数据库用户名, localhost是域。

Please note: You should not be connecting to the mysql database with the root account.请注意:您不应该使用root帐户连接到 mysql 数据库。 You can create a separate account that will have access right limited to the application database only.您可以创建一个单独的帐户,该帐户的访问权限仅限于应用程序数据库。

Here is how you can create a new database user account and connect to the database from your application:以下是创建新数据库用户帐户并从应用程序连接到数据库的方法:

CREATE USER 'appuser'@'localhost' IDENTIFIED BY 'appuserpass';
GRANT ALL PRIVILEGES ON DBNAME.* TO 'appuser'@'localhost';

GRANT ALL ON DBNAME.* TO 'appuser'@'localhost' IDENTIFIED BY 'appuserpass';
GRANT ALL ON DBNAME.* TO 'appuser'@'127.0.0.1' IDENTIFIED BY 'appuserpass';
GRANT ALL ON DBNAME.* TO 'appuser'@'::1' IDENTIFIED BY 'appuserpass';

Here is the connection string:这是连接字符串:

  "ConnectionStrings": {
    "DefaultConnection": "server=127.0.0.1;user=appuser;password=appuserpass;port=3306;database=DBNAME;"
  },

Update:更新:

If you are trying to connect to a MySql database from outside the machine then you need to make sure:如果您尝试从机器外部连接到 MySql 数据库,那么您需要确保:

Do not forget to turn the remote access off when you are done debugging.完成调试后不要忘记关闭远程访问。 Otherwise this will create additional security conserns.否则,这将产生额外的安全隐患。

In order to solve my problem, I am taking a slightly different approach.为了解决我的问题,我采取了稍微不同的方法。

I am using the F1 (free tier) Azure app service to actually host the website.我正在使用 F1(免费层)Azure 应用服务来实际托管该网站。 I am however still going to use my Digital Ocean droplet to host my database.然而,我仍然会使用我的 Digital Ocean 小滴来托管我的数据库。 I am then going to use Cloudflare to point my nameservers to.然后我将使用 Cloudflare 将我的域名服务器指向。 That was I can use my personal domain for the website instead of using the Azure App service domain.那就是我可以将我的个人域用于网站,而不是使用 Azure App 服务域。 I know this isn't really an 'answer', but it provides an easier path to accomplish my goal.我知道这并不是真正的“答案”,但它提供了一条更简单的途径来实现我的目标。 Thank you for everyone's insight!谢谢大家的指点!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM