Install mysql8 on centOS 9 and integrating with Django

 Description

Our application data (live or prod app) will be loaded into RDBMS(Mysql).

So we are integrating mysql with django

 


 

Install mysql8 on centOS 9

This is available in the appSteam repo

[root@localhost ~]# sudo dnf install mysql mysql-server -y
Last metadata expiration check: 0:00:21 ago on Fri 08 Apr 2022 07:27:09 AM IST.
Dependencies resolved.
===================================================================================================================================================================================================================
 Package                                                        Architecture                               Version                                             Repository                                     Size
===================================================================================================================================================================================================================
Installing:
 mysql                                                          x86_64                                     8.0.28-1.el9                                        appstream                                     2.6 M
 mysql-server                                                   x86_64                                     8.0.28-1.el9                                        appstream                                      16 M
Installing dependencies:
 mariadb-connector-c-config                                     noarch                                     3.1.13-3.el9                                        appstream                                      11 k
 mecab                                                          x86_64                                     0.996-3.el9.3                                       appstream                                     356 k
 mysql-common                                                   x86_64                                     8.0.28-1.el9                                        appstream                                      75 k
 mysql-errmsg                                                   x86_64                                     8.0.28-1.el9                                        appstream                                     473 k
 mysql-selinux                                                  noarch                                     1.0.4-2.el9                                         appstream                                      36 k
 protobuf-lite                                                  x86_64                                     3.14.0-13.el9                                       appstream                                     232 k

Transaction Summary
===================================================================================================================================================================================================================
Install  8 Packages

Total download size: 20 M
Installed size: 176 M
Downloading Packages:
(1/8): mariadb-connector-c-config-3.1.13-3.el9.noarch.rpm                                                                                                                          1.8 kB/s |  11 kB     00:06    
(2/8): mecab-0.996-3.el9.3.x86_64.rpm                                                                                                                                               52 kB/s | 356 kB     00:06    
(3/8): mysql-common-8.0.28-1.el9.x86_64.rpm                                                                                                                                        102 kB/s |  75 kB     00:00    
(4/8): mysql-selinux-1.0.4-2.el9.noarch.rpm                                                                                                                                        120 kB/s |  36 kB     00:00    
(5/8): mysql-errmsg-8.0.28-1.el9.x86_64.rpm                                                                                                                                        350 kB/s | 473 kB     00:01    
(6/8): protobuf-lite-3.14.0-13.el9.x86_64.rpm                                                                                                                                      332 kB/s | 232 kB     00:00    
(7/8): mysql-8.0.28-1.el9.x86_64.rpm                                                                                                                                               210 kB/s | 2.6 MB     00:12    
(8/8): mysql-server-8.0.28-1.el9.x86_64.rpm                                                                                                                                        1.4 MB/s |  16 MB     00:12    
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Total                                                                                                                                                                              790 kB/s |  20 MB     00:26     
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
  Preparing        :                                                                                                                                                                                           1/1
  Installing       : mariadb-connector-c-config-3.1.13-3.el9.noarch                                                                                                                                            1/8
  Installing       : mysql-common-8.0.28-1.el9.x86_64                                                                                                                                                          2/8
  Installing       : mysql-8.0.28-1.el9.x86_64                                                                                                                                                                 3/8
  Installing       : mysql-errmsg-8.0.28-1.el9.x86_64                                                                                                                                                          4/8
  Installing       : protobuf-lite-3.14.0-13.el9.x86_64                                                                                                                                                        5/8
  Running scriptlet: mysql-selinux-1.0.4-2.el9.noarch                                                                                                                                                          6/8
  Installing       : mysql-selinux-1.0.4-2.el9.noarch                                                                                                                                                          6/8
  Running scriptlet: mysql-selinux-1.0.4-2.el9.noarch                                                                                                                                                          6/8
