MySQL: Difference between revisions

From WickyWiki
mNo edit summary
mNo edit summary
Line 37: Line 37:


== GUI's ==
== GUI's ==
=== phpMySqlAdmin ===
* https://www.phpmyadmin.net/


=== MySQL Workbench ===
=== MySQL Workbench ===
Line 44: Line 48:
Easily installed using Ubuntu Software Center.
Easily installed using Ubuntu Software Center.


=== MySQL Workbench show BINARY as character string ===
MySQL Workbench show BINARY as character string. Datatypes in the MediaWiki database are all binary, in order to view the contents do the following:
 
Datatypes in the MediaWiki database are all binary, in order to view the contents do the following:


# Go to Edit > Preferences
# Go to Edit > Preferences
# Choose SQL Queries
# Choose SQL Queries
# Under Query Results, check Treat BINARY/VARBINARY as nonbinary character string
# Under Query Results, check Treat BINARY/VARBINARY as nonbinary character string
=== Emma ===
<syntaxhighlight lang=bash>
sudo apt-get install emma
</syntaxhighlight>


=== MySql Administrator and Query Browser ===
=== MySql Administrator and Query Browser ===
Line 69: Line 65:
sudo service mysql stop
sudo service mysql stop
sudo service mysql start
sudo service mysql start
</syntaxhighlight>
=== SQL Developer ===
As a database tool I would definitely recommend SQL Developer. Although it mainly targets Oracle database and it is quite resource hungry, all its features greatly improve your interaction with the database.
Download the ZIP file from Oracle’s site titled “Oracle SQL Developer for other platforms” you will need an Oracle account:
* http://www.oracle.com/technology/software/products/sql/index.html
Depending on your distribution you might alsow ant to read these:
* https://bugs.launchpad.net/ubuntu/+source/sqldeveloper-package/+bug/985810
** fix: use the patch on make-sqldeveloper-package
* https://bugs.launchpad.net/ubuntu/+source/sqldeveloper-package/+bug/588458
** fix: sudo apt-get install debhelper
We will assume it's name is 'sqldeveloper-3.0.04.34-no-jre.zip'.
Install the tofrodos package and create symbolic links to the tofrodos commands to allow the sqldeveloper-package to work:
<syntaxhighlight lang=bash>
sudo apt-get install tofrodos
sudo ln -s /usr/bin/fromdos /usr/bin/dos2unix
sudo ln -s /usr/bin/todos /usr/bin/unix2dos
</syntaxhighlight>
Install the make-sqldeveloper-package package. You will need to select Ubuntu’s multiverse software repository (not explained here):
<syntaxhighlight lang=bash>
sudo apt-get install sqldeveloper-package
</syntaxhighlight>
Build the .DEB package with the downloaded ZIP file:
<syntaxhighlight lang=bash>
cd LOCATION_OF_ZIP_FILE
mkdir temp
make-sqldeveloper-package -b ./temp ./sqldeveloper-3.0.04.34-no-jre.zip
</syntaxhighlight>
Install the package:
<syntaxhighlight lang=bash>
sudo dpkg -i sqldeveloper_3.0.04.34+0.2.3-1_all.deb
</syntaxhighlight>
Start SQL Developer:
<syntaxhighlight lang=bash>
sqldeveloper
</syntaxhighlight>
Look for the full pathname of a jvm installation with:
<syntaxhighlight lang=bash>
update-alternatives --query java
</syntaxhighlight>
Type full pathname of a jvm installation, when you use OpenJDK it would look like:
<syntaxhighlight lang=bash>
/usr/lib/jvm/java-6-openjdk-amd64
</syntaxhighlight>
This setting will be stored in your home folder:
<syntaxhighlight lang=bash>
$home/.sqldeveloper/jdk
</syntaxhighlight>
If you have problems with screen redraw in SQL Developer, then change the theme to Classic:
SQL Developer -> Tools -> Preferences -> Environment -> Theme -> Classic
To connect to a MySQL database you need the JDBC Driver for MySQL (Connector/J). Download from:
* http://www.mysql.com/products/connector/
Within the downloaded archive a file is contained, something like 'mysql-connector-java-5.1.18-bin.jar'.
Copy it to some logical location:
<syntaxhighlight lang=bash>
sudo mkdir /usr/share/sqldeveloper/sqldeveloper/mysql
sudo cp mysql-connector-java-5.1.18-bin.jar /usr/share/sqldeveloper/sqldeveloper/mysql
</syntaxhighlight>
Add the driver in SQL Developer:
* SQL Developer -> Tools -> Database -> Third-party JDBC Drivers -> Add Entry
* then browse to '/usr/share/sqldeveloper/sqldeveloper/mysql'
* select 'mysql-connector-java-5.1.18-bin.jar'
You can now add a new MySQL connection in SQL Developer with servername, port, username and password.
Remove the sqldeveloper-package +- 90MB:
<syntaxhighlight lang=bash>
sudo apt-get remove sqldeveloper-package
</syntaxhighlight>
</syntaxhighlight>



Revision as of 20:48, 29 December 2017


Installation

sudo apt-get install mysql-server

Configure MySQL root password

sudo -i
service mysql stop
mysqld_safe --skip-grant-tables &
mysql -u root

 USE mysql
 update user set password=PASSWORD("**mysql-root-pwd**") where user='root';
 flush privileges;
 \q

service mysql start

Test:

mysql -u root -p 

 Enter password: **mysql-root-pwd**
 \q

exit

GUI's

phpMySqlAdmin

MySQL Workbench

Easily installed using Ubuntu Software Center.

MySQL Workbench show BINARY as character string. Datatypes in the MediaWiki database are all binary, in order to view the contents do the following:

  1. Go to Edit > Preferences
  2. Choose SQL Queries
  3. Under Query Results, check Treat BINARY/VARBINARY as nonbinary character string

