0

I'm trying to do some testing with github actions.
previous post: test-not-found-in-github-actions

Now that the test is recognized, all that's left is to configure mysql.
I'm getting the error "User access is denied" https://github.com/duri0214/portfolio/actions/runs/4250191595/jobs/7391059026#step:4:569

Usually this error goes away by granting privileges to mysql.
It has been confirmed on the production server that django works by giving the following nine permissions.

-- in VirtualPrivateServer
mysql> CREATE DATABASE portfolio_db DEFAULT CHARACTER SET utf8mb4;
       Query OK, 1 row affected (0.01 sec)
mysql> CREATE USER 'python'@'%' IDENTIFIED BY 'XXXXXXXX';
mysql> grant CREATE, DROP, SELECT, UPDATE, INSERT, DELETE, ALTER, REFERENCES, INDEX on portfolio_db.* to 'python'@'localhost';

However, I don't really understand how to type this mysql command.
I get an error even if I issue a command to github actions as follows.
I was able to confirm that the secret was output
https://github.com/duri0214/portfolio/actions/runs/4249942981/jobs/7390568462#step:4:424

mysql -u${{ secrets.DB_USER }} -p${{ secrets.DB_PASSWORD }} -e 'CREATE DATABASE portfolio_db DEFAULT CHARACTER SET utf8mb4;'
mysql -u${{ secrets.DB_USER }} -p${{ secrets.DB_PASSWORD }} -e 'CREATE USER '${{ secrets.DB_USER }}'@'localhost' IDENTIFIED BY '${{ secrets.DB_PASSWORD }}';'
mysql -u${{ secrets.DB_USER }} -p${{ secrets.DB_PASSWORD }} -e 'grant CREATE, DROP, SELECT, UPDATE, INSERT, DELETE, ALTER, REFERENCES, INDEX on portfolio_db.* to '${{ secrets.DB_USER }}'@'localhost';'
Warning: arning] Using a password on the command line interface can be insecure.
ERROR 1045 (28000): Access denied for user '***'@'localhost' (using password: YES)
Error: Process completed with exit code 1.

I've tried many things, but I'm getting tired...
Anyone know how to solve this?

additional

for Azeem san :)

Django will use the information found here to create a throwaway database. this

At this time, I don't know if the credentials are from '.env' or disposable credentials.

As far as I can see the log, I was able to create the migration file, but it seems that I cannot log in to the database. this

If disposable authentication information is fine, 'echo' around here may not be necessarythis