libsemanage.semanage_direct_install_info: Overriding mysql module at lower priority 100 with module at priority 200.

  Installing       : mecab-0.996-3.el9.3.x86_64                                                                                                                                                                7/8
  Running scriptlet: mysql-server-8.0.28-1.el9.x86_64                                                                                                                                                          8/8
  Installing       : mysql-server-8.0.28-1.el9.x86_64                                                                                                                                                          8/8
  Running scriptlet: mysql-server-8.0.28-1.el9.x86_64                                                                                                                                                          8/8
  Running scriptlet: mysql-selinux-1.0.4-2.el9.noarch                                                                                                                                                          8/8
  Running scriptlet: mysql-server-8.0.28-1.el9.x86_64                                                                                                                                                          8/8
  Verifying        : mariadb-connector-c-config-3.1.13-3.el9.noarch                                                                                                                                            1/8
  Verifying        : mecab-0.996-3.el9.3.x86_64                                                                                                                                                                2/8
  Verifying        : mysql-8.0.28-1.el9.x86_64                                                                                                                                                                 3/8
  Verifying        : mysql-common-8.0.28-1.el9.x86_64                                                                                                                                                          4/8
  Verifying        : mysql-errmsg-8.0.28-1.el9.x86_64                                                                                                                                                          5/8
  Verifying        : mysql-selinux-1.0.4-2.el9.noarch                                                                                                                                                          6/8
  Verifying        : mysql-server-8.0.28-1.el9.x86_64                                                                                                                                                          7/8
  Verifying        : protobuf-lite-3.14.0-13.el9.x86_64                                                                                                                                                        8/8

Installed:
  mariadb-connector-c-config-3.1.13-3.el9.noarch mecab-0.996-3.el9.3.x86_64         mysql-8.0.28-1.el9.x86_64 mysql-common-8.0.28-1.el9.x86_64 mysql-errmsg-8.0.28-1.el9.x86_64 mysql-selinux-1.0.4-2.el9.noarch
  mysql-server-8.0.28-1.el9.x86_64               protobuf-lite-3.14.0-13.el9.x86_64

Complete!

Installing mysql devel

devel package is available in crb repo - https://centos.pkgs.org/9-stream/centos-crb-aarch64/mysql-devel-8.0.22-7.el9.aarch64.rpm.html




[root@localhost ~]# dnf --enablerepo=crb install mysql-devel
CentOS Stream 9 - CRB                                                                                                                                                              218 kB/s | 4.1 MB     00:19    
Last metadata expiration check: 0:00:01 ago on Fri 08 Apr 2022 07:38:11 AM IST.
Dependencies resolved.
===================================================================================================================================================================================================================
 Package                                               Architecture                                   Version                                              Repository                                         Size
===================================================================================================================================================================================================================
Installing:
 mysql-devel                                           x86_64                                         8.0.28-1.el9                                         crb                                                94 k
Installing dependencies:
 libzstd-devel                                         x86_64                                         1.5.1-2.el9                                          appstream                                          47 k
 mysql-libs                                            x86_64                                         8.0.28-1.el9                                         crb                                               1.2 M

Transaction Summary
===================================================================================================================================================================================================================
Install  3 Packages

Total download size: 1.4 M
Installed size: 7.0 M
Is this ok [y/N]: y
Downloading Packages:
(1/3): libzstd-devel-1.5.1-2.el9.x86_64.rpm                                                                                                                                        8.3 kB/s |  47 kB     00:05    
(2/3): mysql-devel-8.0.28-1.el9.x86_64.rpm                                                                                                                                          15 kB/s |  94 kB     00:06    
(3/3): mysql-libs-8.0.28-1.el9.x86_64.rpm                                                                                                                                          139 kB/s | 1.2 MB     00:09    
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Total                                                                                                                                                                               63 kB/s | 1.4 MB     00:21     
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
  Preparing        :                                                                                                                                                                                           1/1
  Installing       : mysql-libs-8.0.28-1.el9.x86_64                                                                                                                                                            1/3
  Installing       : libzstd-devel-1.5.1-2.el9.x86_64                                                                                                                                                          2/3
  Installing       : mysql-devel-8.0.28-1.el9.x86_64                                                                                                                                                           3/3
  Running scriptlet: mysql-devel-8.0.28-1.el9.x86_64                                                                                                                                                           3/3
  Verifying        : libzstd-devel-1.5.1-2.el9.x86_64                                                                                                                                                          1/3
  Verifying        : mysql-devel-8.0.28-1.el9.x86_64                                                                                                                                                           2/3
  Verifying        : mysql-libs-8.0.28-1.el9.x86_64                                                                                                                                                            3/3

