How to install and create a database in Ubuntu 20.04?

From PheonixSolutions
Jump to navigation Jump to search

1.Log in terminal

https://wiki.pheonixsolutions.com/index.php/File:Vertopal_9ab8c94744b0429d9f4fd3da428e4715-media-image6.png

2. If new server. Update and install MySQL server.

  1. apt update

https://wiki.pheonixsolutions.com/index.php/File:Vertopal_9ab8c94744b0429d9f4fd3da428e4715-media-image14.png

  1. apt install mysql-server

https://wiki.pheonixsolutions.com/index.php/File:Vertopal_9ab8c94744b0429d9f4fd3da428e4715-media-image2.png

3.Log into MySQL without a password.

  1. mysql

https://wiki.pheonixsolutions.com/index.php/File:Vertopal_9ab8c94744b0429d9f4fd3da428e4715-media-image4.png

4. Create a database
Example: Create a ‘School’ database

mysql> create database School;

https://wiki.pheonixsolutions.com/index.php/File:Vertopal_9ab8c94744b0429d9f4fd3da428e4715-media-image7.png

5. Create tables in the database.

Example: Create a Student_details table in the School database.

mysql> create table Students_Detalis (student_id int, student_name varchar(50), student_marks int, student_mobile_no int);

https://wiki.pheonixsolutions.com/index.php/File:Vertopal_9ab8c94744b0429d9f4fd3da428e4715-media-image10.png

6. Check tables in the School database whether they table create or not.

mysql> show tables;
https://wiki.pheonixsolutions.com/index.php/File:Vertopal_9ab8c94744b0429d9f4fd3da428e4715-media-image1.png

How to take backup and restore the MySQL database?

[edit]
  1. Go to the MySQL directory and have the databases directories


    #cd /var/lib/mysql

https://wiki.pheonixsolutions.com/index.php/File:Vertopal_9ab8c94744b0429d9f4fd3da428e4715-media-image3.png

  1. Dumb the School database to School_information.sql file.

#mysqldump -u root School > School_information.sql

https://wiki.pheonixsolutions.com/index.php/File:Vertopal_9ab8c94744b0429d9f4fd3da428e4715-media-image5.png

  1. Log into the mysql and check the databases.

mysql> show databases;

https://wiki.pheonixsolutions.com/index.php/File:Vertopal_9ab8c94744b0429d9f4fd3da428e4715-media-image12.png

  1. Go to the School database and Check the tables.

mysql > show tables;

https://wiki.pheonixsolutions.com/index.php/File:Vertopal_9ab8c94744b0429d9f4fd3da428e4715-media-image12.png

  1. Delete the School database.

mysql> drop database School;

https://wiki.pheonixsolutions.com/index.php/File:Vertopal_9ab8c94744b0429d9f4fd3da428e4715-media-image13.png

  1. Create a database to restore the deleted database.

mysql> create database School_information;

https://wiki.pheonixsolutions.com/index.php/File:Vertopal_9ab8c94744b0429d9f4fd3da428e4715-media-image16.png

  1. Exit, Go to mysql directory and restore the dumping file.

#cd /var/lib/mysql

  1. mysql -u root -p School_information < School_information.sql

https://wiki.pheonixsolutions.com/index.php/File:Vertopal_9ab8c94744b0429d9f4fd3da428e4715-media-image8.png

https://wiki.pheonixsolutions.com/index.php/File:Vertopal_9ab8c94744b0429d9f4fd3da428e4715-media-image11.png

  1. Check the restore table in School_information.

https://wiki.pheonixsolutions.com/index.php/File:Vertopal_9ab8c94744b0429d9f4fd3da428e4715-media-image15.png