MySql Administrator and Query Browser

sudo apt-get install mysql-gui-tools-common

Restart:

sudo service mysql stop
sudo service mysql start

Backup

The GUI (mysql-gui-tools-common) allow you to schedule backups as well.

You can use a simple command-line to create a compressed backup. The backup contains SQL statements that need to be executed to restore the database.

The following will backup all databases to one file.

mysqldump --user=root --password=**mysql-root-pwd** -A | gzip > /path/to/alldatabases.sql.gz

You can backup a single or a number of named databases with:

mysqldump --user=root --password=**mysql-root-pwd** --databases db1 db2 db3 | gzip > /path/to/databases.sql.gz

Protect your backup with a password (zip):

mysqldump --user=root --password=**mysql-root-pwd** --databases db1 | zip --password "**zip-pwd**" > /path/to/database_db1.sql.zip

Note that your commandline could be visible to other users along with the password.

Schedule a backup with crontab

To schedule a command we need to modify the crontab file:

sudo gedit /etc/crontab

A command is run when date/time matches the pattern. An asterisk (*) is used to match all. Examples:

# minute - hour - day - month - day of week - USER - COMMAND
* * * * * wilbert every_minute.sh
0 * * * * wilbert every_whole_hour.sh
0 0 * * * wilbert every_day_at_midnight.sh
0 0 1 * * wilbert every_1st_of_the_month_at_midnight.sh

We will run a backup check (mysqldump.sh) every hour:

0 * * * * root /root/mysqldump.sh

Using a subscript we can shield the password in the script from unwanted viewers. This script will run every hour and ensures only one backup is made each day:

sudo gedit /root/mysqldump.sh
#!/bin/bash
dumpfile="$home/mysql_$(date '+%Y-%m-%d').sql.gz"
if [ ! -f "$dumpfile" ] ; then
  mysqldump -u root -p**mysql-root-pwd** --all-databases | gzip > $dumpfile
fi

Make executable and protect it from being read by anyone but root:

sudo chmod 0700 /root/mysqldump.sh

Restore

Note: make a new full backup before you try to restore something.

Fully restore all databases:

mysql --user=root --password=**mysql-root-pwd** < alldatabases.sql

You can also start mysql prompt and run the script from there, you don't have to enter the password on the commandline:

sudo mysql --user=root

mysql> source alldatabases.sql

Restore one database

Only restore database mydatabase, MySQL scans for the USE statement in the sql-file to select the correct part.

mysql --user=root --password=**mysql-root-pwd** --one-database mydatabase < alldatabases.sql

Extract database using sed

Extract header and database respectively:

sed -n '/^-- MySQL dump/,/^-- Current Database: `/p' alldatabases.sql | head -n -1 > mydatabase.sql
sed -n '/^-- Current Database: `mydatabase`/,/^-- Current Database: `/p' alldatabases.sql >> mydatabase.sql

Extract database using a text editor

The backup file (in this example: alldatabases.sql) is really nothing more than a sequence of SQL statements. To restore a single database you need to select the continuous part that is the particular database.

For big text files such as these it is better to use a text editor like nano. Start a terminal and type:

nano alldatabases.sql

You can find the start of a database by searching for the text:

-- Current Database:

Use the following nano-shortcuts to remove the text that you won't use. Leave the header (the part that comes before the first database):

 ALT-A  start selection
 CTRL-W find text
 ESC \  first line
 ESC /  last line
 CTRL-K remove selected text
 CTRL-X exit, save

After you are done save the file 'mydatabase.sql'.

Database MySql /MariaDB access from other machine

For example with MySql workbench:

Problem: can't connect to the server

Comment bind-address to allow access from other machines:

sudo nano /etc/mysql/mariadb.conf.d/50-server.cnf
...
#bind-address = 127.0.0.1
...

Restart database service:

sudo systemctl restart mariadb

Problem: Host '...' is not allowed to connect to this MariaDB server

Create 'backup' user and allow local network access.

Note: in this example we allow only IP addresses starting with '192.168.1.'.

sudo -i
mysql -u root mysql -p
CREATE USER 'backup'@'192.168.1.%' IDENTIFIED BY '**pwd**';
GRANT select, show view, trigger, lock tables, reload, file on schema1.*, schema2.* to 'backup'@'192.168.1.%';
FLUSH PRIVILEGES;
\q

Note: this example allows a mysqldump of schema1 and schema2.

Info:

Queries

Ten largest tables

SELECT CONCAT(table_schema, '.', table_name),
       CONCAT(ROUND(table_rows / 1000000, 2), 'M') rows,
       CONCAT(ROUND(data_length / ( 1024 * 1024 * 1024 ), 2), 'G') DATA,
       CONCAT(ROUND(index_length / ( 1024 * 1024 * 1024 ), 2), 'G') idx,
       CONCAT(ROUND(( data_length + index_length ) / ( 1024 * 1024 * 1024 ), 2), 'G') total_size,
       ROUND(index_length / data_length, 2) idxfrac
FROM   information_schema.TABLES
ORDER  BY data_length + index_length DESC
LIMIT  10;

Usage per schema

SELECT count(*) tables,
    table_schema,concat(round(sum(table_rows)/1000000,2),'M') rows,
    concat(round(sum(data_length)/(1024*1024*1024),2),'G') data,
    concat(round(sum(index_length)/(1024*1024*1024),2),'G') idx,
    concat(round(sum(data_length+index_length)/(1024*1024*1024),2),'G') total_size,
    round(sum(index_length)/sum(data_length),2) idxfrac
    FROM information_schema.TABLES
    GROUP BY table_schema
    ORDER BY sum(data_length+index_length) DESC LIMIT 10
    ;