Installed:
  libzstd-devel-1.5.1-2.el9.x86_64                                       mysql-devel-8.0.28-1.el9.x86_64                                       mysql-libs-8.0.28-1.el9.x86_64                                      

Complete!


Start and Enable the Mysql service on boot


[root@localhost ~]# sudo systemctl start mysqld.service
[root@localhost ~]# sudo systemctl status mysqld
● mysqld.service - MySQL 8.0 database server
     Loaded: loaded (/usr/lib/systemd/system/mysqld.service; disabled; vendor preset: disabled)
     Active: active (running) since Fri 2022-04-08 07:48:47 IST; 19s ago
    Process: 5180 ExecStartPre=/usr/libexec/mysql-check-socket (code=exited, status=0/SUCCESS)
    Process: 5202 ExecStartPre=/usr/libexec/mysql-prepare-db-dir mysqld.service (code=exited, status=0/SUCCESS)
   Main PID: 5236 (mysqld)
     Status: "Server is operational"
      Tasks: 38 (limit: 100343)
     Memory: 383.7M
        CPU: 544ms
     CGroup: /system.slice/mysqld.service
             └─5236 /usr/libexec/mysqld --basedir=/usr

Apr 08 07:48:46 localhost.localdomain systemd[1]: Starting MySQL 8.0 database server...
Apr 08 07:48:47 localhost.localdomain systemd[1]: Started MySQL 8.0 database server.
[root@localhost ~]# sudo systemctl enable mysqld
Created symlink /etc/systemd/system/multi-user.target.wants/mysqld.service → /usr/lib/systemd/system/mysqld.service.
[root@localhost ~]# sudo mysql_secure_installation

Securing the MySQL server deployment.

Enter password for user root:
The 'validate_password' component is installed on the server.
The subsequent steps will run with the existing configuration
of the component.
Using existing password for root.

Estimated strength of the password: 100
Change the password for root ? ((Press y|Y for Yes, any other key for No) : N

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


Install Mysql Client on django


(dj-env) [dj_adm@localhost mysite]$ pip install mysqlclient
Collecting mysqlclient
  Using cached mysqlclient-2.1.0.tar.gz (87 kB)
  Preparing metadata (setup.py) ... done
Building wheels for collected packages: mysqlclient
  Building wheel for mysqlclient (setup.py) ... done
  Created wheel for mysqlclient: filename=mysqlclient-2.1.0-cp39-cp39-linux_x86_64.whl size=104059 sha256=1cf4ac2cfdf6febe1ea6a50424b497bb773c593a96e3db05703d8e9be9612057
  Stored in directory: /home/dj_adm/.cache/pip/wheels/87/b7/2c/de7dba0acf2f491d1bbc8c56b7320041ef58621ea75b47bdc9
Successfully built mysqlclient
Installing collected packages: mysqlclient
Successfully installed mysqlclient-2.1.0

[dj_adm@localhost ~]$ source ~/virtualenvs/dj-env/bin/activate
(dj-env) [dj_adm@localhost ~]$
(dj-env) [dj_adm@localhost ~]$
(dj-env) [dj_adm@localhost ~]$
(dj-env) [dj_adm@localhost ~]$ ls -ltr
total 0
drwxr-xr-x. 3 dj_adm dj_adm 20 Apr  7 13:05 virtualenvs
drwxr-xr-x. 4 dj_adm dj_adm 49 Apr  7 13:21 dj-practice
(dj-env) [dj_adm@localhost ~]$ cd dj-practice/
(dj-env) [dj_adm@localhost dj-practice]$ pwd
/home/dj_adm/dj-practice
(dj-env) [dj_adm@localhost dj-practice]$ cd mysite/
(dj-env) [dj_adm@localhost mysite]$

Create the dedicative admin user for django application

[root@localhost ~]# mysql -u root -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 11
Server version: 8.0.28 Source distribution

Copyright (c) 2000, 2022, Oracle and/or its affiliates.

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> SELECT user,authentication_string,plugin,host FROM mysql.user;
+------------------+------------------------------------------------------------------------+-----------------------+-----------+
| user             | authentication_string                                                  | plugin                | host      |
+------------------+------------------------------------------------------------------------+-----------------------+-----------+
| mysql.infoschema | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED | caching_sha2_password | localhost |
| mysql.session    | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED | caching_sha2_password | localhost |
| mysql.sys        | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED | caching_sha2_password | localhost |
WtqYnsJcCmR2S | caching_sha2_password | localhost |
+------------------+------------------------------------------------------------------------+-----------------------+-----------+
4 rows in set (0.00 sec)

mysql> CREATE USER 'dj_adm'@'localhost' IDENTIFIED BY '*******';
Query OK, 0 rows affected (0.03 sec)

mysql> create database prod_app character set utf8;
Query OK, 1 row affected, 1 warning (0.02 sec)

mysql>  GRANT ALL PRIVILEGES ON prod_app.* TO 'dj_adm'@'localhost'
    -> ;

Query OK, 0 rows affected (0.02 sec)

mysql> SELECT user,authentication_string,plugin,host FROM mysql.user;
+------------------+------------------------------------------------------------------------+-----------------------+-----------+
| user             | authentication_string                                                  | plugin                | host      |
+------------------+------------------------------------------------------------------------+-----------------------+-----------+
| dj_adm           | $A$005$ | caching_sha2_password | localhost |
| mysql.infoschema | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED | caching_sha2_password | localhost |
| mysql.session    | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED | caching_sha2_password | localhost |
| mysql.sys        | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED | caching_sha2_password | localhost |
WtqYnsJcCmR2S | caching_sha2_password | localhost |
+------------------+------------------------------------------------------------------------+-----------------------+-----------+
5 rows in set (0.00 sec) 


changes in settings.py


(dj-env) [dj_adm@localhost mysite]$ cat mysite/settings.py

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'prod_app',
        'USER': 'dj_adm',
#        'PASSWORD': '',
        'HOST': 'localhost',
        'PORT': '3306',
        'OPTIONS': {
            'init_command': "SET sql_mode='STRICT_TRANS_TABLES'",
            'read_default_file': '/home/dj_adm/.dj-conf/my.cnf',
        },
    }
}

