{"id":1771,"date":"2024-02-27T12:20:15","date_gmt":"2024-02-27T12:20:15","guid":{"rendered":"https:\/\/www.devopssupport.in\/blog\/?p=1771"},"modified":"2024-03-02T12:24:00","modified_gmt":"2024-03-02T12:24:00","slug":"setting-up-mysql-database-in-php","status":"publish","type":"post","link":"https:\/\/www.devopssupport.in\/blog\/setting-up-mysql-database-in-php\/","title":{"rendered":"Setting Up MySQL Database in PHP"},"content":{"rendered":"\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"719\" height=\"411\" src=\"https:\/\/www.devopssupport.in\/blog\/wp-content\/uploads\/2024\/03\/image-22-edited.png\" alt=\"\" class=\"wp-image-1773\" srcset=\"https:\/\/www.devopssupport.in\/blog\/wp-content\/uploads\/2024\/03\/image-22-edited.png 719w, https:\/\/www.devopssupport.in\/blog\/wp-content\/uploads\/2024\/03\/image-22-edited-300x171.png 300w\" sizes=\"auto, (max-width: 719px) 100vw, 719px\" \/><\/figure>\n\n\n\n<p><strong>Step 1: Install MySQL<\/strong><\/p>\n\n\n\n<p>If you haven&#8217;t already installed MySQL on your server or local development environment, you&#8217;ll need to do so. You can download MySQL from the official website (<a>https:\/\/dev.mysql.com\/downloads\/<\/a>) and follow the installation instructions for your operating system.<\/p>\n\n\n\n<p><strong>Step 2: Start MySQL Server<\/strong><\/p>\n\n\n\n<p>Once MySQL is installed, start the MySQL server. On most systems, you can do this by running a command such as.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo service mysql start\r\n<\/code><\/pre>\n\n\n\n<p><strong>Step 3: Access MySQL Shell<\/strong><\/p>\n\n\n\n<p>Next, access the MySQL shell by running the following command.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>mysql -u root -p\r<\/code><\/pre>\n\n\n\n<p>You&#8217;ll be prompted to enter the root password for MySQL. Once logged in, you&#8217;ll have access to the MySQL command-line interface.<\/p>\n\n\n\n<p><strong>Step 4: Create a Database<\/strong><\/p>\n\n\n\n<p>Now that you&#8217;re in the MySQL shell, let&#8217;s create a new database. Execute the following SQL command to create a database named <code>mydatabase<\/code>.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>CREATE DATABASE mydatabase;\r\n<\/code><\/pre>\n\n\n\n<p>You can replace <code>mydatabase<\/code> with the name of your desired database.<\/p>\n\n\n\n<p><strong>Step 5: Create a User<\/strong><\/p>\n\n\n\n<p>After creating the database, let&#8217;s create a new user and grant them privileges to access the database. Run the following command to create a user named <code>myuser<\/code> with a password.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>CREATE USER 'myuser'@'localhost' IDENTIFIED BY 'mypassword';\r<\/code><\/pre>\n\n\n\n<p>Replace <code>myuser<\/code> with your desired username and <code>mypassword<\/code> with your desired password.<\/p>\n\n\n\n<p><strong>Step 6: Grant Privileges<\/strong><\/p>\n\n\n\n<p>Next, grant the user privileges to access and manipulate the database. Execute the following command.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>GRANT ALL PRIVILEGES ON mydatabase.* TO 'myuser'@'localhost';\r\n<\/code><\/pre>\n\n\n\n<p>This grants the user <code>myuser<\/code> full privileges on the <code>mydatabase<\/code> database. If you want to grant specific privileges, you can replace <code>ALL PRIVILEGES<\/code> with the desired privileges (e.g., <code>SELECT, INSERT, UPDATE, DELETE<\/code>).<\/p>\n\n\n\n<p><strong>Step 7: Flush Privileges<\/strong><\/p>\n\n\n\n<p>After granting privileges, flush the MySQL privileges to ensure that they take effect.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>FLUSH PRIVILEGES;\r\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Step 1: Install MySQL If you haven&#8217;t already installed MySQL on your server or local development environment, you&#8217;ll need to do so. You can download MySQL from&#8230; <\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1771","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/www.devopssupport.in\/blog\/wp-json\/wp\/v2\/posts\/1771","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.devopssupport.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.devopssupport.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.devopssupport.in\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.devopssupport.in\/blog\/wp-json\/wp\/v2\/comments?post=1771"}],"version-history":[{"count":1,"href":"https:\/\/www.devopssupport.in\/blog\/wp-json\/wp\/v2\/posts\/1771\/revisions"}],"predecessor-version":[{"id":1774,"href":"https:\/\/www.devopssupport.in\/blog\/wp-json\/wp\/v2\/posts\/1771\/revisions\/1774"}],"wp:attachment":[{"href":"https:\/\/www.devopssupport.in\/blog\/wp-json\/wp\/v2\/media?parent=1771"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.devopssupport.in\/blog\/wp-json\/wp\/v2\/categories?post=1771"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.devopssupport.in\/blog\/wp-json\/wp\/v2\/tags?post=1771"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}