Logo
  • Ubuntu
  • CentOS
  • Debian
  • Fedora
  • RedHat

How to Create and Select MySQL Database - DesignLinux

Oct 14 2020
designlinux 0 Comments
How to Create and Select MySQL Databases

MySQL is the most popular and open-source relational database management system. In this article we will show you how to create MySQL or MariaDB database through the command line.

Prerequisite#

  • Make sure MySQL or MariaDB is already installed on your system.
  • User account should have privileges to perform actions or have root user account.

Connect MySQL#

To connect to the MySQL shell type the following command and enter your MySQL root user password when prompted:

mysql -u root -p

You should omit the -p option if you have not set a password for your MySQL root user.

Create a MySQL Database#

To create a MySQL database is simple using a single command. Issue the following command to create a MySQL or MariaDB database.

CREATE DATABASE DATABASE_NAME;

In above command you should replace the DATABASE_NAME with your database name which you want as database name.

Query OK, 1 row affected (0.00 sec)

It will show the error message as following, if the database name is already exists.

ERROR 1007 (HY000): Can't create database 'DATABASE_NAME'; database exists

You can avoid this type of error messages using the IF NOT EXISTS statement:

CREATE DATABASE IF NOT EXISTS DATABASE_NAME;
Query OK, 1 row affected, 1 warning (0.00 sec)

In the output above, Query OK means that the query was successful, and 1 warning tells us that the database already exists, and no new database was created.

List All MySQL Databases#

To list the all MySQL databases using MySQL shell, run the following command:

SHOW DATABASES;

This command will show all the databases available on the server. The output will be something like this :

+--------------------+
| Database           |
+--------------------+
| information_schema |
| database_name      |
| mysql              |
| performance_schema |
| test               |
+--------------------+
5 rows in set (0.00 sec)

Select a MySQL Database#

If you have created a new database, by default, it will not be selected to use. Use the following database before you start a MySQL session:

USE DATABASE_NAME;
Database changed

On the selecting database, now onwards all the operations and actions will be performed on the selected database.

You also can define the database name while you are connecting to the MySQL server. Just add the database name at the end of the connecting command as given below:

mysql -u root -p DATABASE_NAME

Create a MySQL Database with mysqladmin#

Using the mysqladmin utility you can create a new MySQL database from the Linux terminal. For example, to create a database named DATABASE_NAME, you would use the following command:

mysqladmin -u root -p create DATABASE_NAME

Conclusion#

We have shown you how to create and select MySQL databases using the MySQL shell and mysqladmin command.

If you have any questions or feedback, feel free to leave a comment below.

Related

Tags: mariadb, mysql

How to Install Drupal on Debian 10

Prev Post

How to Share a Local Folder with a Remote Host Running on VMWare

Next Post
Archives
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022
  • July 2022
  • June 2022
  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • June 2021
  • May 2021
  • April 2021
  • March 2021
  • February 2021
  • January 2021
  • December 2020
  • November 2020
  • October 2020
  • September 2020
  • August 2020
  • July 2020
  • June 2020
  • May 2020
Categories
  • AlmaLinux
  • Android
  • Ansible
  • Apache
  • Arch Linux
  • AWS
  • Backups
  • Bash Shell
  • Bodhi Linux
  • CentOS
  • CentOS Stream
  • Chef
  • Cloud Software
  • CMS
  • Commandline Tools
  • Control Panels
  • CouchDB
  • Data Recovery Tools
  • Databases
  • Debian
  • Deepin Linux
  • Desktops
  • Development Tools
  • Docker
  • Download Managers
  • Drupal
  • Editors
  • Elementary OS
  • Encryption Tools
  • Fedora
  • Firewalls
  • FreeBSD
  • FTP
  • GIMP
  • Git
  • Hadoop
  • HAProxy
  • Java
  • Jenkins
  • Joomla
  • Kali Linux
  • KDE
  • Kubernetes
  • KVM
  • Laravel
  • Let's Encrypt
  • LFCA
  • Linux Certifications
  • Linux Commands
  • Linux Desktop
  • Linux Distros
  • Linux IDE
  • Linux Mint
  • Linux Talks
  • Lubuntu
  • LXC
  • Mail Server
  • Manjaro
  • MariaDB
  • MongoDB
  • Monitoring Tools
  • MySQL
  • Network
  • Networking Commands
  • NFS
  • Nginx
  • Nodejs
  • NTP
  • Open Source
  • OpenSUSE
  • Oracle Linux
  • Package Managers
  • Pentoo
  • PHP
  • Podman
  • Postfix Mail Server
  • PostgreSQL
  • Python
  • Questions
  • RedHat
  • Redis Server
  • Rocky Linux
  • Security
  • Shell Scripting
  • SQLite
  • SSH
  • Storage
  • Suse
  • Terminals
  • Text Editors
  • Top Tools
  • Torrent Clients
  • Tutorial
  • Ubuntu
  • Udemy Courses
  • Uncategorized
  • VirtualBox
  • Virtualization
  • VMware
  • VPN
  • VSCode Editor
  • Web Browsers
  • Web Design
  • Web Hosting
  • Web Servers
  • Webmin
  • Windows
  • Windows Subsystem
  • WordPress
  • Zabbix
  • Zentyal
  • Zorin OS
Visits
  • 0
  • 606
  • 1,055,378

DesignLinux.com © All rights reserved

Go to mobile version