Archive

Archive for the ‘mysql’ Category

Mysql Query Caching

February 9th, 2010

Enabling Caching

*verify caching is on. if need to update, then restart.

jesterj@jesterj-laptop:/etc/mysql$ grep "query" /etc/mysql/my.cnf
query_cache-type = 1 #cache everything
query_cache_limit = 1M
query_cache_size = 16M

*Note that variables in the my.cnf file are reflecteed in the below variables.

mysql> SHOW VARIABLES LIKE '%query_cache%';
+------------------------------+----------+
| Variable_name | Value |
+------------------------------+----------+
| have_query_cache | YES |
| query_cache_limit | 1048576 |
| query_cache_min_res_unit | 4096 |
| query_cache_size | 16777216 |
| query_cache_type | ON |
| query_cache_wlock_invalidate | OFF |
+------------------------------+----------+
6 rows in set (0.00 sec)

View the query cache

mysql> SHOW STATUS LIKE '%qcache%';
+-------------------------+----------+
| Variable_name | Value |
+-------------------------+----------+
| Qcache_free_blocks | 1 |
| Qcache_free_memory | 16612448 |
| Qcache_hits | 1 |
| Qcache_inserts | 2 |
| Qcache_lowmem_prunes | 0 |
| Qcache_not_cached | 110 |
| Qcache_queries_in_cache | 2 |
| Qcache_total_blocks | 6 |
+-------------------------+----------+
8 rows in set (0.00 sec)

Now run test queries for a database so we can test query cache..

mysql> select * from test.data order by name ASC
mysql> select * from test.data order by name DESC

Notice as you run more and more queries the available free memory descreases and the queries in cache number increases

mysql> SHOW STATUS LIKE '%qcache%';
+-------------------------+----------+
| Variable_name | Value |
+-------------------------+----------+
| Qcache_free_blocks | 1 |
| Qcache_free_memory | 16534728 |
| Qcache_hits | 5 |
| Qcache_inserts | 3 |
| Qcache_lowmem_prunes | 0 |
| Qcache_not_cached | 110 |
| Qcache_queries_in_cache | 3 |
| Qcache_total_blocks | 8 |
+-------------------------+----------+
8 rows in set (0.00 sec)

Demand Query Caching

You can also specify query cache on demand so only queries that you specify
are cached.

Change query cache type to 2.

[mysql]
….
query-cache-type = 2
….

Restart mysql.

sudo /etc/init.d/mysql restart

Now log back into mysql and verify that DEMAND caching is set

mysql> SHOW VARIABLES LIKE '%query_cache%';
+------------------------------+----------+
| Variable_name | Value |
+------------------------------+----------+
| have_query_cache | YES |
| query_cache_limit | 1048576 |
| query_cache_min_res_unit | 4096 |
| query_cache_size | 16777216 |
| query_cache_type | DEMAND |
| query_cache_wlock_invalidate | OFF |
+------------------------------+----------+
6 rows in set (0.00 sec)

Now specify SQL_CACHE when you run a statemnt.

mysql> select SQL_CACHE * from test.data order by name ASC

Note that you can also use SQL_NO_CACHE when query-cache-type=1 to turn off caching for a particular sql stmt.

mysql

Mysql Store Proceedures - The Basics

February 9th, 2010

Create the procedure.

mysql> CREATE PROCEDURE donkey() SELECT * FROM data WHERE name LIKE '%64%';
Query OK, 0 rows affected (0.00 sec)

Execute the procedure.


mysql> CALL donkey();
+-----+------------+
| id | name |
+-----+------------+
| 31 | 1265397164 |
| 99 | 1265402640 |
| 100 | 1265402641 |
| 101 | 1265402642 |
| 102 | 1265402643 |
| 103 | 1265402644 |
| 104 | 1265402645 |
| 105 | 1265402646 |
| 106 | 1265402647 |
| 107 | 1265402648 |
| 108 | 1265402649 |
| 122 | 1265402664 |
| 219 | 1265402764 |
| 316 | 1265402864 |
| 479 | 1265404564 |
| 553 | 1265404640 |
| 554 | 1265404641 |
| 555 | 1265404642 |
| 556 | 1265404643 |
+-----+------------+
19 rows in set (0.00 sec)

