MySql Archives - TEKSpace Blog https://blog.tekspace.io/tag/mysql/ Tech tutorials for Linux, Kubernetes, PowerShell, and Azure Wed, 30 Aug 2023 14:56:34 +0000 en-US hourly 1 https://wordpress.org/?v=6.7.1 https://blog.tekspace.io/wp-content/uploads/2023/09/cropped-Tekspace-logo-icon-32x32.png MySql Archives - TEKSpace Blog https://blog.tekspace.io/tag/mysql/ 32 32 Reset MySql 8.0 root password on Windows https://blog.tekspace.io/reset-mysql-8-0-root-password-on-windows/ https://blog.tekspace.io/reset-mysql-8-0-root-password-on-windows/#respond Thu, 01 Oct 2020 13:30:16 +0000 https://blog.tekspace.io/index.php/2020/10/01/reset-mysql-8-0-root-password-on-windows/ If you forgot your root password and need to reset it for your MySql 8 server. Follow below guide. Stop MySQL80 service on Windows This will stop the MySQL80 service. If you wish to stop service from the services console, you can go to Start menu, select Control Panel, then Administrative Tools, then Services. Okay,

The post Reset MySql 8.0 root password on Windows appeared first on TEKSpace Blog.

]]>
If you forgot your root password and need to reset it for your MySql 8 server. Follow below guide.

Stop MySQL80 service on Windows

  1. Open PowerShell as Administrator and execute the below command.
get-service -Name mysql* | Stop-Service

This will stop the MySQL80 service. If you wish to stop service from the services console, you can go to Start menu, select Control Panel, then Administrative Tools, then Services.

Okay, now that we have stopped the service. Let’s proceed with root password reset.

MySQL Root Password Reset

  1. Create a file called reset.txt on your computer at c:\reset.txt.
  2. Copy and paste below to reset.txt file and replace password with your own password and save.
ALTER USER 'root'@'localhost' IDENTIFIED BY 'password';
  1. Open Command promote and go to bin path
cd C:\Program Files\MySQL\MySQL Server 8.0\bin
  1. Execute below command to reset password:
mysqld.exe --defaults-file="C:\\ProgramData\\MySQL\\MySQL Server 8.0\\my.ini" --datadir="C:\\ProgramData\\MySQL\\MySQL Server 8.0\\Data" --init-file=c:\\reset.txt --console

Your output should look something like this:

C:\Program Files\MySQL\MySQL Server 8.0\bin>2020-10-01T13:06:32.497917Z 0 [System] [MY-010910] [Server] C:\Program Files\MySQL\MySQL Server 8.0\bin\mysqld.exe: Shutdown complete (mysqld 8.0.21)  MySQL Community Server - GPL.
mysqld.exe --defaults-file="C:\\ProgramData\\MySQL\\MySQL Server 8.0\\my.ini" --datadir="C:\\ProgramData\\MySQL\\MySQL Server 8.0\\Data" --init-file=c:\\reset.txt --console
2020-10-01T13:06:36.133710Z 0 [Warning] [MY-000081] [Server] option 'read_buffer_size': unsigned value 614 adjusted to 8192.
2020-10-01T13:06:36.133802Z 0 [Warning] [MY-010915] [Server] 'NO_ZERO_DATE', 'NO_ZERO_IN_DATE' and 'ERROR_FOR_DIVISION_BY_ZERO' sql modes should be used with strict mode. They will be merged with strict mode in a future release.
2020-10-01T13:06:36.135314Z 0 [System] [MY-010116] [Server] C:\Program Files\MySQL\MySQL Server 8.0\bin\mysqld.exe (mysqld 8.0.21) starting as process 9284
2020-10-01T13:06:36.149595Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
2020-10-01T13:06:36.942139Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
2020-10-01T13:06:37.269023Z 0 [System] [MY-011323] [Server] X Plugin ready for connections. Bind-address: '::' port: 33060
2020-10-01T13:06:37.392127Z 0 [Warning] [MY-010068] [Server] CA certificate ca.pem is self signed.
2020-10-01T13:06:37.392634Z 0 [System] [MY-013602] [Server] Channel mysql_main configured to support TLS. Encrypted connections are now supported for this channel.
2020-10-01T13:06:37.486710Z 0 [System] [MY-010931] [Server] C:\Program Files\MySQL\MySQL Server 8.0\bin\mysqld.exe: ready for connections. Version: '8.0.21'  socket: ''  port: 3306  MySQL Community Server - GPL.
2020-10-01T13:06:53.165055Z 9 [Warning] [MY-013360] [Server] Plugin sha256_password reported: ''sha256_password' is deprecated and will be removed in a future release. Please use caching_sha2_password instead'
2020-10-01T13:06:59.761228Z 0 [System] [MY-013105] [Server] C:\Program Files\MySQL\MySQL Server 8.0\bin\mysqld.exe: Normal shutdown.