[dj_adm@localhost .dj-conf]$ cat my.cnf
# my.cnf
[client]
database = prod_app
user = dj_adm@localhost
password = *******
host = localhost
port = 3306
default-character-set = utf8
[dj_adm@localhost .dj-conf]$


Migrate the models to mysql


(dj-env) [dj_adm@localhost mysite]$ python manage.py migrate  
Operations to perform:
  Apply all migrations: admin, auth, contenttypes, sessions
Running migrations:
  Applying contenttypes.0001_initial... OK
  Applying auth.0001_initial... OK
  Applying admin.0001_initial... OK
  Applying admin.0002_logentry_remove_auto_add... OK
  Applying admin.0003_logentry_add_action_flag_choices... OK
  Applying contenttypes.0002_remove_content_type_name... OK
  Applying auth.0002_alter_permission_name_max_length... OK
  Applying auth.0003_alter_user_email_max_length... OK
  Applying auth.0004_alter_user_username_opts... OK
  Applying auth.0005_alter_user_last_login_null... OK
  Applying auth.0006_require_contenttypes_0002... OK
  Applying auth.0007_alter_validators_add_error_messages... OK
  Applying auth.0008_alter_user_username_max_length... OK
  Applying auth.0009_alter_user_last_name_max_length... OK
  Applying auth.0010_alter_group_name_max_length... OK
  Applying auth.0011_update_proxy_permissions... OK
  Applying auth.0012_alter_user_first_name_max_length... OK
  Applying sessions.0001_initial... OK


[dj_adm@localhost .dj-conf]$ mysql -u dj_adm -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 17
Server version: 8.0.28 Source distribution

Copyright (c) 2000, 2022, Oracle and/or its affiliates.

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> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| prod_app           |
+--------------------+
2 rows in set (0.00 sec)

mysql> use prod_app;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> show tables;
+----------------------------+
| Tables_in_prod_app         |
+----------------------------+
| auth_group                 |
| auth_group_permissions     |
| auth_permission            |
| auth_user                  |
| auth_user_groups           |
| auth_user_user_permissions |
| django_admin_log           |
| django_content_type        |
| django_migrations          |
| django_session             |
+----------------------------+
10 rows in set (0.00 sec)

mysql>

Comments

Popular posts from this blog

Django Server setup with Apache with mod_wsgi in Centos 9

Mysql Installation on CentOS 9