Display the query.

mysql> SHOW CREATE PROCEDURE donkey2\G;
*************************** 1. row ***************************
Procedure: donkey2
sql_mode:
Create Procedure: CREATE DEFINER=`root`@`localhost` PROCEDURE `donkey2`()
SELECT * FROM data WHERE name LIKE '%3%'
character_set_client: latin1
collation_connection: latin1_swedish_ci
Database Collation: latin1_swedish_ci
1 row in set (0.00 sec)
ERROR:
No query specified

Delete the procedure.

mysql> DROP PROCEDURE donkey;
Query OK, 0 rows affected (0.00 sec)

mysql

ERROR 1045 (28000): Access denied for user ‘root’@'localhost’ (using password: NO)

February 6th, 2010

For some reason after setting up replication the root password on both master and slave were cleared. This has happened more than once. Note that i can access mysql via…

$mysql -u root -p
mysql>

Of course this is not desirable. Simply reset the password on the master and the table propegates to the slave and fixes the problem.

UPDATE mysql.user SET Password=PASSWORD('MyNewPass') WHERE User='root';
FLUSH PRIVILEGES;

mysql

Setting up muliple instances of mysql using mysqlmanager

February 3rd, 2010

1. Create a new copy of the /etc/mysql/my.cnf file called my2.cnf. The new file should have the following configurations.

[client]
port = 3307
socket = /var/run/mysqld2/mysqld.sock

[mysqld_safe]
socket = /var/run/mysqld2/mysqld.sock
nice = 0

[mysqld]
user = mysql
pid-file = /var/run/mysqld2/mysqld.pid
socket = /var/run/mysqld2/mysqld.sock
port = 3307
basedir = /usr
datadir = /var/lib/mysql2
tmpdir = /tmp>

2. Create copy of /var/lib/mysql to /var/lib/mysql2 (data dir)

