Mysql: Difference between revisions

From Objectif Client Inc
Jump to navigation Jump to search
(Created page with "== Syntax == === Data Manipulation === ==== Basic ==== * Select <syntaxhighlight lang="mysql"> SELECT columna, columnb FROM mytable; SELECT columna as alias1, columnb as alias...")
 
 
(12 intermediate revisions by one other user not shown)
Line 1: Line 1:
== Installation ==
Web site [https://www.mysql.com/ MySql]
=== Package Installation ===
* MySql Server
<syntaxhighlight lang="bash">
apt-get install mysql-server
</syntaxhighlight>
* Recommended
<syntaxhighlight lang="bash">
apt-get install mysql-workbench phpmyadmin
</syntaxhighlight>
=== Setup ===
* To be accessible from another server remove the bind from /etc/mysql/mysql.conf.d/mysqld.cnf
<syntaxhighlight lang="bash">
###Nr001 bind-address          = 127.0.0.1
</syntaxhighlight>
== Syntax ==
== Syntax ==
=== Data Manipulation ===
=== Data Manipulation ===
Line 19: Line 41:
<syntaxhighlight lang="mysql">
<syntaxhighlight lang="mysql">
SELECT CONCAT(last_name,', ',first_name) AS full_name FROM mytable;
SELECT CONCAT(last_name,', ',first_name) AS full_name FROM mytable;
</syntaxhighlight>
===== Joint =====
<syntaxhighlight lang="mysql">
SELECT m.MenuName, o.MenuOption FROM `menuoption` o left outer join `menu` m  on o.MenuId=m.id;
</syntaxhighlight>
</syntaxhighlight>


Line 29: Line 56:
</syntaxhighlight>
</syntaxhighlight>
===== Create User =====
===== Create User =====
<syntaxhighlight lang="mysql">
CREATE USER username IDENTIFIED BY 'userpassword';
</syntaxhighlight>
<syntaxhighlight lang="mysql">
<syntaxhighlight lang="mysql">
GRANT ALL PRIVILEGES ON mydatabase.* TO myuser@localhost IDENTIFIED BY 'myuser_password';
GRANT ALL PRIVILEGES ON mydatabase.* TO myuser@localhost IDENTIFIED BY 'myuser_password';
</syntaxhighlight>
</syntaxhighlight>
===== Create Table =====
===== Create Table =====
<syntaxhighlight lang="mysql">
<syntaxhighlight lang="mysql">
</syntaxhighlight>
</syntaxhighlight>


==== Alter Table ====
===== Alter Table =====
<syntaxhighlight lang="mysql">
</syntaxhighlight>
 
 
==== Special Case ====
===== TimeStampe =====
<syntaxhighlight lang="mysql">
<syntaxhighlight lang="mysql">
CREATE TABLE test_table(
  Id INTEGER NOT NULL AUTO_INCREMENT PRIMARY KEY,
  CreatedTime TIMESTAMP DEFAULT '0000-00-00 00:00:00',
  UpdatedTime TIMESTAMP DEFAULT NOW() ON UPDATE NOW()
);
</syntaxhighlight>
== Maintenance ==
=== Binary File ===
* Binary file are usefull to recuperate a database in any point of time
* There are usefull for replication
* But as all informationare written into the logfile before updating the database, you may expect an impact on the perfomance
==== Setup ====
<syntaxhighlight lang="bash">
log_bin                = /var/log/mysql/mysql-bin.log
expire_logs_days        = 10
max_binlog_size        = 250M
binlog_do_db            = include_database_name
binlog_format          = 'MIXED'
</syntaxhighlight>
The binary logs in mysql-bin.index is expected to be <span style="color:red">numerically consecutive</span>. If the binary logs are not numerically consecutive, log rotation is <span style="color:red">disabled</span>.
==== Script for removing binary file  ====
===== Config File =====
<syntaxhighlight lang="bash">
#/bin/bash
#DB Connection paramters
DBHOST=0.0.0.0
DBNAME=test
DBUSER=dbuser
DBUSERPWD=userpassword
DBPORT=3306
</syntaxhighlight>
</syntaxhighlight>
===== Script =====
<syntaxhighlight lang="bash">
#!/bin/bash
CONFIG_FILE='/exploit/sys_mysql_purgelog.conf'
DBHOST=$(cat $CONFIG_FILE | grep -w DBHOST | awk -F'=' '{ print $2 }' )
DBUSER=$(cat $CONFIG_FILE | grep -w DBUSER | awk -F'=' '{ print $2 }' )
DBUSERPWD=$(cat $CONFIG_FILE | grep -w DBUSERPWD | awk -F'=' '{ print $2 }' )
DBPORT=$(cat $CONFIG_FILE | grep -w DBPORT | awk -F'=' '{ print $2 }' )
purgedt=$(mysql -h $DBHOST -u $DBUSER -P $DBPORT --password=$DBUSERPWD -e "select date_sub(current_date(), interval 7 day)" -s -N )
echo "Purge binary logs before $purgedt started at $(date +%m%d%y_%H:%M)"
str="PURGE BINARY LOGS BEFORE '"$purgedt"'"
mysql $DB -h $DBHOST -u $DBUSER -P $DBPORT --password=$DBUSERPWD -e "$str"
success=$?
if [ $success = 0 ];
then
  echo "Purge binary logs before $purgedt completed at $(date +%m%d%y_%H:%M)"
else
  echo "!!!Failed!!!"
fi    #if [ $success = 0 ] then
exit 0
</syntaxhighlight>
== Backup Restore ==
Backup all database
<syntaxhighlight lang="mysql">mysqldump --all-databases --single-transaction --user=root --password > all-database.sql</syntaxhighlight>


Backup a specific database


===
<syntaxhighlight lang="mysql">mysqldump -h server -p database --add-drop-table --single-transaction --user=root --password</syntaxhighlight>

Latest revision as of 16:13, 3 October 2020

Installation

Web site MySql

Package Installation

  • MySql Server
apt-get install mysql-server
  • Recommended
apt-get install mysql-workbench phpmyadmin

Setup

  • To be accessible from another server remove the bind from /etc/mysql/mysql.conf.d/mysqld.cnf
###Nr001 bind-address           = 127.0.0.1


Syntax

Data Manipulation

Basic

  • Select
SELECT columna, columnb FROM mytable;
SELECT columna as alias1, columnb as alias2 FROM mytable;


Special

  • Dummy Table
SELECT 1+1;
SELECT 1+1 FROM DUAL;
  • Concatenate
SELECT CONCAT(last_name,', ',first_name) AS full_name FROM mytable;
Joint
SELECT m.MenuName, o.MenuOption FROM `menuoption` o left outer join `menu` m   on o.MenuId=m.id;

Data Definition

Basic

Create Database
CREATE DATABASE mydatabase;
CREATE DATABASE mydatabase CHARACTER SET utf8 COLLATE utf8_general_ci;
Create User
CREATE USER username IDENTIFIED BY 'userpassword';
GRANT ALL PRIVILEGES ON mydatabase.* TO myuser@localhost IDENTIFIED BY 'myuser_password';
Create Table
Alter Table


Special Case

TimeStampe
CREATE TABLE test_table( 
  Id INTEGER NOT NULL AUTO_INCREMENT PRIMARY KEY, 
  CreatedTime TIMESTAMP DEFAULT '0000-00-00 00:00:00', 
  UpdatedTime TIMESTAMP DEFAULT NOW() ON UPDATE NOW() 
);

Maintenance

Binary File

  • Binary file are usefull to recuperate a database in any point of time
  • There are usefull for replication
  • But as all informationare written into the logfile before updating the database, you may expect an impact on the perfomance

Setup

log_bin                 = /var/log/mysql/mysql-bin.log
expire_logs_days        = 10
max_binlog_size         = 250M
binlog_do_db            = include_database_name
binlog_format           = 'MIXED'

The binary logs in mysql-bin.index is expected to be numerically consecutive. If the binary logs are not numerically consecutive, log rotation is disabled.

Script for removing binary file

Config File
#/bin/bash
#DB Connection paramters
DBHOST=0.0.0.0
DBNAME=test
DBUSER=dbuser
DBUSERPWD=userpassword
DBPORT=3306
Script
#!/bin/bash

CONFIG_FILE='/exploit/sys_mysql_purgelog.conf'

DBHOST=$(cat $CONFIG_FILE | grep -w DBHOST | awk -F'=' '{ print $2 }' )
DBUSER=$(cat $CONFIG_FILE | grep -w DBUSER | awk -F'=' '{ print $2 }' )
DBUSERPWD=$(cat $CONFIG_FILE | grep -w DBUSERPWD | awk -F'=' '{ print $2 }' )
DBPORT=$(cat $CONFIG_FILE | grep -w DBPORT | awk -F'=' '{ print $2 }' )

purgedt=$(mysql -h $DBHOST -u $DBUSER -P $DBPORT --password=$DBUSERPWD -e "select date_sub(current_date(), interval 7 day)" -s -N )
echo "Purge binary logs before $purgedt started at $(date +%m%d%y_%H:%M)"
str="PURGE BINARY LOGS BEFORE '"$purgedt"'"
mysql $DB -h $DBHOST -u $DBUSER -P $DBPORT --password=$DBUSERPWD -e "$str"

success=$?

if [ $success = 0 ];
then
   echo "Purge binary logs before $purgedt completed at $(date +%m%d%y_%H:%M)"
else
   echo "!!!Failed!!!"
fi     #if [ $success = 0 ] then

exit 0

Backup Restore

Backup all database

mysqldump --all-databases --single-transaction --user=root --password > all-database.sql

Backup a specific database

mysqldump -h server -p database --add-drop-table --single-transaction --user=root --password