{"id":103,"date":"2023-04-23T12:01:47","date_gmt":"2023-04-23T12:01:47","guid":{"rendered":"https:\/\/koner.mywire.org\/?page_id=103"},"modified":"2023-04-23T12:38:35","modified_gmt":"2023-04-23T12:38:35","slug":"setup-a-raspberry-pi-mysql-database","status":"publish","type":"page","link":"https:\/\/koner.mywire.org\/index.php\/setup-a-raspberry-pi-mysql-database\/","title":{"rendered":"Setup a Raspberry Pi MYSQL Database"},"content":{"rendered":"\n<h4 class=\"wp-block-heading\">In this Raspberry Pi MYSQL tutorial, we will be showing you how to install and configure the MySQL server on your Pi.<\/h4>\n\n\n\n<p>MySQL is one of the world\u2019s most popular relational database system and is a common inclusion in most&nbsp;<a rel=\"noreferrer noopener\" href=\"https:\/\/en.wikipedia.org\/wiki\/LAMP_(software_bundle)\" target=\"_blank\">LAMP<\/a>&nbsp;( Linux, Apache, MYSQL, and PHP) stacks. It is one of the pieces of technology that helps drive the modern web.<\/p>\n\n\n\n<p>A database such as MYSQL is often a key component of dynamic websites and is one of the best ways of storing data for web applications.<\/p>\n\n\n\n<p>If you\u2019re unfamiliar with MySQL, it is a&nbsp;<a href=\"https:\/\/en.wikipedia.org\/wiki\/Relational_database\" target=\"_blank\" rel=\"noreferrer noopener\">relational database<\/a>&nbsp;management system and allows you to store and maintain large amounts of data easily.<\/p>\n\n\n\n<p>Setup&nbsp;something like PHPMyAdmin&nbsp;if you want a graphical user interface rather than the command line. It does make managing a database slightly easier.<\/p>\n\n\n\n<p>You will need to go through this tutorial for setting up any&nbsp;web server project&nbsp;that requires a database. For example, we use it in our&nbsp;<a href=\"https:\/\/koner.mywire.org\/\" data-type=\"URL\" data-id=\"https:\/\/koner.mywire.org\/\">WordPress tutorial<\/a>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"equipment-list\">Equipment List<\/h2>\n\n\n\n<p>Below are the pieces of equipment that I made use of for this Raspberry Pi MySQL tutorial.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Recommended<\/h3>\n\n\n\n<ul class=\"wp-block-list\" id=\"recommended_equipment\">\n<li>Raspberry Pi<\/li>\n\n\n\n<li>Micro SD Card<\/li>\n\n\n\n<li>Power Supply<\/li>\n\n\n\n<li>Ethernet Cable&nbsp;or&nbsp;Wi-Fi<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Setting up MYSQL on a Raspberry Pi<\/h3>\n\n\n\n<p>As with all of our tutorials, we will be utilizing the Raspbian operating system. If you\u2019re using something different, then the steps may differ slightly.<\/p>\n\n\n\n<p><strong>1.<\/strong>&nbsp;Before we get started with installing MySQL to our Raspberry Pi, we must first update our package list and all installed packages.<\/p>\n\n\n\n<p>We can do this by running the following two commands.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo apt update\nsudo apt upgrade<\/code><\/pre>\n\n\n\n<p><strong>2.<\/strong>&nbsp;The next step is to install the MySQL server software to your Raspberry Pi.<\/p>\n\n\n\n<p>Installing MySQL to the Raspberry Pi is a simple process and can be done with the following command.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo apt install mariadb-server<\/code><\/pre>\n\n\n\n<p><strong>3.<\/strong>&nbsp;With the MySQL server software installed to the Raspberry Pi, we will now need to secure it by setting a password for the \u201c<code>root<\/code>\u201d user.<\/p>\n\n\n\n<p>By default, MySQL is installed without any password set up meaning you can access the MySQL server without any authentication.<\/p>\n\n\n\n<p>Run the following command to begin the MySQL securing process.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>sudo mysql_secure_installation<\/code><\/pre>\n\n\n\n<p>Just follow the prompts to set a password for the root user and to secure your MySQL installation.<\/p>\n\n\n\n<p>For a more secure installation, you should answer \u201c<code>Y<\/code>\u201d to all prompts when asked to answer \u201c<code>Y<\/code>\u201d or \u201c<code>N<\/code>\u201c.<\/p>\n\n\n\n<p>These prompts will remove features that allows someone to gain access to the server easier.<\/p>\n\n\n\n<p>Make sure you write down the password you set during this process as we will need to use it to access the MySQL server and create databases and users for software such as WordPress or PHPMyAdmin.<\/p>\n\n\n\n<p><strong>4.<\/strong>&nbsp;Now if you want to access your Raspberry Pi\u2019s MySQL server and start making changes to your databases, you can enter the following command.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>sudo mysql -u root -p<\/code><\/pre>\n\n\n\n<p><strong>5.<\/strong>&nbsp;You will be prompted to enter the password that we just created in&nbsp;<strong>step 3<\/strong>&nbsp;for MySQL\u2019s root user.<\/p>\n\n\n\n<p><strong>Note:<\/strong>&nbsp;Like most Linux password inputs, the text will not show up as you type.<\/p>\n\n\n\n<p><strong>6.<\/strong>&nbsp;You can now enter MYSQL commands to create, alter, and delete databases. Through this interface, you can also create or delete users and assign them the rights to manage any database.<\/p>\n\n\n\n<p><strong>7.<\/strong>&nbsp;There are two different ways you can quit out of the MYSQL command line, the first of those is to&nbsp;<kbd>type<\/kbd>&nbsp;\u201c<code>quit;<\/code>\u201d into the MySQL interface.<\/p>\n\n\n\n<p>The other way of quitting out of the MYSQL command line is to press&nbsp;<kbd>CTRL<\/kbd>&nbsp;+&nbsp;<kbd>D<\/kbd>.<\/p>\n\n\n\n<p><strong>8.<\/strong>&nbsp;At this point, you will now have successfully setup MySQL on your Raspberry Pi. Our next few sections will go into making better use of this database.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Creating a MySQL Database and User<\/h3>\n\n\n\n<p><strong>1.<\/strong>&nbsp;Before we proceed to create a MySQL user and database on our Raspberry Pi, we must first log back into the MySQL command-line tool.<\/p>\n\n\n\n<p>Run the following command to log in to the MySQL command line. You will be prompted to enter the password for the \u201c<strong>root<\/strong>\u201d account that you set up earlier.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo mysql -u root -p<\/code><\/pre>\n\n\n\n<p><strong>2.<\/strong>&nbsp;Let\u2019s start by creating a MySQL database using the following command.<\/p>\n\n\n\n<p>This command is super simple and is just \u201c<code>CREATE DATABASE<\/code>\u201d followed by the name that you want to give the database.<\/p>\n\n\n\n<p>In our example, we will be calling this database \u201c<code>exampledb<\/code>\u201c.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>CREATE DATABASE exampledb;<\/code><\/pre>\n\n\n\n<p><strong>3.<\/strong>&nbsp;Next, we will create a MySQL user that we will assign to our new database. We can create this user by running the following command.<\/p>\n\n\n\n<p>For this example, we will be calling the user \u201c<code>exampleuser<\/code>\u201d and giving it the password \u201c<code>pimylifeup<\/code>\u201c. When creating your own, make sure you replace both of these.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>CREATE USER 'exampleuser'@'localhost' IDENTIFIED BY 'pimylifeup';<\/code><\/pre>\n\n\n\n<p><strong>4.<\/strong>&nbsp;With the user created, we can now go ahead and grant all privileges to the user so that it can interact with the database.<\/p>\n\n\n\n<p>This command will grant all permissions to our \u201c<code>exampleuser<\/code>\u201d for all tables within our \u201c<code>exampledb<\/code>\u201d database.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>GRANT ALL PRIVILEGES ON exampledb.* TO 'exampleuser'@'localhost';<\/code><\/pre>\n\n\n\n<p><strong>5.<\/strong>&nbsp;The final thing we need to do for both our MySQL database and user to be finalized is to flush the privilege table. Without flushing the privilege table, the new user won\u2019t be able to access the database.<\/p>\n\n\n\n<p>We can do this by running the following command.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>FLUSH PRIVILEGES;<\/code><\/pre>\n\n\n\n<p>If you rather not use the command line to administrate your databases then you can always\u00a0install PHPMyAdmin instead.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Installing the PHP MySQL Connector<\/h3>\n\n\n\n<p><strong>1.<\/strong>&nbsp;If you intend on using a MySQL database from PHP, you will need to make sure that you have the module installed.<\/p>\n\n\n\n<p>You can install the MySQL connector for PHP to your Raspberry Pi by running the following command.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>sudo apt install php-mysql<\/code><\/pre>\n\n\n\n<p>As I mentioned earlier, there are\u00a0many projects\u00a0where a database will come in handy. Most modern websites will require a database to be able to function correctly.<\/p>\n\n\n\n<p>At this point of the tutorial you should now have a MySQL server up and running on your Raspberry Pi. If you have ran into any issues feel free to drop a comment in the comments section below.<\/p>\n\n\n\n<p class=\"has-small-font-size\"><em>I need to thanks<\/em><a href=\"https:\/\/littlebigbyte.com\/\">\u00a0Little Big Byte<\/a><em>, a small business located in Hobart, Tasmania, Australia for giving this great tutorial. It really works!<\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this Raspberry Pi MYSQL tutorial, we will be showing you how to install and configure the MySQL server on your Pi. MySQL is one of the world\u2019s most popular relational database system and is a common inclusion in most&nbsp;LAMP&nbsp;( Linux, Apache, MYSQL, and PHP) stacks. It is one of the pieces of technology that [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-103","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/koner.mywire.org\/index.php\/wp-json\/wp\/v2\/pages\/103","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/koner.mywire.org\/index.php\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/koner.mywire.org\/index.php\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/koner.mywire.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/koner.mywire.org\/index.php\/wp-json\/wp\/v2\/comments?post=103"}],"version-history":[{"count":6,"href":"https:\/\/koner.mywire.org\/index.php\/wp-json\/wp\/v2\/pages\/103\/revisions"}],"predecessor-version":[{"id":130,"href":"https:\/\/koner.mywire.org\/index.php\/wp-json\/wp\/v2\/pages\/103\/revisions\/130"}],"wp:attachment":[{"href":"https:\/\/koner.mywire.org\/index.php\/wp-json\/wp\/v2\/media?parent=103"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}