In order to change any numeric data type to UNSIGNED here is a simple and basic alter table statement that needs to be executed.
alter table frm_hack modify id tinyint unsigned;
But if the table is big, it will take a lot of time and here is a frm hack suggested...
http://www.facebook.com/note.php?note_id=10150115679045933
And here is how it works:
mysql> create table frm_hack(id tinyint, name varchar(100), primary key (id)) engine=InnoDB;
Query OK, 0 rows affected (0.10 sec)
mysql> insert into frm_hack values (1, 'shantanu'), (2, 'john'), (121, 'ram'), (234, 'shyam');
Query OK, 4 rows affected, 1 warning (0.03 sec)
Records: 4 Duplicates: 0 Warnings: 1
mysql> create table frm_hack_new (id tinyint unsigned, name varchar(100), primary key (id)) engine=InnoDB;
Query OK, 0 rows affected (0.08 sec)
mysql> \q
Bye
[root@localhost test]# cp frm_hack_new.frm frm_hack.frm
cp: overwrite `frm_hack.frm'? y
[root@localhost test]# mysql test
Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
mysql> show create table frm_hack\G
*************************** 1. row ***************************
Table: frm_hack
Create Table: CREATE TABLE `frm_hack` (
`id` tinyint(4) NOT NULL default '0',
`name` varchar(100) default NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
1 row in set (0.00 sec)
mysql> show create table frm_hack_new\G
*************************** 1. row ***************************
Table: frm_hack_new
Create Table: CREATE TABLE `frm_hack_new` (
`id` tinyint(3) unsigned NOT NULL default '0',
`name` varchar(100) default NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
1 row in set (0.00 sec)
mysql> select * from frm_hack;
+-----+----------+
| id | name |
+-----+----------+
| 1 | shantanu |
| 2 | john |
| 121 | ram |
| 127 | shyam |
+-----+----------+
4 rows in set (0.00 sec)
mysql> flush tables;
Query OK, 0 rows affected (0.00 sec)
mysql> show create table frm_hack\G
*************************** 1. row ***************************
Table: frm_hack
Create Table: CREATE TABLE `frm_hack` (
`id` tinyint(3) unsigned NOT NULL default '0',
`name` varchar(100) default NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
1 row in set (0.00 sec)
mysql> insert into frm_hack values (213, 'abc');
Query OK, 1 row affected (0.02 sec)
mysql> select * from frm_hack;
+-----+----------+
| id | name |
+-----+----------+
| 213 | abc |
| 1 | shantanu |
| 2 | john |
| 121 | ram |
| 127 | shyam |
+-----+----------+
5 rows in set (0.01 sec)
Labels: mysql tips
Here is an easy trick to find the table names where comma "," is being used. I will export the data in "tab" delimited format using maatkit and then look for the comma.
rm -rf /pdump/ && mkdir /pdump/
chmod 777 /pdump/
/usr/bin/mk-parallel-dump --no-gzip --base-dir '/pdump' --databases 'my_db' --tab
# grep -r ',' *.txt | awk -F":" '{print $1}' | sort -u | awk -F"." '{print $1}'
address
address_log
The 2 tables address and address_log in the my_db database has the comma somewhere in its record. This tip can be used to find a word or phrase as well.
Labels: mysql tips
Let's assume you want to update the second column of the table to upper case. The following SQL statement will do the needful.
update tbl set second_col = upper(second_col)
But if you want to do it in some other way, or if there are more complex processes involved where an SQL won't work, then here is the solution. This is 4 steps procedure to change the data. An utility called "maatkit" is required for this.
##### create directory and make it writable #####
# cd /home/
# rm -rf /pdump/
# mkdir /pdump
# chmod 777 /pdump/*
##### Download data in TAB or CSV format #####
/usr/bin/mk-parallel-dump -h'localhost' --no-gzip --base-dir '/pdump' --databases 'myDB' --tables 'country' --csv
CHUNK TIME EXIT SKIPPED DATABASE.TABLE
db 0.02 0 0 myDB
all 0.02 0 0 -
##### Change the third column data from one encryption to another #####
sh /home/shantanu/line.sh > /pdump/myDB/temp.txt
mv /pdump/myDB/temp.txt /pdump/myDB/country.000000.txt
##### drop the table insert the updated data back to the table #####
/usr/bin/mk-parallel-restore -h'localhost' --database 'myDB' --tables 'country' '/pdump' --csv
1 tables, 2 files, 1 successes, 0 failures, 0.48 wall-clock time, 0.48 load time
_____
The line.sh shell script looks like this...
#!/bin/sh
# read file line by line
file="/pdump/myDB/country.000000.txt"
while read line
do
# select the second column
mycol=`echo $line | awk -F"," '{print $2}'`
# convert it to upper case
mycol_new=`echo $mycol | tr "[:lower:]" [:upper:]`
# do some more java processing with the value
# replace the upper case text in the text file
echo $line | awk -v var="$mycol_new" -F"," '{print $1 "," var "," $3 "," $4 "," $5 "," $6 "," $7 "," $8}'
done < $file
Labels: mysql tips
mysql monitor is the nagios of Database. It is especially useful when you have a bunch of mysql DB servers to manage.
http://www.mysql.com/products/enterprise/monitor.htmlHere are 8 steps to enable the monitoring agent on the DB server. It is assumed that the monitor server is already configured and active on a server (for e.g. 10.10.10.63)
1) sudo root
2) download agent
3) Change file permissions
chmod +x mysqlmonitoragent-2.3.1.2044-linux-glibc2.3-x86-64bit-installer.bin
4) Run file
[root@localhost]# ./mysqlmonitoragent-2.3.1.2044-linux-glibc2.3-x86-64bit-installer.bin
5) Start agent
/etc/init.d/mysql-monitor-agent start
6) check status
ps -ef | grep monitor
7) check log
cat /opt/mysql/enterprise/agent/mysql-monitor-agent.log
8) create mysql user for e.g.
mysql> grant all on *.* to 'agent'@'%' identified by 'agent';
_____
The following options must be selected very carefully or else monitor will not work as expected.
Installation directory
Installation directory [/opt/mysql/enterprise/agent]:
How will the agent connect to the database it is monitoring?
[1] TCP/IP
[2] Socket
Please choose an option [1] : 1
----------------------------------------------------------------------------
Components to Install
Monitoring:
Enable the Agent to monitor a MySQL Server [Y/n]: Y
Query Analyzer:
Enable the Aggregator to aggregate Query Analyzer data from an external source [y/N]: N
Enable MySQL Proxy to collect and aggregate Query Analyzer data [y/N]: y
----------------------------------------------------------------------------
Monitored Database Information
MySQL hostname or IP address [127.0.0.1]:
Validate MySQL hostname or IP address [y/N]: N
MySQL Port [3306]:
MySQL Username [agent]: agent
MySQL Password :
Re-enter :
----------------------------------------------------------------------------
Agent User Account Creation
[1] Yes, use the credentials below to create the Agent's user account
[2] No, I've already created the account and I'm sure it's correct
Please choose an option [1] : 2
----------------------------------------------------------------------------
MySQL Enterprise Monitor Options
Hostname or IP address []: 10.10.10.63
Tomcat Server Port [18080]:
Tomcat SSL Port [18443]:
Use SSL? [y/N]: y
Agent Username [agent]: agent
Agent Password :
Re-enter :
Labels: mysql