<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/">
  <channel>
    <title>Database Admin on n3s0 || notes</title>
    <link>https://notes.n3s0.tech/tags/database-admin/</link>
    <description>Recent content in Database Admin on n3s0 || notes</description>
    <generator>Hugo -- gohugo.io</generator>
    <language>en</language>
    <lastBuildDate>Wed, 06 Mar 2024 15:25:01 -0500</lastBuildDate><atom:link href="https://notes.n3s0.tech/tags/database-admin/index.xml" rel="self" type="application/rss+xml" />
    <item>
      <title>Setup Initial Application MySQL Database With SQL Script</title>
      <link>https://notes.n3s0.tech/posts/20240306152501/</link>
      <pubDate>Wed, 06 Mar 2024 15:25:01 -0500</pubDate>
      
      <guid>https://notes.n3s0.tech/posts/20240306152501/</guid>
      <description>Simple MySQL script I wrote for the initial setup of application databases.</description>
      <content:encoded><![CDATA[<p>This is a script I use to build MySQL databases with. Generally on
standalone web or application servers where the database is on the same
server. This can be copied into a text document, edited, and saved as a
.sql file. Allowing you to run it on the SQL database server and
automate the process as explained below.</p>
<p>First it creates the database and names it as the application name. It
can be anything we like. This just makes it obvious to the person
installing the application what the database is used for.</p>
<p>Second it creates a new user with a password. The
&lsquo;application_name&rsquo;@&rsquo;localhost&rsquo; provides indication that the user is
intended to be a service account for that application. Of course, the
script sets a password for the user. When creating the user make sure
you&rsquo;re using a secure password.</p>
<p>Third it&rsquo;s time to get into the grants. Although it would probably be
more secure to go through common applications and the common queries
they make. I&rsquo;m more then happy to read through an application to review
what privileges I need to grant given I have the time. The last section
grants all privileges on the database and it&rsquo;s tables to the application
user we just created. One thing to understand is that this doesn&rsquo;t give
ALL privileges to the databases on the server - that would be horrible
and indicated by <em>.</em> which means all databases and their tables. Our
script here just focuses on the database of the application.</p>
<p>Finally, the script will flush the privileges for the database server.</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-sql" data-lang="sql"><span class="line"><span class="cl"><span class="k">CREATE</span><span class="w"> </span><span class="k">DATABASE</span><span class="w"> </span><span class="n">application_name</span><span class="p">;</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w"></span><span class="k">CREATE</span><span class="w"> </span><span class="k">USER</span><span class="w"> </span><span class="s1">&#39;application_name&#39;</span><span class="o">@</span><span class="s1">&#39;localhost&#39;</span><span class="w"> </span><span class="n">IDENTIFIED</span><span class="w"> </span><span class="k">BY</span><span class="w"> </span><span class="s1">&#39;db_password&#39;</span><span class="p">;</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w"></span><span class="k">GRANT</span><span class="w"> </span><span class="k">ALL</span><span class="w"> </span><span class="k">PRIVILEGES</span><span class="w"> </span><span class="k">ON</span><span class="w"> </span><span class="n">application_name</span><span class="p">.</span><span class="o">*</span><span class="w"> </span><span class="k">TO</span><span class="w"> </span><span class="s1">&#39;application_name&#39;</span><span class="o">@</span><span class="s1">&#39;localhost&#39;</span><span class="p">;</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w"></span><span class="n">FLUSH</span><span class="w"> </span><span class="k">PRIVILEGES</span><span class="p">;</span><span class="w">
</span></span></span></code></pre></div><p>Once this file is created and the edits to the file are complete. Save
the file as somename.sql and run it on the database. On MySQL
specifically on say Ubuntu Server 22.04 LTS the mysql command can be run
as root and the scripts contents can be piped into the database server.</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-sh" data-lang="sh"><span class="line"><span class="cl">sudo mysql -u root &lt; somename.sql
</span></span></code></pre></div><p>Once you&rsquo;ve done this. Login to the MySQL console and run the following
command and you&rsquo;ll see the new database. This will provide a list of the
databases housed on the system.</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-sql" data-lang="sql"><span class="line"><span class="cl"><span class="k">SHOW</span><span class="w"> </span><span class="n">DATABASES</span><span class="p">;</span><span class="w"> 
</span></span></span></code></pre></div><p>To show the updated user. You can use the following command to see the
list of users in the mysql databases user table.</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-sql" data-lang="sql"><span class="line"><span class="cl"><span class="k">SELECT</span><span class="w"> </span><span class="k">User</span><span class="p">,</span><span class="k">Host</span><span class="w"> </span><span class="k">FROM</span><span class="w"> </span><span class="n">mysql</span><span class="p">.</span><span class="k">user</span><span class="p">;</span><span class="w">
</span></span></span></code></pre></div><p>Scripting in SQL makes life easy no matter how simple or complex need
is. Just remember that the more complex it is. The more you&rsquo;ll want to
test. Never run SQL scripts on a production server that you didn&rsquo;t read
or understand. Test within a lab environment first.</p>
]]></content:encoded>
    </item>
    
  </channel>
</rss>
