Multiple instances of mysql same version

# mkdir /etc/mysql

# /etc/mysql/dev.cnf (Instance name)

[mysqld]
bind-address = IP Address
datadir=/data/mysql-dev
socket=/data/mysql-dev/mysql.sock
user=mysql
# Default to using old password format for compatibility with mysql 3.x
# clients (those using the mysqlclient10 compatibility package).
general-log-file=/var/log/mysqld_dev.log
general-log=on

[mysqld_safe]
log-error=/data/mysql-dev/error.log
socket=/data/mysql-dev/mysql.sock

# Make a datadir and /etc/mysql/dev .cnf file for all instances you would like such as stg and prod etc.

# Edit default mysql init script with the following differences

45a46,47
> conffile=dev.cnf
> confdir=/etc/mysql
47c49,50
< datadir=

> datadir=/data/mysql-dev/
> mysqld_pid_file_path=/data/mysql-dev/mysqld.pid
63d65
< mysqld_pid_file_path=
283c285
< $bindir/mysqld_safe –datadir=”$datadir” –pid-file=”$mysqld_pid_file_path” $other_args >/dev/null 2>&1 &

> $bindir/mysqld_safe –defaults-file=$confdir/$conffile –datadir=”$datadir” –pid-file=”$mysqld_pid_file_path” $other_args >/dev/null 2>&1 &
384a387
>

# touch /var/log/mysqld_dev; chown mysql:mysql /var/log/mysqld_dev

# mkdir /data/mysql-dev; chown -R mysql:mysql /data/mysql-dev

# Run: /usr/bin/mysql_install_db –datadir=/data/mysql-dev

# chown -R mysql:mysql /data/mysql-dev/

# Create the necessary binaries

# vi /usr/bin/mysql-dev

# Insert: /usr/bin/mysql -S /data/mysql-dev/mysql.sock $@

# vi /usr/bin/mysqldump-dev

# Insert: /usr/bin/mysqldump -S /data/mysql-dev/mysql.sock $@

MySQL Quick Reference

Lots of this was taken from here: http://www.pantz.org/software/mysql/mysqlcommands.html

Figured since I referenced it so much …

Show replication slave status.

mysql> show slave status;

Show process list.

mysql> show processlist; (show full proccesslist;)

Show variables.

mysql> show variables like ‘%collation%’;

To login (from unix shell) use -h only if needed.

# [mysql dir]/bin/mysql -h hostname -u root -p

Create a database on the sql server.

mysql> create database [databasename];

Create a database on the sql server setting to UTF8

mysql> CREATE DATABASE dbname CHARACTER SET utf8 COLLATE utf8_general_ci;

List all databases on the sql server.

mysql> show databases;

Switch to a database.

mysql> use [db name];

To see all the tables in the db.

mysql> show tables;

To see database’s field formats.

mysql> describe [table name];

To delete a db.

mysql> drop database [database name];

To delete a table.

mysql> drop table [table name];

Show all data in a table.

mysql> SELECT * FROM [table name];

Returns the columns and column information pertaining to the designated table.

mysql> show columns from [table name];

Show certain selected rows with the value “whatever”.

mysql> SELECT * FROM [table name] WHERE [field name] = “whatever”;

Show all records containing the name “Bob” AND the phone number ‘3444444’.

mysql> SELECT * FROM [table name] WHERE name = “Bob” AND phone_number = ‘3444444’;

Show all records not containing the name “Bob” AND the phone number ‘3444444’ order by the phone_number field.

mysql> SELECT * FROM [table name] WHERE name != “Bob” AND phone_number = ‘3444444’ order by phone_number;

Show all records starting with the letters ‘bob’ AND the phone number ‘3444444’.

mysql> SELECT * FROM [table name] WHERE name like “Bob%” AND phone_number = ‘3444444’;

Show all records starting with the letters ‘bob’ AND the phone number ‘3444444’ limit to records 1 through 5.

