MySQL Script on ubuntu-server
MySQL Script

MySQL Script on ubuntu-server

set-hostname

sudo hostnamectl set-hostname mysql_script.server
hostname        
No alt text provided for this image

write a script in file name mysql_setup.sh

vim mysql_setup.sh        

install the MySQL package and enable & restart service and set a root user password in script .

#!/bin/bas
sudo apt install -y mysql-server
sudo systemctl enable --now mysql.service
sudo systemctl restart mysql.service
MYSQL_ROOT_PASSWORD=$(cat /root/secure/password-file)
mysql -u root -p"${MYSQL_ROOT_PASSWORD}" <<SQL
CREATE DATABASE shubham;
CREATE USER 'Gupta'@'localhost' IDENTIFIED BY '${MYSQL_ROOT_PASSWORD}';
GRANT ALL PRIVILEGES ON shubham.* TO 'Gupta'@'localhost';
SHOW GRANTS FOR 'Gupta'@'localhost';
FLUSH PRIVILEGES;
SQL
echo "MySQL setup complete!"
mysql -u Gupta -p"${MYSQL_ROOT_PASSWORD}" <<SQL
SHOW DATABASES;
USE shubham;
CREATE TABLE Designx (
? ? id INT PRIMARY KEY AUTO_INCREMENT,
? ? name VARCHAR(50),
? ? age INT,
? ? dob DATE
);
INSERT INTO Designx (name, age, dob) VALUES ('shubham', 24, '1998-05-02');
INSERT INTO Army (name, age, dob) VALUES ('amit', 26, '1996-06-01');
SQL        

if I want to secure password so we mention password in another file and this file path mention in script then script read MySQL root password securely from file and make a variable that password file.

vim /root/secure/password-file
redhat        
No alt text provided for this image

script description in detail

  1. # Install MySQL

sudo apt install -y mysql-server

2. # Start MySQL service and enable it on boot

sudo systemctl enable --now mysql.service

sudo systemctl restart mysql.service

3. # Read MySQL root password securely from a file

MYSQL_ROOT_PASSWORD=$(cat /root/secure/password-file)

4. # Log in to MySQL and perform SQL commands

mysql -u root -p"${MYSQL_ROOT_PASSWORD}" <<SQL

CREATE DATABASE shubham;

CREATE USER 'Gupta'@'localhost' IDENTIFIED BY '${MYSQL_ROOT_PASSWORD}';

GRANT ALL PRIVILEGES ON shubham.* TO 'Gupta'@'localhost';

SHOW GRANTS FOR 'Gupta'@'localhost';

FLUSH PRIVILEGES;

SQL

5. # Log in to MySQL as user 'Gupta' and perform SQL commands

mysql -u Gupta -p"${MYSQL_ROOT_PASSWORD}" <<SQL

SHOW DATABASES;

USE shubham;

CREATE TABLE IF NOT EXISTS Designx (

??id INT PRIMARY KEY AUTO_INCREMENT,

??name VARCHAR(50),

??age INT,

??dob DATE

);

INSERT INTO Designx (name, age, dob) VALUES ('shubham', 25, '1998-05-02');

INSERT INTO Designx (name, age, dob) VALUES ('Amit', 26, '1997-06-01');

SQL


Vijay Pal Singh

DevOps | AWS | Linux | Cloud | Terraform | Ansible | Git and Git Hub | Docker | Jenkins |

1 å¹´

??

赞
回复

要查看或添加评论,请登录

Shubham Gupta的更多文章

  • Install and Secure Grafana on Ubuntu 20.04

    Install and Secure Grafana on Ubuntu 20.04

    1. Introduction To Grafana Grafana is an open-source platform for data visualization, monitoring and analysis.

  • NFS (Network File System)

    NFS (Network File System)

    Network File System allows a system to share directories and files with others over a network. By using NFS, users and…

    1 条评论
  • To Configure a Mail Server on RHEL v9 m/c ...

    To Configure a Mail Server on RHEL v9 m/c ...

    Mail Server is a software program that sends and receives email. Often, it is used as a blanket term for both mail…

  • Difference B/w Apache webserver vs Nginx webserver ??

    Difference B/w Apache webserver vs Nginx webserver ??

    Comparision : Apache vs Nginx web-server..

社区洞察

其他会员也浏览了