简体   繁体   中英

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. 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'. 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"'. I've already installed dotnet-core, MySQL, Nginx and all that good stuff on my digital ocean droplet.

Thanks in advance!!

'Access denied for user 'root'@'localhost' - I had the same issue on a 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. Connect to the mysql database from shell and run the following query:

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 . 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.

Please note: You should not be connecting to the mysql database with the root account. 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:

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. I am however still going to use my Digital Ocean droplet to host my database. I am then going to use Cloudflare to point my nameservers to. That was I can use my personal domain for the website instead of using the Azure App service domain. I know this isn't really an 'answer', but it provides an easier path to accomplish my goal. Thank you for everyone's insight!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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