snipt

Ctrl+h for KB shortcuts

SQL

MySQL create user, grant privilege

CREATE USER 'newuser'@'localhost' IDENTIFIED BY 'password';
GRANT ALL PRIVILEGES ON my_db.* TO 'newuser'@'localhost';
https://snipt.net/embed/7d3ade302b050a58b4fecda7fcfa83b7/
https://snipt.net/raw/7d3ade302b050a58b4fecda7fcfa83b7/
7d3ade302b050a58b4fecda7fcfa83b7
sql
SQL
3
2016-05-05T02:05:14
True
False
Oct 25, 2015 at 02:38 PM
/api/public/snipt/141931/
mysql-create-user-grant-privilege
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1">1</a> <a href="#L-2">2</a></pre></div></td><td class="code"><div class="highlight"><pre><span id="L-1"><a name="L-1"></a><span class="k">CREATE</span> <span class="k">USER</span> <span class="s1">&#39;newuser&#39;</span><span class="o">@</span><span class="s1">&#39;localhost&#39;</span> <span class="n">IDENTIFIED</span> <span class="k">BY</span> <span class="s1">&#39;password&#39;</span><span class="p">;</span> </span><span id="L-2"><a name="L-2"></a><span class="k">GRANT</span> <span class="k">ALL</span> <span class="k">PRIVILEGES</span> <span class="k">ON</span> <span class="n">my_db</span><span class="p">.</span><span class="o">*</span> <span class="k">TO</span> <span class="s1">&#39;newuser&#39;</span><span class="o">@</span><span class="s1">&#39;localhost&#39;</span><span class="p">;</span> </span></pre></div> </td></tr></table>
mysql, sql

Bash

mysql2 gem with mariadb

$ sudo apt-get install libmariadbd-dev
$ gem install mysql2
https://snipt.net/embed/7714f80e2d8ee14c7c0fd0df6ff3e5a4/
https://snipt.net/raw/7714f80e2d8ee14c7c0fd0df6ff3e5a4/
7714f80e2d8ee14c7c0fd0df6ff3e5a4
bash
Bash
3
2016-05-05T02:05:32
True
False
/api/public/snipt/140788/
mysql2-gem-with-mariadb
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1">1</a> <a href="#L-2">2</a></pre></div></td><td class="code"><div class="highlight"><pre><span id="L-1"><a name="L-1"></a><span class="nv">$ </span>sudo apt-get install libmariadbd-dev </span><span id="L-2"><a name="L-2"></a><span class="nv">$ </span>gem install mysql2 </span></pre></div> </td></tr></table>
gem, mariadb, mysql, ruby

MySQL

Mysql importing errors

SET foreign_key_checks = 0;
https://snipt.net/embed/de729115ea11ac53277b45d415e8d9f9/
https://snipt.net/raw/de729115ea11ac53277b45d415e8d9f9/
de729115ea11ac53277b45d415e8d9f9
mysql
MySQL
1
2016-05-05T02:06:46
True
False
Mar 30, 2014 at 09:22 PM
/api/public/snipt/126066/
mysql-importing-errors
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1">1</a></pre></div></td><td class="code"><div class="highlight"><pre><span id="L-1"><a name="L-1"></a><span class="kt">SET</span> <span class="n">foreign_key_checks</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> </span></pre></div> </td></tr></table>
importing, mysql, sql

Text only

MySQL Find & Replace

update table_name set field_name = replace(field_name,'string_to_find','string_to_replace');
https://snipt.net/embed/91ceec1f408f00ebb1d2f4dbdce72f8d/
https://snipt.net/raw/91ceec1f408f00ebb1d2f4dbdce72f8d/
91ceec1f408f00ebb1d2f4dbdce72f8d
text
Text only
1
2016-05-05T02:07:03
True
False
Mar 18, 2014 at 12:04 PM
/api/public/snipt/122803/
mysql-find-replace
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1">1</a></pre></div></td><td class="code"><div class="highlight"><pre><span id="L-1"><a name="L-1"></a>update table_name set field_name = replace(field_name,&#39;string_to_find&#39;,&#39;string_to_replace&#39;); </span></pre></div> </td></tr></table>
mysql

PHP

Connect to a database

1
2
3
4
5
6
7
8
<?php
	$mysql_host = "localhost";
	$mysql_database = "test";
	$mysql_user = "root";
	$mysql_password = "";
	$socket = mysql_connect($mysql_host, $mysql_user, $mysql_password) or die(mysql_error());
	$db_select= mysql_select_db($mysql_database) or die(mysql_error());