mysql> SELECT * FROM [table name] WHERE name like “Bob%” AND phone_number = ‘3444444’ limit 1,5;

Use a regular expression to find records. Use “REGEXP BINARY” to force case-sensitivity. This finds any record beginning with a.

mysql> SELECT * FROM [table name] WHERE rec RLIKE “^a”;

Show unique records.

mysql> SELECT DISTINCT [column name] FROM [table name];

Show selected records sorted in an ascending (asc) or descending (desc).

mysql> SELECT [col1],[col2] FROM [table name] ORDER BY [col2] DESC;

Return number of rows.

mysql> SELECT COUNT(*) FROM [table name];

Sum column.

mysql> SELECT SUM(*) FROM [table name];

Join tables on common columns.

mysql> select lookup.illustrationid, lookup.personid,person.birthday from lookup left join person on lookup.personid=person.personid=statement to join birthday in person table with primary illustration id;

Creating a new user. Login as root. Switch to the MySQL db. Make the user. Update privs.

# mysql -u root -p
mysql> use mysql;
mysql> INSERT INTO user (Host,User,Password) VALUES(‘%’,’username’,PASSWORD(‘password’));
mysql> flush privileges;

Change a users password from unix shell.

# [mysql dir]/bin/mysqladmin -u username -h hostname.blah.org -p password ‘new-password’

Change a users password from MySQL prompt. Login as root. Set the password. Update privs.

# mysql -u root -p
mysql> SET PASSWORD FOR ‘user’@’hostname’ = PASSWORD(‘passwordhere’);
mysql> flush privileges;

Recover a MySQL root password. Stop the MySQL server process. Start again with no grant tables. Login to MySQL as root. Set new password. Exit MySQL and restart MySQL server.

# /etc/init.d/mysql stop
# mysqld_safe –skip-grant-tables &
# mysql -u root
mysql> use mysql;
mysql> update user set password=PASSWORD(“newrootpassword”) where User=’root’;
mysql> flush privileges;
mysql> quit
# /etc/init.d/mysql stop
# /etc/init.d/mysql start

Set a root password if there is on root password.

# mysqladmin -u root password newpassword

Update a root password.

# mysqladmin -u root -p oldpassword newpassword

Allow the user “bob” to connect to the server from localhost using the password “passwd”. Login as root. Switch to the MySQL db. Give privs. Update privs.

# mysql -u root -p
mysql> use mysql;
mysql> grant usage on *.* to bob@localhost identified by ‘passwd’;
mysql> flush privileges;

Give user privilages for a db. Login as root. Switch to the MySQL db. Grant privs. Update privs.

# mysql -u root -p
mysql> use mysql;
mysql> INSERT INTO db (Host,Db,User,Select_priv,Insert_priv,Update_priv,Delete_priv,Create_priv,Drop_priv) VALUES (‘%’,’databasename’,’username’,’Y’,’Y’,’Y’,’Y’,’Y’,’N’);
mysql> flush privileges;

or

mysql> grant all privileges on databasename.* to username@localhost;
mysql> flush privileges;

To update info already in a table.

mysql> UPDATE [table name] SET Select_priv = ‘Y’,Insert_priv = ‘Y’,Update_priv = ‘Y’ where [field name] = ‘user’;

Delete a row(s) from a table.

mysql> DELETE from [table name] where [field name] = ‘whatever’;

Update database permissions/privilages.

mysql> flush privileges;

Delete a column.

mysql> alter table [table name] drop column [column name];

Add a new column to db.

mysql> alter table [table name] add column [new column name] varchar (20);

Change column name.

mysql> alter table [table name] change [old column name] [new column name] varchar (50);

Make a unique column so you get no dupes.

mysql> alter table [table name] add unique ([column name]);

Make a column bigger.

mysql> alter table [table name] modify [column name] VARCHAR(3);

Delete unique from table.

