<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Tomek Pryjma</title>
    <description>The latest articles on Forem by Tomek Pryjma (@tomekpryjma).</description>
    <link>https://forem.com/tomekpryjma</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F476024%2Fafa5d524-b17f-4c44-bcb7-c05857b5e019.jpeg</url>
      <title>Forem: Tomek Pryjma</title>
      <link>https://forem.com/tomekpryjma</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/tomekpryjma"/>
    <language>en</language>
    <item>
      <title>Create a database without PhpMyAdmin</title>
      <dc:creator>Tomek Pryjma</dc:creator>
      <pubDate>Sun, 27 Sep 2020 11:19:18 +0000</pubDate>
      <link>https://forem.com/tomekpryjma/create-a-database-without-phpmyadmin-a8</link>
      <guid>https://forem.com/tomekpryjma/create-a-database-without-phpmyadmin-a8</guid>
      <description>&lt;p&gt;Sometimes you may find that you have to create a MySQL/MariaDB database without the use of a nice GUI provided by PhpMyAdmin.&lt;br&gt;
This is the 10th circle of hell dedicated to sysadmins and unlucky souls who just happened to be at the wrong place at the wrong time.&lt;br&gt;
Good news is that this is the only circle you can actually leave and redeem yourself.&lt;br&gt;
I will show you how.&lt;/p&gt;


&lt;h2&gt;
  
  
  What you will need
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Server SSH access&lt;/li&gt;
&lt;li&gt;Either the MySQL root username and password or a MySQL user with the ability to &lt;code&gt;CREATE&lt;/code&gt;, &lt;code&gt;INSERT&lt;/code&gt;, &lt;code&gt;RELOAD&lt;/code&gt; and &lt;code&gt;ALTER&lt;/code&gt; inside the MySQL command line interface (CLI).&lt;/li&gt;
&lt;li&gt;And as pointed out by &lt;a href="https://dev.to/moopet"&gt;Ben Sinclair&lt;/a&gt;, you may also need to know which port you have to connect on. MySQL typically uses 3306 by default but always best to check to avoid mishaps.&lt;/li&gt;
&lt;/ul&gt;


&lt;h2&gt;
  
  
  Let's begin
&lt;/h2&gt;

&lt;p&gt;After you access the server you must get yourself into MySQL's own command line interface (CLI).&lt;/p&gt;

&lt;p&gt;Do that by running: &lt;code&gt;mysql -u root -p&lt;/code&gt;&lt;/p&gt;
&lt;h4&gt;
  
  
  Breakdown of command
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;mysql&lt;/code&gt; -  tells the server we want to log into MySQL's CLI.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;-u root&lt;/code&gt; - tells the &lt;code&gt;mysql&lt;/code&gt; command which user to log into MySQL CLI with, in my case I'll be using &lt;code&gt;root&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;-p&lt;/code&gt; - tells the command to prompt us for a password after we run it.&lt;/li&gt;
&lt;li&gt;As mentioned before if MySQL is using a port other than the default 3306 you will need to provide the command with that port by adding &lt;code&gt;--port=&lt;/code&gt; followed by the port number.&lt;/li&gt;
&lt;/ul&gt;


&lt;h2&gt;
  
  
  In the CLI
&lt;/h2&gt;

&lt;p&gt;What you should see after running the command in the step above is a variation of the following:&lt;/p&gt;



&lt;p&gt;&lt;strong&gt;MySQL&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 265976
Server version: 5.5.62-0+deb8u1 (Debian)

Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;






&lt;p&gt;&lt;strong&gt;MariaDB&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 8
Server version: 10.1.41-MariaDB-0+deb9u1 Debian 9.9

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h2&gt;
  
  
  Creating a database
&lt;/h2&gt;

&lt;p&gt;This is simple enough, we do this:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;MariaDB 10.1.3+ &amp;amp; MySQL 5.5+&lt;/strong&gt;&lt;br&gt;
&lt;code&gt;CREATE DATABASE t800;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;small&gt;&lt;strong&gt;Output you should see&lt;/strong&gt;&lt;/small&gt;&lt;br&gt;
&lt;small&gt;&lt;code&gt;Query OK, 1 row affected (0.00 sec)&lt;/code&gt;&lt;/small&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  Every database needs a user
&lt;/h2&gt;