?>
https://snipt.net/embed/4de122eda83cb85058012df995226217/
https://snipt.net/raw/4de122eda83cb85058012df995226217/
4de122eda83cb85058012df995226217
php
PHP
8
2016-05-05T02:07:20
True
False
Mar 03, 2014 at 09:23 PM
/api/public/snipt/118288/
connect-to-a-databse
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1">1</a> <a href="#L-2">2</a> <a href="#L-3">3</a> <a href="#L-4">4</a> <a href="#L-5">5</a> <a href="#L-6">6</a> <a href="#L-7">7</a> <a href="#L-8">8</a></pre></div></td><td class="code"><div class="highlight"><pre><span id="L-1"><a name="L-1"></a><span class="cp">&lt;?php</span> </span><span id="L-2"><a name="L-2"></a> <span class="nv">$mysql_host</span> <span class="o">=</span> <span class="s2">&quot;localhost&quot;</span><span class="p">;</span> </span><span id="L-3"><a name="L-3"></a> <span class="nv">$mysql_database</span> <span class="o">=</span> <span class="s2">&quot;test&quot;</span><span class="p">;</span> </span><span id="L-4"><a name="L-4"></a> <span class="nv">$mysql_user</span> <span class="o">=</span> <span class="s2">&quot;root&quot;</span><span class="p">;</span> </span><span id="L-5"><a name="L-5"></a> <span class="nv">$mysql_password</span> <span class="o">=</span> <span class="s2">&quot;&quot;</span><span class="p">;</span> </span><span id="L-6"><a name="L-6"></a> <span class="nv">$socket</span> <span class="o">=</span> <span class="nb">mysql_connect</span><span class="p">(</span><span class="nv">$mysql_host</span><span class="p">,</span> <span class="nv">$mysql_user</span><span class="p">,</span> <span class="nv">$mysql_password</span><span class="p">)</span> <span class="k">or</span> <span class="k">die</span><span class="p">(</span><span class="nb">mysql_error</span><span class="p">());</span> </span><span id="L-7"><a name="L-7"></a> <span class="nv">$db_select</span><span class="o">=</span> <span class="nb">mysql_select_db</span><span class="p">(</span><span class="nv">$mysql_database</span><span class="p">)</span> <span class="k">or</span> <span class="k">die</span><span class="p">(</span><span class="nb">mysql_error</span><span class="p">());</span> </span><span id="L-8"><a name="L-8"></a><span class="cp">?&gt;</span><span class="x"></span> </span></pre></div> </td></tr></table>
mysql, php

Shell Session

Import sql file to MySQL database using SSH

mysql -p -u DB_USERNAME DB_NAME < PATH/TO/FILE.sql
https://snipt.net/embed/2889d808e26a0fbea9ed3b8e1bfeeecf/
https://snipt.net/raw/2889d808e26a0fbea9ed3b8e1bfeeecf/
2889d808e26a0fbea9ed3b8e1bfeeecf
shell-session
Shell Session
1
2016-05-05T02:07:39
True
False
Feb 17, 2014 at 05:03 PM
/api/public/snipt/114552/
import-sql-file-to-mysql-database-using-ssh
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1">1</a></pre></div></td><td class="code"><div class="highlight"><pre><span id="L-1"><a name="L-1"></a><span class="go">mysql -p -u DB_USERNAME DB_NAME &lt; PATH/TO/FILE.sql</span> </span></pre></div> </td></tr></table>
database, mysql, sql, ssh, terminal

Text only

Create a new MySQL user and grant permissions

1
2
3
CREATE USER 'newuser'@'localhost' IDENTIFIED BY 'password';
GRANT ALL PRIVILEGES ON *.* TO 'newuser'@'localhost';
FLUSH PRIVILEGES;
https://snipt.net/embed/e75d2b07cd56ee71c09da2af27eb7b58/
https://snipt.net/raw/e75d2b07cd56ee71c09da2af27eb7b58/
e75d2b07cd56ee71c09da2af27eb7b58
text
Text only
3
2016-05-05T02:08:20
True
False
Jan 20, 2014 at 07:43 AM
/api/public/snipt/107183/
create-a-new-mysql-user-and-grant-permissions
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1">1</a> <a href="#L-2">2</a> <a href="#L-3">3</a></pre></div></td><td class="code"><div class="highlight"><pre><span id="L-1"><a name="L-1"></a>CREATE USER &#39;newuser&#39;@&#39;localhost&#39; IDENTIFIED BY &#39;password&#39;; </span><span id="L-2"><a name="L-2"></a>GRANT ALL PRIVILEGES ON *.* TO &#39;newuser&#39;@&#39;localhost&#39;; </span><span id="L-3"><a name="L-3"></a>FLUSH PRIVILEGES; </span></pre></div> </td></tr></table>
mysql, user
Copyrighted, illegal, or inappropriate content? Email support@snipt.net.