mkdir mysql2; cp -a mysql/* mysql2

3. create /var/run/mysqld2 dir. should be owned by mysql. (where pid and sock files are)

chown mysql:mysql /var/run/mysqld2

4. Then call mysqlmanager with the defaults file. Best to just modify the /etc/init.d/mysql2 file as a shell script and call directly!

mysqlmanager --defaults-file=/etc/mysql/my2.cnf --log=/var/log/mysqlmanager.log --pid-file=/var/run/mysqld2/mysqld.pid --angel-pid-file=/var/run/mysqld2/mysqlmanager.angel.pid --run-as-service --user=mysql

*create the mysqlmanager.log file.

sudo touch /var/log/mysqlmanager.log
sudo chown mysql:mysql mysqlmanager.log

5. Login to your new instance. Must specify new port and sock file.


root@slave:# mysql -P 3307 --socket=/var/run/mysqld2/mysqld.sock -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 61
Server version: 5.0.51a-3ubuntu5.4 (Ubuntu)

6. Type ‘help;’ or ‘\h’ for help. Type ‘\c’ to clear the buffer.

mysql>

Now connect to the new host from ‘mytop’ to monitor your instance.

mytop -d mysql -P 3307 -S /var/run/mysqld2/mysqld.sock -u root -pPASSHERE

Rinse and repeat for additional mysql instances!

souce: http://www.ducea.com/2009/01/19/running-multiple-instances-of-mysql-on-the-same-machine/

mysql

Vi Editing Basics

January 14th, 2010

Basic Commands

:w  - write file

:wq - write and quit

:q! - exit, ignore changes

:set number - show line numbers

:set nonumber - turn off line numbers.


Text Mode

dd - Delete lines

2dd - delete 2 lines

p  - paste buffer lines

w - skip to mext word

a - append after cursor

i - insert at cursor

o - open new line after current line

r - replace character.

yy - yank current line

Searching

/string - search forward for string

?string - search back for string.

/tel[a-z] - search for ‘tel’ with last letter a-z

Find and Replace - ‘g’ stand for global, all lines in document.

:s/OLDWORD/NEWWORD/g

Search and replace,escaping slashes.

To replace the phrase /var/lib/mysql with /home/mysql you need to escape each ‘/’ with a ‘\’. For example…

:%s/\/var\/lib\/mysql/\/home\/mysql/

Regular Expressions

/Hello/ - search for word ‘Hello’ in line.

/^Hello$/ - search and matches if Hello is only word on line.

Recording Macros

qv - start record mode for macro ‘a’. second letter should be lower case character.

q - stop record

@v - to paste recorded text

9 @v - repeat v macro 9 times.
Save Settings

You can save persistent changes for vi by editing ~/.exrc

set number
….

Uncategorized, mysql

Accessing MySQL Remotely

February 25th, 2009

Say for example, you have a web page you want to access some data on but the database is located on a different server. To do this you will need to add another user to the mysql.users table and specify the remote location where you are accesing from.

$ mysql -u root -p

mysql>  GRANT ALL PRIVILEGES ON database_name.* TO myUser@host_name IDENTIFIED BY 'pass' WITH GRANT OPTION;

*user ‘%’ for hostname if wanting to accept connections from all hosts.

mysql> Flush priviledges

Next, edit your /etc/mysql/my.cnf file:

Comment the bind-address

#bind-address: 127.0.0.1  //if enabled with disallow connections from a remote host.

Restart mysql:

# /etc/init.d/mysql restart

Uncategorized, mysql

Insert multiple records in MySQL with one Stmt.

December 11th, 2008
INSERT INTO table (id, name) values ('1','test');
INSERT INTO table (id, name) values ('2','test2');
INSERT INTO table (id, name) values ('3','test3');

However, it is much quicker to do it this way:

INSERT INTO table (id, name) values ('1', 'test1'), ('2', 'test2'), ('3', 'test3');

Much faster!

Uncategorized, mysql

Fixing a corrupt mysql database

November 13th, 2008

Do a check on the database and verify if there are errrors:

root@psedev2:/elog# mysqlcheck -u root -p jesterads
jester_jesterads.ajax_example                      OK
jester_jesterads.banners                           OK
jester_jesterads.category1                         OK
jester_jesterads.category2                         OK
jester_jesterads.category3                         OK
jester_jesterads.event_invites                     OK
jester_jesterads.event_member_list                 OK
jester_jesterads.event_reminders                   OK
jester_jesterads.faq                               OK
jester_jesterads.groups                            OK
jester_jesterads.groups_discussions                OK
jester_jesterads.inventory                         OK
jester_jesterads.location_cities                   OK
jester_jesterads.location_map                      OK
jester_jesterads.location_neighborhoods            OK
jester_jesterads.location_states                   OK
jester_jesterads.log
warning  : Table is marked as crashed
warning  : 3 clients are using or haven’t closed the table properly
error    : Size of indexfile is: 8814592        Should be: 8815616
error    : Corrupt
jester_jesterads.mailing_list                      OK
jester_jesterads.members                           OK

# mysql -u jester_root -p jester_jest
Enter password:
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 479671 to server version: 4.1.22-standard-log

Type ‘help;’ or ‘\h’ for help. Type ‘\c’ to clear the buffer.

mysql> check table log;
+———————-+——-+———-+———————————————————-+
| Table                | Op    | Msg_type | Msg_text                                                 |
+———————-+——-+———-+———————————————————-+
| jester_jesterads.log | check | warning  | Table is marked as crashed                               |
| jester_jesterads.log | check | warning  | 3 clients are using or haven’t closed the table properly |
| jester_jesterads.log | check | error    | Size of indexfile is: 8814592        Should be: 8815616  |
| jester_jesterads.log | check | error    | Corrupt                                                  |
+———————-+——-+———-+———————————————————-+
4 rows in set (0.00 sec)

mysql> repair table log;
+———————-+——–+———-+———-+
| Table                | Op     | Msg_type | Msg_text |
+———————-+——–+———-+———-+
| jester_jesterads.log | repair | status   | OK       |
+———————-+——–+———-+———-+
1 row in set (14.66 sec)

Linux, mysql