&lt;p&gt;All databases need their own user so we create one like so:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;MariaDB 10.1.3+ &amp;amp; MySQL 5.5+&lt;/strong&gt;&lt;br&gt;
&lt;code&gt;CREATE USER arnold@localhost IDENTIFIED BY 'dAs2WnZD^j3D1Nxpu%ES';&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;small&gt;&lt;strong&gt;Output you should see&lt;/strong&gt;&lt;/small&gt;&lt;br&gt;
&lt;small&gt;&lt;code&gt;Query OK, 0 rows affected (0.00 sec)&lt;/code&gt;&lt;/small&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  Breakdown of command
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;We provide &lt;code&gt;arnold@localhost&lt;/code&gt; to the &lt;code&gt;CREATE USER&lt;/code&gt; statement as a means of telling the system that we want to create a user whose name is &lt;code&gt;arnold&lt;/code&gt; and who can only connect from the same server he is on, i.e. &lt;code&gt;localhost&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;IDENTIFIED BY&lt;/code&gt; part lets us set a password for our user. I strongly suggest you use a &lt;a href="https://www.lastpass.com/password-generator"&gt;password generator&lt;/a&gt; to help out with this.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Granting privileges
&lt;/h2&gt;

&lt;p&gt;Once &lt;code&gt;arnold&lt;/code&gt; is created we will need to set some ground rules about how &lt;code&gt;arnold&lt;/code&gt; is allowed to interact with the &lt;code&gt;t800&lt;/code&gt; database.&lt;/p&gt;

&lt;p&gt;We give him a set of privileges, like this:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;MariaDB 10.1.3+ &amp;amp; MySQL 5.5+&lt;/strong&gt;&lt;br&gt;
&lt;code&gt;GRANT ALL PRIVILEGES ON t800.* TO arnold@localhost;&lt;/code&gt;&lt;br&gt;
&lt;small&gt;&lt;strong&gt;Output you should see&lt;/strong&gt;&lt;/small&gt;&lt;br&gt;
&lt;small&gt;&lt;code&gt;Query OK, 0 rows affected (0.00 sec)&lt;/code&gt;&lt;/small&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;FLUSH PRIVILEGES;&lt;/code&gt;&lt;br&gt;
&lt;small&gt;&lt;strong&gt;Output you should see&lt;/strong&gt;&lt;/small&gt;&lt;br&gt;
&lt;small&gt;&lt;code&gt;Query OK, 0 rows affected (0.00 sec)&lt;/code&gt;&lt;/small&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  Breakdown of command
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;We tell the system to give &lt;code&gt;arnold&lt;/code&gt; all the permissions he may need to properly interact with the database. That includes reading from it, writing to it, deleting from it etc.&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;FLUSH PRIVILEGES&lt;/code&gt; statement makes it so that our &lt;code&gt;GRANT ALL&lt;/code&gt; statement has an effect. Essentially telling the system to refresh everything it currently knows about its current users and their privileges.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Finishing touches
&lt;/h2&gt;

&lt;p&gt;Chances are, if you're creating a database you probably don't want it to be empty.&lt;br&gt;
So considering we don't have PhpMyAdmin access, we will need to import a database via the command line.&lt;/p&gt;

&lt;p&gt;You can quit the MySQL/MariaDB CLI by typing &lt;code&gt;quit&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;You will need to upload the database file onto your server into the directory you are currently in (for ease of typing later on) you can do this in two ways:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Drag and drop the file via FTP&lt;/li&gt;
&lt;li&gt;Run &lt;code&gt;scp /your/local/computer/database.sql user@server-name-or-ip:/path/to/directory/database.sql&lt;/code&gt;. &lt;code&gt;scp&lt;/code&gt; stands for &lt;strong&gt;s&lt;/strong&gt;ecure &lt;strong&gt;c&lt;/strong&gt;o&lt;strong&gt;p&lt;/strong&gt;y and will transfer the file via SSH if FTP is not available to you.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now that the file is on you server, the next command assumes the &lt;code&gt;.sql&lt;/code&gt; file is in the same directory that you are in on the command line:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;mysql -u root -p t800 &amp;lt; database.sql&lt;/code&gt;&lt;/p&gt;




&lt;p&gt;And that's about it! We have successfully escaped from the 10th circle of hell.&lt;/p&gt;

</description>
      <category>linux</category>
      <category>mysql</category>
      <category>database</category>
    </item>
  </channel>
</rss>
