Delete MySQL database using command line

2024-02-17 21:45:52

<p>Before starting, we need to ensure that we have MySQL administrator privileges and root access privileges, as well as backup and confirm that deleted database data cannot be restored.</p><p>The following are the steps to delete a MySQL database:</p><p><br/></p><p>Step 1: Connect to the MySQL database server</p><p>To connect to the MySQL server, we need to use the MySQL command executable file and the credentials of the root user. Type the following command in the terminal:</p><div class="pre-wrapper" style="margin: 8px 0px; padding: 0px; box-sizing: border-box; position: relative; color: rgb(45, 48, 55); font-family: &quot;Helvetica Neue&quot;, Helvetica, Arial, &quot;Microsoft Yahei&quot;, &quot;Hiragino Sans GB&quot;, &quot;Heiti SC&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif; text-wrap: wrap; background-color: rgb(255, 255, 255);"><pre class="brush:php;toolbar:false; hljs language-css" data-copyid="1" data-highlighted="yes" style="margin-top: 8px; margin-bottom: 8px; padding: 15px; box-sizing: border-box; overflow-x: auto; background-color: rgb(240, 242, 245); font-size: 14px; border: 1px solid rgb(219, 225, 232); border-radius: 4px; text-wrap: wrap; overflow-wrap: break-word; word-break: break-all;">mysql&nbsp;-u&nbsp;root&nbsp;-p</pre><button class="ai-code-helper" style="margin: 0px; padding: 0px 8px; position: absolute; top: 2px; right: 80px; height: 24px; line-height: 24px; font-size: 12px; color: rgb(255, 239, 239); background: rgb(255, 102, 102); border-radius: 4px; outline: none; border-width: initial; border-style: none; border-color: initial; cursor: pointer; opacity: 0;">AI代码助手</button><button class="copy-code" title="复制" style="margin: 0px; padding: 0px 8px; position: absolute; top: 2px; right: 8px; background: rgb(240, 242, 245); height: 24px; line-height: 24px; font-size: 12px; color: rgb(158, 167, 179); border-radius: 4px; outline: none; border-width: initial; border-style: none; border-color: initial; cursor: pointer; opacity: 0;">复制代码</button></div><p style="margin-top: 24px; margin-bottom: 24px; padding: 0px; box-sizing: border-box; line-height: 30px; color: rgb(45, 48, 55); word-break: break-all; font-family: &quot;Helvetica Neue&quot;, Helvetica, Arial, &quot;Microsoft Yahei&quot;, &quot;Hiragino Sans GB&quot;, &quot;Heiti SC&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif; text-wrap: wrap; background-color: rgb(255, 255, 255);">This will start the MySQL server and log in as root. Enter the root user password to complete the login.</p><p>Step 2: List the existing databases</p><p>Once we successfully log in to the MySQL server, at the MySQL command prompt, we can list all existing databases using the following command:</p><div class="pre-wrapper" style="margin: 8px 0px; padding: 0px; box-sizing: border-box; position: relative; color: rgb(45, 48, 55); font-family: &quot;Helvetica Neue&quot;, Helvetica, Arial, &quot;Microsoft Yahei&quot;, &quot;Hiragino Sans GB&quot;, &quot;Heiti SC&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif; text-wrap: wrap; background-color: rgb(255, 255, 255);"><pre class="brush:php;toolbar:false; hljs language-sql" data-copyid="2" data-highlighted="yes" style="margin-top: 8px; margin-bottom: 8px; padding: 15px; box-sizing: border-box; overflow-x: auto; background-color: rgb(240, 242, 245); font-size: 14px; border: 1px solid rgb(219, 225, 232); border-radius: 4px; text-wrap: wrap; overflow-wrap: break-word; word-break: break-all;">SHOW&nbsp;DATABASES;</pre><button class="ai-code-helper" style="margin: 0px; padding: 0px 8px; position: absolute; top: 2px; right: 80px; height: 24px; line-height: 24px; font-size: 12px; color: rgb(255, 239, 239); background: rgb(255, 102, 102); border-radius: 4px; outline: none; border-width: initial; border-style: none; border-color: initial; cursor: pointer; opacity: 0;">AI代码助手</button><button class="copy-code" title="复制" style="margin: 0px; padding: 0px 8px; position: absolute; top: 2px; right: 8px; background: rgb(240, 242, 245); height: 24px; line-height: 24px; font-size: 12px; color: rgb(158, 167, 179); border-radius: 4px; outline: none; border-width: initial; border-style: none; border-color: initial; cursor: pointer; opacity: 0;">复制代码</button></div><p>When we press the enter key, the system will display all existing databases on the command line.</p><p><br/></p><p>Step 3: Select the database to be deleted</p><p>To delete a database, we need to use the DROP DATABASE command, but before executing it, we need to determine the name of the database to be deleted. Select the database to be deleted using the following command:</p><div class="pre-wrapper" style="margin: 8px 0px; padding: 0px; box-sizing: border-box; position: relative; color: rgb(45, 48, 55); font-family: &quot;Helvetica Neue&quot;, Helvetica, Arial, &quot;Microsoft Yahei&quot;, &quot;Hiragino Sans GB&quot;, &quot;Heiti SC&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif; text-wrap: wrap; background-color: rgb(255, 255, 255);"><pre class="brush:php;toolbar:false; hljs language-undefined" data-copyid="3" data-highlighted="yes" style="margin-top: 8px; margin-bottom: 8px; padding: 15px; box-sizing: border-box; overflow-x: auto; background-color: rgb(240, 242, 245); font-size: 14px; border: 1px solid rgb(219, 225, 232); border-radius: 4px; text-wrap: wrap; overflow-wrap: break-word; word-break: break-all;">USE&nbsp;database_name;</pre><button class="ai-code-helper" style="margin: 0px; padding: 0px 8px; position: absolute; top: 2px; right: 80px; height: 24px; line-height: 24px; font-size: 12px; color: rgb(255, 239, 239); background: rgb(255, 102, 102); border-radius: 4px; outline: none; border-width: initial; border-style: none; border-color: initial; cursor: pointer; opacity: 0;">AI代码助手</button><button class="copy-code" title="复制" style="margin: 0px; padding: 0px 8px; position: absolute; top: 2px; right: 8px; background: rgb(240, 242, 245); height: 24px; line-height: 24px; font-size: 12px; color: rgb(158, 167, 179); border-radius: 4px; outline: none; border-width: initial; border-style: none; border-color: initial; cursor: pointer; opacity: 0;">复制代码</button></div><p>In this command, replace &quot;databasename&quot; with the actual database name to be deleted. This will make the selection database active, allowing the DROP DATABASE command to be executed.</p><p><br/></p><p>Step 4: Delete the database</p><p>Once we have selected the database to be deleted, we can use the following command to delete it:</p><div class="pre-wrapper" style="margin: 8px 0px; padding: 0px; box-sizing: border-box; position: relative; color: rgb(45, 48, 55); font-family: &quot;Helvetica Neue&quot;, Helvetica, Arial, &quot;Microsoft Yahei&quot;, &quot;Hiragino Sans GB&quot;, &quot;Heiti SC&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif; text-wrap: wrap; background-color: rgb(255, 255, 255);"><pre class="brush:php;toolbar:false; hljs language-sql" data-copyid="4" data-highlighted="yes" style="margin-top: 8px; margin-bottom: 8px; padding: 15px; box-sizing: border-box; overflow-x: auto; background-color: rgb(240, 242, 245); font-size: 14px; border: 1px solid rgb(219, 225, 232); border-radius: 4px; text-wrap: wrap; overflow-wrap: break-word; word-break: break-all;">DROP&nbsp;DATABASE&nbsp;database_name;</pre><button class="ai-code-helper" style="margin: 0px; padding: 0px 8px; position: absolute; top: 2px; right: 80px; height: 24px; line-height: 24px; font-size: 12px; color: rgb(255, 239, 239); background: rgb(255, 102, 102); border-radius: 4px; outline: none; border-width: initial; border-style: none; border-color: initial; cursor: pointer; opacity: 0;">AI代码助手</button><button class="copy-code" title="复制" style="margin: 0px; padding: 0px 8px; position: absolute; top: 2px; right: 8px; background: rgb(240, 242, 245); height: 24px; line-height: 24px; font-size: 12px; color: rgb(158, 167, 179); border-radius: 4px; outline: none; border-width: initial; border-style: none; border-color: initial; cursor: pointer; opacity: 0;">复制代码</button></div><p>After executing this command, MySQL will delete the specified database and all its contents from the server. Please note that the deletion operation is irreversible, so be sure to backup before executing DROP DATABASE.</p><p><br/></p><p>Step 5: Exit MySQL</p><p>After deleting the required database, we can exit the MySQL server. At the MySQL command line, enter the following command:</p><div class="pre-wrapper" style="margin: 8px 0px; padding: 0px; box-sizing: border-box; position: relative; color: rgb(45, 48, 55); font-family: &quot;Helvetica Neue&quot;, Helvetica, Arial, &quot;Microsoft Yahei&quot;, &quot;Hiragino Sans GB&quot;, &quot;Heiti SC&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif; text-wrap: wrap; background-color: rgb(255, 255, 255);"><pre class="brush:php;toolbar:false; hljs language-vbnet" data-copyid="5" data-highlighted="yes" style="margin-top: 8px; margin-bottom: 8px; padding: 15px; box-sizing: border-box; overflow-x: auto; background-color: rgb(240, 242, 245); font-size: 14px; border: 1px solid rgb(219, 225, 232); border-radius: 4px; text-wrap: wrap; overflow-wrap: break-word; word-break: break-all;">EXIT;</pre><button class="ai-code-helper" style="margin: 0px; padding: 0px 8px; position: absolute; top: 2px; right: 80px; height: 24px; line-height: 24px; font-size: 12px; color: rgb(255, 239, 239); background: rgb(255, 102, 102); border-radius: 4px; outline: none; border-width: initial; border-style: none; border-color: initial; cursor: pointer; opacity: 0;">AI代码助手</button><button class="copy-code" title="复制" style="margin: 0px; padding: 0px 8px; position: absolute; top: 2px; right: 8px; background: rgb(240, 242, 245); height: 24px; line-height: 24px; font-size: 12px; color: rgb(158, 167, 179); border-radius: 4px; outline: none; border-width: initial; border-style: none; border-color: initial; cursor: pointer; opacity: 0;">复制代码</button></div><p style="margin-top: 24px; margin-bottom: 24px; padding: 0px; box-sizing: border-box; line-height: 30px; color: rgb(45, 48, 55); word-break: break-all; font-family: &quot;Helvetica Neue&quot;, Helvetica, Arial, &quot;Microsoft Yahei&quot;, &quot;Hiragino Sans GB&quot;, &quot;Heiti SC&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif; text-wrap: wrap; background-color: rgb(255, 255, 255);">After completion, we will be taken back to the terminal prompt.</p><p><br/></p>


Copyright Description:No reproduction without permission。

Knowledge sharing community for developers。

Let more developers benefit from it。

Help developers share knowledge through the Internet。

Follow us

high perspicacity