Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using MySQL as Database and Connecting it with Django #10

Merged
merged 1 commit into from
Oct 16, 2019
Merged

Conversation

davin111
Copy link
Contributor

@davin111 davin111 commented Oct 13, 2019

Related Issue: #8
This PR will resolve #8 issue.

Major change

I modified 'backend/backend_python/settings.py' to use MySQL as our default databases.

How to use MySQL with Django in your local environment

Even though it was a really small change, I think each of us should setup environment for this. Because we can't use a server like Azure all the time(at least at the early sprints), I think each of us should install MySQL in environment, create a database with the specific name, and also create an user with the specific name and password. Please let me know if you have a better idea.

Please follow the instruction below. I suppose you use Linux like Ubuntu or MacOS.

1. Install MySQL in your environment.

Download it from https://dev.mysql.com/downloads/mysql/ or in any other way. (I'm using the version 'MySQL Community Server 8.0.17'.)

When installing, it would give you a temporary password or let you set your own password for root. Whatever the password is, it's ok. We'll create another user(not root) for PapersFeed.

2. Log in to mysql as root, and create a database and an user.

In terminal, please type this and enter the password.
mysql -u root -p

By type this, create a DB with the name 'PapersFeed_DB'.
mysql> CREATE DATABASE PapersFeed_DB;

And please create user with this command.
mysql> CREATE USER 'PapersFeed'@'localhost' IDENTIFIED WITH mysql_native_password BY 'swpp2019team3';

You should give the user all privileges of 'PapersFeed_DB'.
mysql> GRANT ALL PRIVILEGES ON PapersFeed_DB.* to 'PapersFeed'@'localhost';

3. Set 'mysql_native_password' as 'default-authentication-plugin'.

This process is related with the issue 'https://stackoverflow.com/questions/50469587/django-db-utils-operationalerror-2059-authentication-plugin-caching-sha2-pas'.

Please type 'exit' to exit from mysql.

On terminal, please type this to check your setting.
mysqld --verbose --help

Maybe you can find 'default-authentication-plugin' is 'caching_sha2_password'.
You can change this by modifying or creating the file '~/.my.cnf'.(You can find some other choices by typing mysql --help | grep my.cnf.)
In the file, please make it include below two lines.

[mysqld]
default-authentication-plugin=mysql_native_password

4. Install mysqlclient.

For Django to deal with MySQL, mysqlclient is needed.
I specified it with the version in 'requirements.txt'. Please install it in some ways like pip3 install -r /path/to/requirements.txt.

5. (Optional) Set DYLD_LIBRARY_PATH in your env.

In my case, even though I installed mysqlclient, when I ran 'manage.py', it couldn't find the module. Then you should set DYLD_LIBRARY_PATH in your shell setting file like '~/.bashrc', '~/.zshrc', etc.

Please make it include this.
export DYLD_LIBRARY_PATH="/usr/local/mysql/lib/:$DYLD_LIBRARY_PATH"

6. Migrate!

In the 'backend' dir, please type this to make migrations and migrate.

python3 manage.py makemigrations
python3 manage.py migrate

Please let me know immediately when you encounter some problems related with this.

Minor change

I added a '.gitignore' file in the 'backend' dir.

And I delete 'lazy-object-proxy' from 'requirements.txt' because it is specified with the version '0.0.0', which is strange.(@abcthing)

Also, I find ApiError defined in 'utils.py' of the 'application' dir is always called when I run 'manage.py'.(@abcthing) To avoid confusion, I modified the error message to 'API ERROR(fixme)'.

@davin111 davin111 self-assigned this Oct 13, 2019
@abcthing abcthing merged commit af4277c into master Oct 16, 2019
@davin111 davin111 added the good first issue Good for newcomers label Oct 16, 2019
@davin111 davin111 deleted the mysqldb branch October 16, 2019 14:37
@davin111 davin111 added the backend Related with backend works label Nov 2, 2019
@davin111 davin111 added good reference This issue or PR is good to know and removed good first issue Good for newcomers labels Nov 22, 2019
@davin111
Copy link
Contributor Author

You'd better use GRANT ALL PRIVILEGES ON *.* to 'PapersFeed'@'localhost'; because the 'PapersFeed' user should create and drop databases while testing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backend Related with backend works good reference This issue or PR is good to know
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Connecting MySQL with Django
2 participants