MySQL

Open source SQL database often used on Linux with Apache and PHP (LAMP)

Tuning MySQL

Update - New Innodb tuning section added below.  See also Common MySQL Queries.

If tmp_table_size is not equal to max_heap_table_size then change them to be the same.

If Created_tmp_disk_tables is big, you may want to increase the tmp_table_size value to cause
temporary tables to be memory-based instead of disk-based.

Adding Swap Space to Linux without Rebooting

Today I got an error in MySQL "InnoDB: Error: connot allocate   bytes... you should increase swap"
I wanted to add more swap you can do it with a file like below or if you have space create a partition or logical volume (lv).

So, here's a simple step to add a swap space (Read more for details):

mysql 145 Table '' is marked as crashed and should be repaired

Got error: 145: Table {sometable} is marked as crashed and should be repaired

I got the this error when trying to do a sqldump of my database.  To fix this error use the utility mysqlcheck from the command line.

This statment shows that the table is marked as crashed:

# mysqlcheck -uuser -ppass database table
mediawiki.searchindex
warning  : Table is marked as crashed
status   : OK

This statement will repair the table:

# mysqlcheck -uroot -pvisitor --auto-repair database table
database.table                              OK

MySQL What is happening?

The 5.1 release was very slow getting released

  • Percona did work on innodb XtraDB plugin
  • Google did patches
  • Facebook did a patch set
  • Monty Program - New company started by the founder of MySQL, Forked Maria

MySQL 5.1 Sun/Oracle version we now consider it stable

Oracle bought MySQL, and it has gone OK so far.

Use the Innodb Plugin is much better performance than the included version.

MySQL 5.4

Join Optimizations, Stored Procedures, General Scalability Improvements

MySQL 5.5

  • SemiSync Replication
  • Perf Schema
  • MRS (multiple Rollback Segments), InnoDB Recovery, Delete Buffer, MDL, Split InnoDB Buffer Pools (based on the number of buffer pools you want), InnoDB as Default

PostgreSQL Commands

Useful SQL for DBAs

List all tables in the current database:

=> SELECT tablename FROM pg_tables where tablename not like 'pg_%';

Limit rows returned as in where rownum <= 10

=> select tablename from pg_tables limit 10;

List tables in one schema

=> select tablename from pg_tables where tableowner='qedit';

List running queries

=> select * from pg_stat_activity;

List database activity

PostgreSQL Procedural Langauage - plpgsql

Run the command to install procedural languages or you will get:
ERROR: language "plpgsql" does not exist

Connect as the superuser

bash-3.2$

Run the command
bash-3.2$ createlang -d movecms1 plpgsql

Login as regular user
bash-3.2$ psql -U qedit -d movecms1

Now create the type to return the values

PostgresQL on Fedora

Installing PostgreSQL on Fedora Core

This will install the PostgreSQL database server and the component required to write PHP scripts that communicate with postgres. We use yum to handle dependencies and gather all of the required packages. For more information on PostgreSQL, see http://www.postgresql.org

1. Install PostgreSQL and the component that allows php to talk to PostgreSQL.

 yum -y install postgresql postgresql-server php-pgsql 

2. Configure the new service to start automatically

  /sbin/chkconfig postgresql on
/sbin/chkconfig postgresql initdb
/sbin/service postgresql start

3. Start the postgresql interactive shell and create your first user and database.

Convert your MySQL database from MyISAM to InnoDB

Before you go any further backup your database before doing any steps below. If you 'splode your database for any reason, you'll need it.

Here are the steps:

1. Shutdown MySQL

2. Move/copy/change the name of ib_logfile0 and ib_logfile1 files. (find where MySQL exists on your system - locations can vary greatly). MySQL will recreate these files when you start it up again. Not anytime you change the innodb_log_file_size parameter you will need to shutdown MySQL, move these files, and start up MySQL again.

mysqltuner.pl

Use the following script to analyze MySQL database for tuning purposes.

#!/usr/bin/perl -w
# mysqltuner.pl - Version 1.0.0
# High Performance MySQL Tuning Script
# Copyright (C) 2006-2008 Major Hayden - major@mhtx.net
#
# For the latest updates, please visit http://mysqltuner.com/
# Subversion repository available at http://tools.assembla.com/svn/mysqltuner/
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the

MySQL 5 Useful Commands

MySQL Replication

These are the steps to set up Master/Slave replication on MySQL

mysql> grant replication slave, replication client on *.* to
repl@'10.%' identified by 'rvisitor';

mysql> show master status;

Change on my.cnf on salve
table_cache=750
query_cache_size = 32M
query_cache_limit = 512K
#replication settings
log_bin = replicate/mysql-bin
server_id = 20
relay_log = mysql-relay-bin
log_slave_updates = 1
log-slow-queries = mysql-slow-queries
long_query_time = 1
log-queries-not-using-indexes

replicate-do-db=test

replicate-do-db=test2

binlog_cache_size=128K

How to enable UTF-8 and Latin Charatersets in PHP and MySQL

I can view the UT8 characters correctly in phpMyAdmin. The data is stored correctly in the MySQL db.

However, when I call the data on a php page, php displays a? instead of the characters from the db.

This solved it for me.

Sample my.cnf

Here are the contents of my my.cnf:

Syndicate content