Once the password reset is completed. You can go ahead and start the MySQL80 service.

get-service -Name mysql* | Start-Service

If you wish to stop service from services console, you can go to Start menu, select Control Panel, then Administrative Tools, then Services and start MySql80 service.

The post Reset MySql 8.0 root password on Windows appeared first on TEKSpace Blog.

]]>
https://blog.tekspace.io/reset-mysql-8-0-root-password-on-windows/feed/ 0
How to encrypt and decrypt files using openssl https://blog.tekspace.io/how-to-encrypt-and-decrypt-files-using-openssl/ https://blog.tekspace.io/how-to-encrypt-and-decrypt-files-using-openssl/#respond Wed, 30 Sep 2020 22:09:57 +0000 https://blog.tekspace.io/index.php/2020/09/30/how-to-encrypt-and-decrypt-files-using-openssl/ In this tutorial we will go over how to encrypt a text file that we can store in public storage without any security concerns. Suppose you are running an application in a cloud platform, and you are running daily backup of SQL files and want to store it securely in block storage somewhere in the

The post How to encrypt and decrypt files using openssl appeared first on TEKSpace Blog.

]]>
In this tutorial we will go over how to encrypt a text file that we can store in public storage without any security concerns. Suppose you are running an application in a cloud platform, and you are running daily backup of SQL files and want to store it securely in block storage somewhere in the cloud environment. In that case, using certificates to encrypt the file is very useful and worry free.

To get started, I am using a Linux operating system with OpenSSL.

Generating private and public certificate files

The below command will create 2 files on your Linux file systems.
example.priv.pem – This is your private key. You must store this somewhere secure.
example.pub.pem – This is your public key. That we will use to encrypt files with.

NOTE: The below command will create a private key with a password. I highly encourage using password to keep your private key secure. If you do not wish to use a password. Add -nodes to the below command, and it will create a private key without password.

openssl req -x509 -newkey rsa:4096 -keyout example.priv.pem -out example.pub.pem

Interactive view