mysql> alter table [table name] drop index [colmn name];

Load a CSV file into a table.

mysql> LOAD DATA INFILE ‘/tmp/filename.csv’ replace INTO TABLE [table name] FIELDS TERMINATED BY ‘,’ LINES TERMINATED BY ‘\n’ (field1,field2,field3);

Dump all databases for backup. Backup file is sql commands to recreate all db’s.

# [mysql dir]/bin/mysqldump -u root -ppassword –opt >/tmp/alldatabases.sql

Dump one database for backup.

# [mysql dir]/bin/mysqldump -u username -ppassword –databases databasename >/tmp/databasename.sql

Dump a table from a database.

# [mysql dir]/bin/mysqldump -c -u username -ppassword databasename tablename > /tmp/databasename.tablename.sql

Restore database (or database table) from backup.

# [mysql dir]/bin/mysql -u username -ppassword databasename < /tmp/databasename.sql

Get stored procedures for a database (if you have the permissions)

mysqldump -u root -p  –routines –no-create-info –no-data –no-create-db –skip-opt database_name > file_name.sql

Convert MySQL Slave to Master

Found this interesting the other day. Our production master SQL server was having connection issues.  So we needed to cut over from the master to the slave to try to resolve the issues.

What we did

# Shutdown the IP address for the master

# Shut off the mysql service on the master

# Bring up the IP address on the slave

This was all that was REQUIRED to get MySQL connections pointing to the slave.

What was WRONG with this was what happens when we want to cut back over once the issues with the master have been resolved. Turned out to be a ethernet cable. Once the cable was replaced we were able to verify the issue no longer existed.

What we SHOULD have done

# Log into the MySQL slave and run STOP SLAVE; RESET SLAVE;

# Add log-bin to the slave’s /etc/my.cnf file

# Stop the IP interface on the master (optionally the MySQL service)

# Add the IP address to the slave system

# Restart the MySQL service on the slave server. It should now be running as the master.

The reason this is a better solution is that now when the broken system is fixed, we can set it up as the slave and have the binary log replicate and catch up the slave to the new master and then cut over to the fixed system as the master in the same steps as above.

MySQL accepts network connections

Make sure skip-networking is commented out in my.cnf file.

Test with: netstat -antulp | grep LISTEN

tcp        0      0 127.0.0.1:199               0.0.0.0:*                   LISTEN      2297/snmpd          
tcp        0      0 0.0.0.0:3306                0.0.0.0:*                   LISTEN      4485/mysqld         
tcp        0      0 :::22                       :::*                        LISTEN      2334/sshd

The mysqld entry will not be listed if MySQL is not accepting network connections.

MySQL logs filling up /var

Ran into an issue where the MySQL log was filling up the /var partition.  Size of the log was 21G and growing.

root@system_name:/path# > /var/log/mysqld.log (This will zero the mysqld.log)

This will cause a blip in mysql connections. A better method is above to not interrupt connections.

#1. Move the current log file: mv mysql.log mysql.log.YYYYMMDD

#2. The MySQL service should still be writing to the mysql.log.YYYYMMDD file.

#3. Edit perms of the new mysql.log file: touch mysql.log; chown mysql:mysql mysql.log

#4. Now the fun part especially on a production MySQL server, we need to HUP the MySQL process to get it to start logging to the new file: ps aux | grep mysql; kill -HUP PID 

#5. Make sure you are still getting mysql connections: mysql -u root -p ; mysql> show full process list;

#6. According to this thread (http://dev.gigigo.tw/index.php?option=com_content&view=article&id=341&Itemid=330) the HUP does this: Correct, by default this signal is caught and the logs, table cache, status info, privileges and host
cache are all flushed. This is equivalent to bash# mysqladmin reload.

#7.  We had a connection timeout in our Nagios from our frontend web servers when we ran this command but as far as we could tell we didn’t lose any database connections and the mysql service started logging to the new logfile.