Category Archives: Databases

How to Easily See Who’s Connected to Your MySQL Server

How to Easily See Who’s Connected to Your MySQL Server
I’m posting this here since it has been useful for me, and the blog is a nice place to keep public notes.
If you have servers which have multiple application servers connected to them, you often need to see things like who’s connected, how many connections they have, and which users. Using SHOW PROCESSLIST doesn’t work that well, since it gives you a row for each server.

What we want is an output similar to this:

+—————–+—————–+———-+
| host_short | users | count(*) |
+—————–+—————–+———-+
| slave1 | repl | 1 |
| slave2 | repl | 1 |
| localhost | event_scheduler | 1 |
| 111.111.222.111 | root, foo | 2 |
| 111.111.222.222 | appuser, bar | 3 |
| 111.111.222.333 | appuser, moshe | 9 |
+—————–+—————–+———-+

And it is achieved using a simple query such as this one:

SELECT SUBSTRING_INDEX(host, ‘:’, 1) AS host_short,
GROUP_CONCAT(DISTINCT USER) AS users,
COUNT(*)
FROM information_schema.processlist
GROUP BY host_short
ORDER BY COUNT(*),
host_short;

A final note: I’m not sure what version of MySQL this query needs to function, but it works great on MySQL 5.5, and should work just as well on MySQL 5.1.

Credit :http://blog.shlomoid.com/2011/08/how-to-easily-see-whos-connected-to.html

mysql2sqlite.sh

#!/bin/sh

# Converts a mysqldump file into a Sqlite 3 compatible file. It also extracts the MySQL `KEY xxxxx` from the
# CREATE block and create them in separate commands _after_ all the INSERTs.

# Awk is choosen because it’s fast and portable. You can use gawk, original awk or even the lightning fast mawk.
# The mysqldump file is traversed only once.

# Usage: $ ./mysql2sqlite mysqldump-opts db-name | sqlite3 database.sqlite
# Example: $ ./mysql2sqlite –no-data -u root -pMySecretPassWord myDbase | sqlite3 database.sqlite

# Thanks to and @artemyk and @gkuenning for their nice tweaks.

mysqldump –compatible=ansi –skip-extended-insert –compact “$@” | \

awk ‘

BEGIN {
FS=”,$”
print “PRAGMA synchronous = OFF;”
print “PRAGMA journal_mode = MEMORY;”
print “BEGIN TRANSACTION;”
}

# CREATE TRIGGER statements have funny commenting. Remember we are in trigger.
/^\/\*.*CREATE.*TRIGGER/ {
gsub( /^.*TRIGGER/, “CREATE TRIGGER” )
print
inTrigger = 1
next
}