yoshitaka okada
  • 113
  • 1
  • 12
  • Relevant: https://stackoverflow.com/questions/75169859/mysql-import-error-in-github-actions-with-laravel – Azeem Feb 23 '23 at 10:05
  • In your workflow, you just [started the MySQL server](https://github.com/duri0214/portfolio/actions/runs/4250191595/workflow#L37) but did not configure its default credentials. But, in your commands, you're giving your own credentials. You need to configure MySQL first. Apart from that, I'm not sure why you would do that for a local database which will be destroyed after the tests are complete. You could use the default credentials and be on your way. – Azeem Feb 23 '23 at 10:09
  • @Azeem san, thank you again. :) I may not be familiar with the 'default credentials' Is it okay to talk Django specific? When I run the Django test (`python3 manage.py test`), it creates a test database in the background and completes the test in my local environment. The reference link has instructions using docker. but not very familiar with docker. I think you are referring to the default credentials, probably using the 'env' keyword and filling in the 'DB_...' values. I will add the django test to the article, so could you take a look at it later? – yoshitaka okada Feb 23 '23 at 13:45
  • No worries. :) Please go through the [thread](https://stackoverflow.com/questions/75169859/mysql-import-error-in-github-actions-with-laravel) that I shared earlier. It does exactly what you're looking for. – Azeem Feb 23 '23 at 13:56
  • 'docker' can take in environment variables for certain keywords and currently the only way to test mysql on ubuntu is to use docker. am i right? The '<<<' mark seems to be stuck, but is the syntax correct? [this](https://github.com/duri0214/portfolio/actions/runs/4258566254/workflow) It seems that SELECT is issued to the created table – yoshitaka okada Feb 24 '23 at 08:24
  • Enclose query in double quotes i.e. `<<< "SELECT id,name,email FROM person;" 2>/dev/null`. – Azeem Feb 24 '23 at 08:34
  • hmm... Changing to double quotes doesn't seem to change the situation. [this](https://github.com/duri0214/portfolio/actions/runs/4260856552/workflow) – yoshitaka okada Feb 24 '23 at 09:00
  • It's the comment at line [82](https://github.com/duri0214/portfolio/actions/runs/4260856552/workflow#L82) which was causing parsing issues. Move `#` right next to the command under that multiline string block. – Azeem Feb 24 '23 at 11:43
  • I made some progress by delete and undoing some code. I think this error should have been a Mysql error. thank you for always [this](https://github.com/duri0214/portfolio/actions/runs/4262540014/jobs/7418167639#step:6:419) – yoshitaka okada Feb 24 '23 at 12:54
  • You are already using MySQL via the services' container. So, line [81](https://github.com/duri0214/portfolio/actions/runs/4262540014/workflow#L81) is redundant. Remove this and it should work. – Azeem Feb 24 '23 at 12:57
  • hoo.. went 100 commits over lol. If I remove line 81, I go to see the extra socket and get an error[this](https://github.com/duri0214/portfolio/actions/runs/4262661324/jobs/7418425928#step:6:568) – yoshitaka okada Feb 24 '23 at 13:08
  • You're still using credentials from `secrets` [here](https://github.com/duri0214/portfolio/actions/runs/4262661324/workflow#L75-L80). Use the DB credentials that are in your workflow. See [here](https://github.com/duri0214/portfolio/actions/runs/4262661324/workflow#L75-L80) and [here](https://github.com/duri0214/portfolio/actions/runs/4262661324/workflow#L59). – Azeem Feb 24 '23 at 13:17
  • Apparently the error content seems to have changed to django's. The error message is probably asking to read information from .env.[this](https://github.com/duri0214/portfolio/actions/runs/4262872057/jobs/7418895195#step:6:497) – yoshitaka okada Feb 24 '23 at 13:36
  • That's progress. MySQL issue is fixed. Now, it's on the django side. See https://stackoverflow.com/questions/26080303/improperlyconfigured-settings-databases-is-improperly-configured-please-supply. – Azeem Feb 24 '23 at 13:39
  • [this](https://github.com/duri0214/portfolio/blob/master/mysite/mysite/settings.py#L80-L90) is the file Django normally reads database configuration information from '.env'. For that reason, I was creating a ".env" in "job" and trying to login to mysql using the secret. If I'm true to one side, I'm false to the other. – yoshitaka okada Feb 24 '23 at 13:57
  • Setting up automated tests is so difficult... – yoshitaka okada Feb 24 '23 at 13:59
  • If it is resolved, you are contributing so much that I want to pay little to you with Paypal if you want. But even if the problem persists, we did our best, so I'm happy. – yoshitaka okada Feb 24 '23 at 14:27
  • Set up your last step like [this](https://rhysd.github.io/actionlint/#eJyNkcFuwjAMhu99Cr9Ay2G33phAE4exCTRxRGlj2tDUDnFShrSHX1ohxgFtu0SW/8+/HZtUjyXs2HcHy+csYyrBRWmz7MiVlBlAFY3VYwDgI0k+ErGKFGJuVUAJkyQBXcKnOAeaXFckQVkLC3RIGqk2KKBIjz4wVsqEAyAN5TUEWDzvl+uX1XpZgj4qarjQVVGpukseUvQXOdl7dj1/TeTotnfsw4Gt4Xv9Y7vclOCZw332fb7d7t42i/RZJXJmr69qGq2ErxvqLqFleoK8hyFNOT03UTj6GqfcrDI0U3UwQ1rJo2pnHJjrPvI8usYrjWP2T9iDx1M0HnukIEX4/PlIrSHtwzzo2CtSDRbukqIOe5PaBcMkyasx6Vb+/xWDwZDuufcoqHzdQtMr36EHadk5Qw1YQ1hxgLPy2HKUX8eZjH8jxlN+A4W3zog=). – Azeem Feb 24 '23 at 17:34
  • [this](https://github.com/duri0214/portfolio/actions/runs/4266860798/jobs/7427858646#step:6:490) is how it looks now. I go to see the extra socket and get an error. thank you for always. Azeem san. I wonder if 'touch /tmp/mysql.sock' is necessary – yoshitaka okada Feb 25 '23 at 02:06
  • Your `settings.py` doesn't take `HOST` and `PORT` from the env vars so using MySQL won't work directly because Django's default for `HOST` is `localhost` and that's why it looks for the Unix-domain socket. With `127.0.0.1`, it'll connect on the TCP PORT. So, I resorted to using the default MySQL. You were creating the `test_portfolio` DB but the tests create and destroy it themselves so its creation was redundant. You should create your actual DB otherwise e.g. `portfolio`, not `test_portfolio`. See this [workflow](https://github.com/iamazeem/test/actions/runs/4268979995/workflow). – Azeem Feb 25 '23 at 07:46
  • See the workflow logs [here](https://github.com/iamazeem/test/actions/runs/4268979995/jobs/7431836996#step:4:451). Now, only 3 tests are failing. From the logs, it looks like those tests need to be updated accordingly. I didn't look at the tests though. This workflow also depend on `DJANGO_SECRET_KEY` so I generated a random one [here](https://github.com/iamazeem/test/actions/runs/4268979995/workflow#L42). You'll be setting it up via the `.env` file. – Azeem Feb 25 '23 at 07:49
  • Here's the updated [workflow](https://github.com/iamazeem/test/actions/runs/4269029667/workflow#L21-L42) that you need to refer to for adjusting your own. – Azeem Feb 25 '23 at 08:04
  • we passed!! great! Wow! [Ran 18 tests in 0.744s](https://github.com/duri0214/portfolio/actions/runs/4270013538/jobs/7433565895#step:4:578). Please rewrite the answer as I will mark it as solved. You have a lot of stackoverflow badges. great person. I will write an article about the flow so far and share it with Japanese people. If you give me your Paypal account, I will pay my feelings. I wonder be about the price of lunch :) – yoshitaka okada Feb 25 '23 at 12:56
  • Glad I could be of help! :) I have posted it as an answer. You might want to consider and start using `docker` and `docker-compose` as your app and its setup will be much easier to handle with `docker-compose`. All the best! – Azeem Feb 25 '23 at 14:22

1 Answers1

0

Your settings.py doesn't take HOST and PORT from the env vars so using MySQL won't work with services because Django's default for HOST is localhost and that's why it looks for the Unix domain socket. With an env var HOST set to 127.0.0.1, it'll connect on the TCP PORT.

So, I resorted to using the default preinstalled MySQL instance:

sudo systemctl start mysql

You were creating the test_portfolio DB but the tests create and destroy it themselves so its creation was redundant and created conflicts when the tests are run. You should create your actual DB e.g. portfolio, not test_portfolio.

mysql -uroot -proot -e "CREATE DATABASE 'portfolio';"
mysql -uroot -proot -e "SHOW DATABASES;"

Here is the workflow that I tested on my side:

name: python_django_test

on: workflow_dispatch

jobs:
  ci:
    runs-on: ubuntu-latest

    steps:
    - name: Checkout
      uses: actions/checkout@v3
      with:
        repository: duri0214/portfolio

    - name: Set up Python
      uses: actions/setup-python@v4
      with:
        python-version: 3.8
        cache: pip

    - name: Set up and run tests
      env:
        DB_ENGINE: django.db.backends.mysql
        DB_NAME: portfolio
        DB_USER: root
        DB_PASSWORD: root
      run: |
        sudo systemctl start mysql
        mysql -u$DB_USER -p$DB_PASSWORD -e "CREATE DATABASE $DB_NAME;"
        mysql -u$DB_USER -p$DB_PASSWORD -e "SHOW DATABASES;"
        python3 --version
        python3 -m venv venv
        . venv/bin/activate
        python -m pip install --upgrade pip
        python -m pip install -r requirements.txt
        cd mysite
        python manage.py makemigrations register
        python manage.py makemigrations vietnam_research gmarker shopping linebot warehouse
        python manage.py migrate
        export DJANGO_SECRET_KEY="$(base64 <<< "$RANDOM|TeStiNg|$RANDOM" | tr -d '\n')"
        python manage.py test

Please observe DJANGO_SECRET_KEY in there. Your app requires this so I just generated a random value for this. You might want to use your own.

Also, now the tests run but some of them fail so you need to adjust those accordingly. It's now on your app side. GHA workflow is working fine.

Azeem
  • 11,148
  • 4
  • 27
  • 40