{"id":30100,"date":"2021-01-18T03:14:19","date_gmt":"2021-01-17T19:14:19","guid":{"rendered":"https:\/\/web.mwwsb.com.my\/pjci\/?post_type=kb&#038;p=30100"},"modified":"2022-09-08T19:13:39","modified_gmt":"2022-09-08T11:13:39","slug":"how-to-manage-mysql","status":"publish","type":"kb","link":"https:\/\/www.casbay.com\/guide\/kb\/how-to-manage-mysql","title":{"rendered":"HOW TO: Manage MySQL"},"content":{"rendered":"\t\t<div data-elementor-type=\"wp-post\" data-elementor-id=\"30100\" class=\"elementor elementor-30100\" data-elementor-post-type=\"kb\">\n\t\t\t\t\t\t\t\t\t<section class=\"elementor-section elementor-top-section elementor-element elementor-element-d94f1e5 elementor-section-boxed elementor-section-height-default elementor-section-height-default\" data-id=\"d94f1e5\" data-element_type=\"section\">\n\t\t\t\t\t\t<div class=\"elementor-container elementor-column-gap-default\">\n\t\t\t\t\t<div class=\"elementor-column elementor-col-100 elementor-top-column elementor-element elementor-element-3e47d75\" data-id=\"3e47d75\" data-element_type=\"column\">\n\t\t\t<div class=\"elementor-widget-wrap elementor-element-populated\">\n\t\t\t\t\t\t\t\t<div class=\"elementor-element elementor-element-ceaf8b3 elementor-widget elementor-widget-heading\" data-id=\"ceaf8b3\" data-element_type=\"widget\" data-widget_type=\"heading.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t<h2 class=\"elementor-heading-title elementor-size-default\">Manage MySQL Database<\/h2>\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-a21f422 elementor-widget elementor-widget-text-editor\" data-id=\"a21f422\" data-element_type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t<p>This guide describes how to do common MySQL database administration tasks using the command line with the <em>mysql<\/em> program. These tasks include how to create and delete databases, users, and tables.<\/p>\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-8ee1b1e elementor-widget elementor-widget-heading\" data-id=\"8ee1b1e\" data-element_type=\"widget\" data-widget_type=\"heading.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t<h3 class=\"elementor-heading-title elementor-size-default\">Add users and databases<\/h3>\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-bbb66f8 elementor-widget elementor-widget-text-editor\" data-id=\"bbb66f8\" data-element_type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t<ol><li>Firstly, log in to MySQL as the root user at the command line:<pre class=\"command\">mysql -u root -p<\/pre><\/li><li>Then, type the MySQL root password, and press <strong>Enter<\/strong>.<br \/><br \/><\/li><li><p>To create a <strong>database user<\/strong>, type the following command. <br \/>Replace\u00a0<em>username <\/em>and <em>password<\/em> with the user you want to create and\u00a0the user&#8217;s password:<\/p><pre class=\"command\">GRANT ALL PRIVILEGES ON *.* TO 'username'@'localhost' IDENTIFIED BY 'password';<\/pre><div class=\"note msg-box\"><p>This command grants the user all permissions. However, you can specify permissions to maintain precise control over database access. For instance, to explicitly grant the <strong>SELECT<\/strong> permission, you can use the following command:<\/p><pre class=\"command\">GRANT SELECT ON *.* TO 'username'@'localhost';<\/pre><p>For more information about setting MySQL database permissions, kindly visit <a href=\"https:\/\/dev.mysql.com\/doc\/refman\/5.5\/en\/grant.html\" class=\"broken_link\" rel=\"noopener\">https:\/\/dev.mysql.com\/doc\/refman\/5.5\/en\/grant.html<\/a>.<\/p><\/div><\/li><li>After that, type <span class=\"user-input\">\\q<\/span>\u00a0to exit the\u00a0<em>mysql<\/em>\u00a0program.<br \/><br \/><\/li><li><p>To log in to MySQL as the user you just created, type the following command. Replace\u00a0<em>username<\/em>\u00a0with the name of the user you created in step 3:<\/p><pre class=\"command\">mysql -u username -p<\/pre><\/li><li>Next, type the user&#8217;s password, and then press <strong>Enter<\/strong>.<br \/><br \/><\/li><li><p>To create a <strong>database<\/strong>, type the following command.<br \/>Replace\u00a0<em>dbname<\/em>\u00a0with the name of the database that you want to create:<\/p><pre class=\"command\">CREATE DATABASE dbname;<\/pre><\/li><li><p>To <strong>work with the new database<\/strong>, use the following command. <br \/>Replace <em>dbname<\/em>\u00a0with the name of the database you created in step 7:<\/p><pre class=\"command\">USE dbname;<\/pre><\/li><li><p>You can now work with the database. For example, the following commands show how to create a basic table named <em>table<\/em>, and how to insert some data into it:<\/p><pre class=\"command\">CREATE TABLE table( id smallint unsigned not null auto_increment, name varchar(20) not null, constraint pk_table primary key (id) );\nINSERT INTO table( id, name ) VALUES ( null, 'Sample data' );<\/pre><\/li><\/ol>\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-a5e988f elementor-widget elementor-widget-heading\" data-id=\"a5e988f\" data-element_type=\"widget\" data-widget_type=\"heading.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t<h3 class=\"elementor-heading-title elementor-size-default\">Remove Tables and Databases<\/h3>\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-f3bdf85 elementor-widget elementor-widget-text-editor\" data-id=\"f3bdf85\" data-element_type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t<p>To delete a table, use the following command from the <strong>mysql&gt;<\/strong>\u00a0prompt. Replace\u00a0<em>tablename<\/em>\u00a0with the name of the table that you want to delete:<\/p><pre class=\"command\">DROP TABLE tablename;<\/pre><div class=\"note msg-box\">This command assumes that you have already selected a database by using the\u00a0<strong>USE<\/strong>\u00a0statement.<br \/><br \/><\/div><p>Similarly, to delete an entire database, use the following command from the <strong>mysql&gt;<\/strong>\u00a0prompt. Replace\u00a0<em>dbname<\/em>\u00a0with the name of the database that you want to delete:<\/p><pre class=\"command\">DROP DATABASE dbname;<\/pre>\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-1fb6676 elementor-widget elementor-widget-heading\" data-id=\"1fb6676\" data-element_type=\"widget\" data-widget_type=\"heading.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t<h3 class=\"elementor-heading-title elementor-size-default\">Removing Database Users<\/h3>\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-d3910b8 elementor-widget elementor-widget-text-editor\" data-id=\"d3910b8\" data-element_type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t<p>To view a list of all users, type the following command from the\u00a0<strong>mysql&gt;<\/strong>\u00a0prompt:<\/p><pre class=\"command\">SELECT user FROM mysql.user GROUP BY user;<\/pre><p>To delete a specific user, type the following command from the\u00a0<strong>mysql&gt;<\/strong>\u00a0prompt. Replace\u00a0<em>username<\/em>\u00a0with the name of the user that you want to delete:<\/p><pre class=\"command\">DELETE FROM mysql.user WHERE user = 'username';<\/pre>\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-084ec0f elementor-widget elementor-widget-text-editor\" data-id=\"084ec0f\" data-element_type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t<p>We hope this article helped you to learn how to manage MySQL database. For more articles, please go to <a href=\"https:\/\/www.casbay.com.my\/guide\/\">Knowledge Base<\/a>.<\/p>\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t\t<\/div>\n\t\t<\/div>\n\t\t\t\t\t\t\t<\/div>\n\t\t<\/section>\n\t\t\t\t\t\t\t<\/div>\n\t\t","protected":false},"excerpt":{"rendered":"<p>Manage MySQL Database This guide describes how to do common MySQL database administration tasks using the command line with the mysql program. These tasks include how to create and delete databases, users, and tables. Add users and databases Firstly, log in to MySQL as the root user at the command line: mysql -u root -p [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"site-sidebar-layout":"no-sidebar","site-content-layout":"page-builder","ast-site-content-layout":"","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"disabled","ast-breadcrumbs-content":"","ast-featured-img":"disabled","footer-sml-layout":"","theme-transparent-header-meta":"default","adv-header-id-meta":"","stick-header-meta":"default","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-gradient":""}}},"kbtopic":[60],"kbtag":[106],"mkb_version":[],"_links":{"self":[{"href":"https:\/\/www.casbay.com\/guide\/wp-json\/wp\/v2\/kb\/30100"}],"collection":[{"href":"https:\/\/www.casbay.com\/guide\/wp-json\/wp\/v2\/kb"}],"about":[{"href":"https:\/\/www.casbay.com\/guide\/wp-json\/wp\/v2\/types\/kb"}],"author":[{"embeddable":true,"href":"https:\/\/www.casbay.com\/guide\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.casbay.com\/guide\/wp-json\/wp\/v2\/comments?post=30100"}],"version-history":[{"count":1,"href":"https:\/\/www.casbay.com\/guide\/wp-json\/wp\/v2\/kb\/30100\/revisions"}],"predecessor-version":[{"id":31739,"href":"https:\/\/www.casbay.com\/guide\/wp-json\/wp\/v2\/kb\/30100\/revisions\/31739"}],"wp:attachment":[{"href":"https:\/\/www.casbay.com\/guide\/wp-json\/wp\/v2\/media?parent=30100"}],"wp:term":[{"taxonomy":"kbtopic","embeddable":true,"href":"https:\/\/www.casbay.com\/guide\/wp-json\/wp\/v2\/kbtopic?post=30100"},{"taxonomy":"kbtag","embeddable":true,"href":"https:\/\/www.casbay.com\/guide\/wp-json\/wp\/v2\/kbtag?post=30100"},{"taxonomy":"mkb_version","embeddable":true,"href":"https:\/\/www.casbay.com\/guide\/wp-json\/wp\/v2\/mkb_version?post=30100"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}