# The end of CREATE TRIGGER has a stray comment terminator
/END \*\/;;/ { gsub( /\*\//, “” ); print; inTrigger = 0; next }

# The rest of triggers just get passed through
inTrigger != 0 { print; next }

# Skip other comments
/^\/\*/ { next }

# Print all `INSERT` lines. The single quotes are protected by another single quote.
/INSERT/ {
gsub( /\\\047/, “\047\047″ )
gsub(/\\n/, “\n”)
gsub(/\\r/, “\r”)
gsub(/\\”/, “\””)
gsub(/\\\\/, “\\”)
gsub(/\\\032/, “\032″)
print
next
}

# Print the `CREATE` line as is and capture the table name.
/^CREATE/ {
print
if ( match( $0, /\”[^\"]+/ ) ) tableName = substr( $0, RSTART+1, RLENGTH-1 )
}

# Replace `FULLTEXT KEY` or any other `XXXXX KEY` except PRIMARY by `KEY`
/^ [^"]+KEY/ && !/^ PRIMARY KEY/ { gsub( /.+KEY/, ” KEY” ) }

# Get rid of field lengths in KEY lines
/ KEY/ { gsub(/\([0-9]+\)/, “”) }

# Print all fields definition lines except the `KEY` lines.
/^ / && !/^( KEY|\);)/ {
gsub( /AUTO_INCREMENT|auto_increment/, “” )
gsub( /(CHARACTER SET|character set) [^ ]+ /, “” )
gsub( /DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP|default current_timestamp on update current_timestamp/, “” )
gsub( /(COLLATE|collate) [^ ]+ /, “” )
gsub(/(ENUM|enum)[^)]+\)/, “text “)
gsub(/(SET|set)\([^)]+\)/, “text “)
gsub(/UNSIGNED|unsigned/, “”)
if (prev) print prev “,”
prev = $1
}

# `KEY` lines are extracted from the `CREATE` block and stored in array for later print
# in a separate `CREATE KEY` command. The index name is prefixed by the table name to
# avoid a sqlite error for duplicate index name.
/^( KEY|\);)/ {
if (prev) print prev
prev=””
if ($0 == “);”){
print
} else {
if ( match( $0, /\”[^"]+/ ) ) indexName = substr( $0, RSTART+1, RLENGTH-1 )
if ( match( $0, /\([^()]+/ ) ) indexKey = substr( $0, RSTART+1, RLENGTH-1 )
key[tableName]=key[tableName] “CREATE INDEX \”” tableName “_” indexName “\” ON \”” tableName “\” (” indexKey “);\n”
}
}

# Print all `KEY` creation lines.
END {
for (table in key) printf key[table]
print “END TRANSACTION;”
}

exit 0

reference:

https://gist.github.com/esperlu/943776#file-mysql2sqlite-sh

$ ./mysql2sqlite.sh -u MyUserName -pMySecretPassWord myDbase | sqlite3 database.sqlite

the used table type doesn’t support fulltext indexes mysql

Are you using InnoDB? The only table type that supports FULLTEXT is MyISAM.

eg
CREATE TABLE gamemech_chat (
id bigint(20) unsigned NOT NULL auto_increment,
from_userid varchar(50) NOT NULL default ’0′,
to_userid varchar(50) NOT NULL default ’0′,
text text NOT NULL,
systemtext text NOT NULL,
timestamp datetime NOT NULL default ’0000-00-00 00:00:00′,
chatroom bigint(20) NOT NULL default ’0′,
PRIMARY KEY (id),
KEY from_userid (from_userid),
FULLTEXT KEY from_userid_2 (from_userid),
KEY chatroom (chatroom),
KEY timestamp (timestamp)
) ENGINE=MyISAM;

http://sqlfiddle.com/#!2/e6635

http://stackoverflow.com/questions/20964269/1214-the-used-table-type-doesnt-support-fulltext-indexes

DROP DATABASE `phpcrawl`;

http://en.wikipedia.org/wiki/Web_crawler

http://scrapy.org/doc/

Database Create and drop

mysql -u [username] -p[password] -e ‘drop database db-name;’

mysqladmin -u[username] -p[password] drop [database]
mysql -u root -e “create database testdb”;

If you create a new database it’s good to create user with permissions only for this database (if anything goes wrong you won’t compromise root user login and password). So everything together will look like this:

mysql -u base_user -pbase_user_pass -e “create database new_db; GRANT ALL PRIVILEGES ON new_db.* TO new_db_user@localhost IDENTIFIED BY ‘new_db_user_pass’”

Where:
base_user is the name for user with all privileges (probably the root)
base_user_pass it’s the password for base_user (lack of space between -p and base_user_pass is important)
new_db is name for newly created database
new_db_user is name for the new user with access only for new_db
new_db_user_pass it’s the password for new_db_user

How to Back Up and Restore a MySQL Database

How to Back Up and Restore a MySQL Database

If you’re storing anything in MySQL databases that you do not want to lose, it is very important to make regular backups of your data to protect it from loss. This tutorial will show you two easy ways to backup and restore the data in your MySQL database. You can also use this process to move your data to a new web server.

Back up From the Command Line (using mysqldump)
Back up your MySQL Database with Compress
Restoring your MySQL Database
Backing Up and Restoring using PHPMyAdmin

Back up From the Command Line (using mysqldump)

If you have shell or telnet access to your web server, you can backup your MySQL data by using the mysqldump command. This command connects to the MySQL server and creates an SQL dump file. The dump file contains the SQL statements necessary to re-create the database. Here is the proper syntax:
$ mysqldump –opt -u [uname] -p[pass] [dbname] > [backupfile.sql]

[uname] Your database username
[pass] The password for your database (note there is no space between -p and the password)
[dbname] The name of your database
[backupfile.sql] The filename for your database backup
[--opt] The mysqldump option

For example, to backup a database named ‘Tutorials’ with the username ‘root’ and with no password to a file tut_backup.sql, you should accomplish this command:
$ mysqldump -u root -p Tutorials > tut_backup.sql

This command will backup the ‘Tutorials’ database into a file called tut_backup.sql which will contain all the SQL statements needed to re-create the database.

With mysqldump command you can specify certain tables of your database you want to backup. For example, to back up only php_tutorials and asp_tutorials tables from the ‘Tutorials’ database accomplish the command below. Each table name has to be separated by space.
$ mysqldump -u root -p Tutorials php_tutorials asp_tutorials > tut_backup.sql

Sometimes it is necessary to back up more that one database at once. In this case you can use the –database option followed by the list of databases you would like to backup. Each database name has to be separated by space.
$ mysqldump -u root -p –databases Tutorials Articles Comments > content_backup.sql

If you want to back up all the databases in the server at one time you should use the –all-databases option. It tells MySQL to dump all the databases it has in storage.
$ mysqldump -u root -p –all-databases > alldb_backup.sql

The mysqldump command has also some other useful options:

–add-drop-table: Tells MySQL to add a DROP TABLE statement before each CREATE TABLE in the dump.

–no-data: Dumps only the database structure, not the contents.

–add-locks: Adds the LOCK TABLES and UNLOCK TABLES statements you can see in the dump file.

The mysqldump command has advantages and disadvantages. The advantages of using mysqldump are that it is simple to use and it takes care of table locking issues for you. The disadvantage is that the command locks tables. If the size of your tables is very big mysqldump can lock out users for a long period of time.

Back up your MySQL Database with Compress

If your mysql database is very big, you might want to compress the output of mysqldump. Just use the mysql backup command below and pipe the output to gzip, then you will get the output as gzip file.
$ mysqldump -u [uname] -p[pass] [dbname] | gzip -9 > [backupfile.sql.gz]

If you want to extract the .gz file, use the command below:
$ gunzip [backupfile.sql.gz]

Restoring your MySQL Database

Above we backup the Tutorials database into tut_backup.sql file. To re-create the Tutorials database you should follow two steps:

Create an appropriately named database on the target machine
Load the file using the mysql command:

$ mysql -u [uname] -p[pass] [db_to_restore] < [backupfile.sql]

Have a look how you can restore your tut_backup.sql file to the Tutorials database.
$ mysql -u root -p Tutorials < tut_backup.sql

To restore compressed backup files you can do the following:
gunzip < [backupfile.sql.gz] | mysql -u [uname] -p[pass] [dbname]

If you need to restore a database that already exists, you’ll need to use mysqlimport command. The syntax for mysqlimport is as follows:
mysqlimport -u [uname] -p[pass] [dbname] [backupfile.sql]

Backing Up and Restoring using PHPMyAdmin

It is assumed that you have phpMyAdmin installed since a lot of web service providers use it. To backup your MySQL database using PHPMyAdmin just follow a couple of steps:

Open phpMyAdmin.
Select your database by clicking the database name in the list on the left of the screen.
Click the Export link. This should bring up a new screen that says View dump of database (or something similar).
In the Export area, click the Select All link to choose all of the tables in your database.
In the SQL options area, click the right options.
Click on the Save as file option and the corresponding compression option and then click the ‘Go’ button. A dialog box should appear prompting you to save the file locally.

Restoring your database is easy as well as backing it up. Make the following:

Open phpMyAdmin.
Create an appropriately named database and select it by clicking the database name in the list on the left of the screen. If you would like to rewrite the backup over an existing database then click on the database name, select all the check boxes next to the table names and select Drop to delete all existing tables in the database.
Click the SQL link. This should bring up a new screen where you can either type in SQL commands, or upload your SQL file.
Use the browse button to find the database file.
Click Go button. This will upload the backup, execute the SQL commands and re-create your database.

 

 

Credit to

http://webcheatsheet.com/sql/mysql_backup_restore.php#mysqldump