Generating a 4096 bit RSA private key
.......................................................................................................................................................................................++
.......................................................................................++
writing new private key to 'example.priv.pem'
Enter PEM pass phrase: <YOUR SECRET PASSWORD>
Verifying - Enter PEM pass phrase: <YOUR SECRET PASSWORD>
-----
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [XX]:US
State or Province Name (full name) []:Texas
Locality Name (eg, city) [Default City]:Houston
Organization Name (eg, company) [Default Company Ltd]:Example
Organizational Unit Name (eg, section) []:IT
Common Name (eg, your name or your server's hostname) []:example
Email Address []:JohnSmith@example.com

Encrypt file

In the below example, I will encrypt a database.sql file. You can encrypt any file you desire. It can be text, pdf, logs, etc.

openssl smime -encrypt -binary -text -aes256 -in database.sql -out database.sql.enc -outform DER example.pub.pem

Now when you open your database.sql.enc file. It should look something like as shown below:

0<82>^C ^F      *<86>H<86>÷^M^A^G^C <82>^Bú0<82>^Bö^B^A^@1<82>^B±0<82>^B­^B^A^@0<81><94>0<81><86>1^K0   ^F^CU^D^F^S^BUS1^N0^L^F^CU^D^H^L^ETexas1^P0^N^F^CU^D^G^L^GHouston1^P0^N^F^CU^D
^L^GExample1^K0 ^F^CU^D^K^L^BIT1^P0^N^F^CU^D^C^L^Gexample1$0"^F *<86>H<86>÷^M^A ^A^V^UJohnSmith@example.com^B   ^@ñ<99>%<93>dÑñ40^M^F   *<86>H<86>÷^M^A^A^A^E^@^D<82>^B^@^]<90>^\c1TÊUí»?<8b>b½Ü|P^X^F<87>nñfÅ" á^Mîma<9d>ô<94>½>³/Æ<82> >/s,[¸<8d>J<95>^Q<8b>[¯J^@WÜË{ü^Nú@½<99>5XJ^YA æË³Æ ÝN:÷ïìÒúÇe µ¹<99>^R^L^DX´<8a>^H2 Ùñ}Ú^E¥ç´Gë²Ô=JþÁ¾V^Q1Óq+Z£ñÔe´ä<95>^D|.¯Vq<93>Ó¸|û)i3<"^[Q¤\¿><98>ú<95>ö^\W^\^\í~)&<92>¡ÎrÈ<82>Æ·^?XäHs;'ý]<9b>Ü ¥¤<92>>\=¡5     <8a>ß9ßl<92>^T<8a>f"^A}=¸V^S^Qø=^YG÷WM@YØ|yxºÄ<95>\¢°Pbsn,nùãf¾>×Ó®¨ü4:RÁq<97>ÏKïÇ^X.]1^Z<8a>^R^KÜi<96><82>­î^HòË%æ¸[eò7<89>,vùÎ8Áb'YõòÔCÝå<9e>hA7èn;ÑÚCê^@ìE÷<9a>¨8<81>Tn«4Ñ^\ª#^?ÁÍ^V^Bcj"£±tÙ<99>^Q^T<87>³§^V °¿z^V^Z^E<9d>%x|K<90>ël¼Á<8b>·»<96><9c>s¢<85>ÙvÚß<92>üs^G^XÚR«REÌ[3³û<9c>®9²´Zr¼^B<93>^P>(^Zü?^O<91>;RKÚ;«¡`,$+^C.C^NÇV1@^?¾^¡<87>^?   IZè6MûyõÉð^B­¦×{ó<9d>^Nb^^D^\¤¹Õý^LÒ^[^B9<96>á^^ÜæÍ<95>µb¿HỸC^D[<8b>^Q<9a>;6˹Ãä<8d>º<81>p-<9d>îÄIq^Y£^YÌÑ¿<99>^_'*3ìòèÿ0<^F  *<86>H<86>÷^M^A^G^A0^]^F        `<86>H^Ae^C^D^A*^D^P<9a><84>¥^Qn<84>Á<90><95>Ûjá^A÷íT<80>^P^GA^@4¤^Q#jÉO^X<94>ñõ<9d>§

Decrypt file

Below command will decrypt previously encrypted file. In this example, we will decrypt database.sql.enc

openssl smime -decrypt -in database.sql.enc -binary -inform DEM -inkey example.priv.pem -out database-unencrypted.sql

You will be promoted for password after you execute above command.

Enter pass phrase for example.priv.pem:

Once you enter right password, you should have be able to read database-unencrypted.sql file.

Using key and crt files to encrypt and decrypt files

openssl req -newkey rsa:4096 -x509 -sha256 -days 3650 -out example.crt -keyout example.key

Above command will generate new .crt and .key files. Now we can reference these files to encrypt and decrypt files.

Encrypting file

openssl smime -encrypt -binary -text -aes256 -in database.sql -out database.sql.enc -outform DER example.crt

Decrypting file

openssl smime -decrypt -in database.sql.enc -binary -inform DEM -inkey example.key -out database-unencrypted.sql

The post How to encrypt and decrypt files using openssl appeared first on TEKSpace Blog.

]]>
https://blog.tekspace.io/how-to-encrypt-and-decrypt-files-using-openssl/feed/ 0
How to create new MySQL user and assign database https://blog.tekspace.io/how-to-create-new-mysql-user-and-assign-database/ https://blog.tekspace.io/how-to-create-new-mysql-user-and-assign-database/#respond Mon, 21 Sep 2020 18:42:17 +0000 https://blog.tekspace.io/index.php/2020/09/21/how-to-create-new-mysql-user-and-assign-database/ To create a new user in MySQL follow the below steps: Create new MySQL user Once the user is created, you will need to set permissions to grant access to the database. Here I will first create a testdb to create a new database to set permissions. Create new database Assign permissions to database Update

The post How to create new MySQL user and assign database appeared first on TEKSpace Blog.

]]>
To create a new user in MySQL follow the below steps:

Create new MySQL user

CREATE USER 'demo'@'localhost' IDENTIFIED BY 'dem0123';

Once the user is created, you will need to set permissions to grant access to the database. Here I will first create a testdb to create a new database to set permissions.

Create new database
CREATE DATABASE testdb;
Assign permissions to database
GRANT ALL PRIVILEGES ON testdb.* TO 'demo'@'localhost';

Update changes by executing the below command:

FLUSH PRIVILEGES;

Drop user

DROP USER 'demo'@'localhost';

Validate permissions

SHOW GRANTS FOR 'demo'@'localhost';

Output:

+----------------------------------------------------------------+
| Grants for demo@localhost                                |
+----------------------------------------------------------------+
| GRANT USAGE ON *.* TO `demo`@`localhost`                 |
| GRANT ALL PRIVILEGES ON `testdb`.* TO `demo`@`localhost` |
+----------------------------------------------------------------+
2 rows in set (0.00 sec)

Allowing remote access for MySQL 8 server

In MySQL 8 on Centos 8 server, edit /etc/my.cnf file and add the following line at the end and save:

bind-address            = 0.0.0.0

You can reference this tutorial here for any further details.

Create user

CREATE USER 'demo'@'10.1.1.%' IDENTIFIED BY 'password';

Grant privileges

GRANT ALL ON testdb.* TO 'demo'@'10.1.1.%';
FLUSH PRIVILEGES;

The post How to create new MySQL user and assign database appeared first on TEKSpace Blog.

]]>
https://blog.tekspace.io/how-to-create-new-mysql-user-and-assign-database/feed/ 0
How to install MySQL 8 on CentOS 8 https://blog.tekspace.io/how-to-install-mysql-8-on-centos-8/ https://blog.tekspace.io/how-to-install-mysql-8-on-centos-8/#respond Sat, 19 Sep 2020 16:58:02 +0000 https://blog.tekspace.io/index.php/2020/09/19/how-to-install-mysql-8-on-centos-8/ Download rpm package and configure yum repository Disable mysql5.7: Enable mysql8.0: Verify: Disabling default MySQL modules Installation Service start stop start: status check: Changing temp password after installation The above output will display the default password Z#5m,9wsmlw in the console. Changing password by following below setup command Interactive setup: Connecting to MySQL server

The post How to install MySQL 8 on CentOS 8 appeared first on TEKSpace Blog.

]]>
Download rpm package and configure yum repository
wget https://dev.mysql.com/get/mysql80-community-release-el8-1.noarch.rpm
sudo yum localinstall mysql80-community-release-el8-1.noarch.rpm
sudo yum repolist enabled | grep "mysql.*-community.*"

output:
mysql-connectors-community              MySQL Connectors Community
mysql-tools-community                   MySQL Tools Community
mysql80-community                       MySQL 8.0 Community Server
sudo yum repolist all | grep mysql

Disable mysql5.7:

sudo yum-config-manager --disable mysql57-community

Enable mysql8.0:

sudo yum-config-manager --enable mysql80-community

Verify:

sudo yum repolist enabled | grep mysql

Disabling default MySQL modules

sudo yum module disable mysql

Installation

sudo yum install mysql-community-server

Service start stop

start:

sudo service mysqld start

status check:

sudo service mysqld status

Changing temp password after installation

sudo grep 'temporary password' /var/log/mysqld.log

output:
2020-09-19T16:33:12.280169Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: .Z#5m,9wsmlw

The above output will display the default password Z#5m,9wsmlw in the console.

Changing password by following below setup command

sudo mysql_secure_installation

Interactive setup:

Securing the MySQL server deployment.

Enter password for user root: Enter New Root Password

VALIDATE PASSWORD PLUGIN can be used to test passwords
and improve security. It checks the strength of password
and allows the users to set only those passwords which are
secure enough. Would you like to setup VALIDATE PASSWORD plugin?

Press y|Y for Yes, any other key for No: y

There are three levels of password validation policy:

LOW    Length >= 8
MEDIUM Length >= 8, numeric, mixed case, and special characters
STRONG Length >= 8, numeric, mixed case, special characters and dictionary                  file

Please enter 0 = LOW, 1 = MEDIUM and 2 = STRONG: 2
Using existing password for root.

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

New password: Set New MySQL Password

Re-enter new password: Re-enter New MySQL Password

Estimated strength of the password: 100 
Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) : y
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! 

Connecting to MySQL server

mysql -u root -p

The post How to install MySQL 8 on CentOS 8 appeared first on TEKSpace Blog.

]]>
https://blog.tekspace.io/how-to-install-mysql-8-on-centos-8/feed/ 0
Managing MySQL database using PHPMyAdmin in Kubernetes https://blog.tekspace.io/managing-mysql-database-using-php/ https://blog.tekspace.io/managing-mysql-database-using-php/#respond Tue, 15 Sep 2020 01:35:49 +0000 https://blog.tekspace.io/index.php/2020/09/15/managing-mysql-database-using-php/ Make sure you are familiar with connecting to a Kubernetes cluster, have the Nginx ingress controller configured with a certificate manager, and have a MySQL database pod deployed. Only then, can you proceed. Follow this guide if you do not have MySQL deployed. Follow this guide to set up Nginx ingress and cert manager. PhpMyAdmin

The post Managing MySQL database using PHPMyAdmin in Kubernetes appeared first on TEKSpace Blog.

]]>

Make sure you are familiar with connecting to a Kubernetes cluster, have the Nginx ingress controller configured with a certificate manager, and have a MySQL database pod deployed. Only then, can you proceed. Follow this guide if you do not have MySQL deployed. Follow this guide to set up Nginx ingress and cert manager.

PhpMyAdmin is a popular open source tool to manage MySQL database server. Learn how to create a deployment and expose it as a service to access PhpMyAdmin from the internet using Nginx ingress controller.

  1. Create a deployment file called phpmyadmin-deployment.yaml and paste the following values:
apiVersion: apps/v1
kind: Deployment
metadata:
  name: phpmyadmin-deployment
  labels:
    app: phpmyadmin
spec:
  replicas: 1
  selector:
    matchLabels:
      app: phpmyadmin
  template:
    metadata:
      labels:
        app: phpmyadmin
    spec:
      containers:
        - name: phpmyadmin
          image: phpmyadmin/phpmyadmin
          ports:
            - containerPort: 80
          env:
            - name: PMA_HOST
              value: mysql-service
            - name: PMA_PORT
              value: "3306"
            - name: MYSQL_ROOT_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: mysql-secrets
                  key: ROOT_PASSWORD

NOTE: ROOT_PASSWORD value will be consumed from Kubernetes secrets. If you want to learn more about Kubernetes secrets. Follow this guide.

  1. Execute the below command to create a new deployment:
kubectl apply -f phpmyadmin-deployment.yaml

Output:

deployment.apps/phpmyadmin-deployment created

Exposing PhpMyAdmin via Services

  1. Create new file called phpmyadmin-service.yaml and paste the following values:
apiVersion: v1
kind: Service
metadata:
  name: phpmyadmin-service
spec:
  type: NodePort
  selector:
    app: phpmyadmin
  ports:
  - protocol: TCP
    port: 80
    targetPort: 80
  1. Execute the below command to create service:
kubectl apply -f phpmyadmin-service.yaml

Output:

service/phpmyadmin-service created

Once you are done with the above configurations, it’s time to exposePhpMyAdminn service via internet.

I use DigitalOcean-managed Kubernetes. I manage my own DNS, and DigitalOcean automatically creates a load balancer for my Nginx ingress controller. Once again if you want to follow this guide, it will be very helpful.

Nginx Ingress configuration

  1. Create a new YAML file called phpmyadmin-ingress.yaml and paste the following values:
apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  name: echo-ingress
  annotations:
    cert-manager.io/cluster-issuer: "letsencrypt-prod"
spec:
  tls:
  - hosts:
    - phpmyadmin.example.com
    secretName: echo-tls
  rules:
  - host: mydemo.example.com
    http:
      paths:
      - backend:
          serviceName: phpmyadmin-service
          servicePort: 80
  1. Apply the changes:
kubectl apply -f mydemo-ingress.yaml

The post Managing MySQL database using PHPMyAdmin in Kubernetes appeared first on TEKSpace Blog.

]]>
https://blog.tekspace.io/managing-mysql-database-using-php/feed/ 0
How to Deploy MySQL database on Digital Ocean Managed Kubernetes Cluster https://blog.tekspace.io/how-to-deploy-mysql-database-on-digital-ocean-managed-kubernetes-cluster/ https://blog.tekspace.io/how-to-deploy-mysql-database-on-digital-ocean-managed-kubernetes-cluster/#respond Mon, 14 Sep 2020 18:40:34 +0000 https://blog.tekspace.io/index.php/2020/09/14/how-to-deploy-mysql-database-on-digital-ocean-managed-kubernetes-cluster/ NOTE: This tutorial assumes you know how to connect to a Kubernetes cluster. Create secrets to securely store MySQL credentials Output: Persistant volume and MySQL deployment Execute the below command to create persistent volume: Output: Execute the below command to deploy MySQL pod: Output: Exposing MySQL as a Service Output: Output:

The post How to Deploy MySQL database on Digital Ocean Managed Kubernetes Cluster appeared first on TEKSpace Blog.

]]>

NOTE: This tutorial assumes you know how to connect to a Kubernetes cluster.

Create secrets to securely store MySQL credentials

  1. Guide on how to create base 64 encoded values:

    Windows 10 guideLinux guide

  2. Create a new file called: mysql-secret.yaml and paste the value below.
    NOTE: You must first capture the value in base 64 by following the guide in step 1.
---
apiVersion: v1
kind: Secret
metadata:
  name: mysqldb-secrets
type: Opaque
data:
  ROOT_PASSWORD: c3VwZXItc2VjcmV0LXBhc3N3b3JkLWZvci1zcWw= 
  1. Execute the below command to create secrets:
kubectl apply -f mysql-secret.yaml

Output:

secret/mysqldb-secrets created
  1. To see if the secret is created, execute the below command:
kubectl get secret
NAME                  TYPE                                  DATA   AGE
default-token-jqq69   kubernetes.io/service-account-token   3      6h20m
echo-tls              kubernetes.io/tls                     2      5h19m
mysqldb-secrets       Opaque                                1      42s
  1. To see the description of the secret, execute the below command:
kubectl describe secret mysqldb-secrets
Name:         mysqldb-secrets
Namespace:    default
Labels:       <none>
Annotations:  <none>

Type:  Opaque

Data
====
ROOT_PASSWORD:  29 bytes

Persistant volume and MySQL deployment

  1. Create a persistent volume YAML file called: mysql-pvc.yaml and paste the following values:
apiVersion: v1
kind: PersistentVolume
metadata:
  name: mysql-pvc
  labels:
    type: local
spec:
  storageClassName: manual
  capacity:
    storage: 1Gi
  accessModes:
    - ReadWriteOnce
  hostPath:
    path: "/mnt/mysql-data"
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: mysql-pvc-claim
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 1Gi
  storageClassName: do-block-storage
  1. Create a new deployment YAML file called: mysql-deployment.yaml and paste the following values:
apiVersion: v1
kind: Service
metadata:
  name: mysql
spec:
  ports:
  - port: 3306
  selector:
    app: mysql
  clusterIP: None
---
apiVersion: apps/v1 # for versions before 1.9.0 use apps/v1beta2
kind: Deployment
metadata:
  name: mysql
spec:
  selector:
    matchLabels:
      app: mysql
  strategy:
    type: Recreate
  template:
    metadata:
      labels:
        app: mysql
    spec:
      containers:
      - image: mysql:5.6
        name: mysql
        env:
        - name: MYSQL_ROOT_PASSWORD
          valueFrom:
            secretKeyRef:
              name: mysqldb-secrets
              key: ROOT_PASSWORD
        ports:
        - containerPort: 3306
          name: mysql
        volumeMounts:
        - name: mysql-persistent-storage
          mountPath: /var/lib/mysql
      volumes:
      - name: mysql-persistent-storage
        persistentVolumeClaim:
          claimName: mysql-pvc-claim

Execute the below command to create persistent volume:

kubectl apply -f mysql-pvc.yaml

Output:

persistentvolume/mysql-pvc createdpersistentvolumeclaim/mysql-pvc-claim created

Execute the below command to deploy MySQL pod:

kubectl apply -f mysql-deployment.yaml

Output:

service/mysql created

Exposing MySQL as a Service

  1. Create a file called mysql-service.yaml and paste the following values:
apiVersion: v1
kind: Service
metadata:
  name: mysql-service
spec:
  selector:
    app: mysql
  ports:
  - protocol: TCP
    port: 3306
    targetPort: 3306
  1. Execute the below command to create a service for MySQL:
kubectl apply -f mysql-service.yaml

Output:

service/mysql-service created
  1. To confirm if the service is created successfully, execute the below command:
kubectl get svc

Output:

NAME            TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)    AGE
echo1           ClusterIP   10.245.179.199   <none>        80/TCP     6h4m
echo2           ClusterIP   10.245.58.44     <none>        80/TCP     6h2m
kubernetes      ClusterIP   10.245.0.1       <none>        443/TCP    6h33m
mysql           ClusterIP   None             <none>        3306/TCP   4m57s
mysql-service   ClusterIP   10.245.159.76    <none>        3306/TCP   36s

The post How to Deploy MySQL database on Digital Ocean Managed Kubernetes Cluster appeared first on TEKSpace Blog.

]]>
https://blog.tekspace.io/how-to-deploy-mysql-database-on-digital-ocean-managed-kubernetes-cluster/feed/ 0