Cool Dba

  • Home
  • Sql tricks
  • Security
  • Maintainance
  • Backups
  • Recovery
  • Replication
  • Installation
  • Linux
Showing posts with label sql tricks. Show all posts
Showing posts with label sql tricks. Show all posts

Friday, May 27, 2016

Tracing the execution of processes using strace

 Anil Joshi     12:47 AM     Linux, Mysql, sql tricks, Strace     No comments   


Strace is quite simply a tool that traces the execution of system calls. It can trace the execution of a binary from start to end, and output a line of text with the name of the system call, the arguments and the return value for every system call over the lifetime of the process.

   1.Find out which config files a program reads on startup
strace php 2>&1 | grep php.ini
open("/usr/local/bin/php.ini", O_RDONLY) = -1 ENOENT (No such file or directory)
open("/usr/local/lib/php.ini", O_RDONLY) = 4
lstat64("/usr/local/lib/php.ini", {st_mode=S_IFLNK|0777, st_size=27, ...}) = 0
readlink("/usr/local/lib/php.ini", "/usr/local/Zend/etc/php.ini", 4096) = 27

lstat64("/usr/local/Zend/etc/php.ini", {st_mode=S_IFREG|0664, st_size=40971, ...}) = 0

2.What is that process doing RIGHT NOW?
Ever had a process suddenly eating lots of CPU? Or had a process seem to be hanging? Then you find the pid, and do this:
root@dev:~# strace -p 15427
Process 15427 attached - interrupt to quit
futex(0x402f4900, FUTEX_WAIT, 2, NULL
Process 15427 detached

3.MySQL with strace
Start MySQL Daemon
service mysqld start

Connect strace to mysqld
ps -elf | grep mysqld (and find out that mysqld is PID 1234)

sudo strace -p 1234 -f


Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg

Sunday, May 22, 2016

How to Export and Import files in Mysql

 Anil Joshi     8:14 AM     Backups, sql tricks     No comments   


 Import of csv file into mysql(Linux)

LOAD DATA INFILE '/path to csv file/' INTO TABLE 'tbl_name'
  FIELDS TERMINATED BY ',' ENCLOSED BY '"'
  LINES TERMINATED BY '\r\n'
  IGNORE 1 LINES
 (@col1,@col2) SET fileld1=@col1,field2=@col2;

 Import of csv file into mysql(Windows)

LOAD DATA LOCAL INFILE '//path to csv file//'
INTO TABLE `tbl_name`
FIELDS TERMINATED BY ',' ENCLOSED BY '"'
LINES TERMINATED BY '\r\n'
(`field1, `field2`, `field3`, `field4`)

Export of mysql data into csv file(Linux)

SELECT col1,col2
union
SELECT col1,col2  INTO OUTFILE '/path to csv/'
from 'tbl_name'
FIELDS TERMINATED BY ','ENCLOSED BY '"'
LINES TERMINATED BY '\n' 

Export of mysql data into csv file(Windows)

SELECT col1,col2
union
SELECT col1,col2 INTO OUTFILE '\\path to csv\\'
FROM tbl_name
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\n';


Find columns names from a table in case of large columns
(helpful while importing large data with column name)

select GROUP_CONCAT(CONCAT("'",COLUMN_NAME,"'"))
from INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'tbl_name'
AND TABLE_SCHEMA = 'db_name'
order BY ORDINAL_POSITION


Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg

Wednesday, May 18, 2016

MySQL Restart Failed

 Anil Joshi     11:34 PM     sql tricks     No comments   



     



 MySQL Restart Failed :  
The Server Quit Without Updating PID File



[root@server:~ ] $ service mysqld start
Starting MySQL... ERROR! The server quit without updating PID file
This error can be de to different reasons.
1. Check if there is any running MySQL process by using the following commands and kill all and restart.
ps aux | grep mysql
If you are able to see any mysql process like below, just kill that process.
root@server [~]# ps aux | grep mysql
root      1140  0.0  0.0   4140   676 pts/0    D+   22:34   0:00 grep mysql
root      2987  0.0  0.0   4736  1288 ?        S    Sep27   0:00 /bin/sh /usr/bin/mysqld_safe
Now kill it.
kill -9 2987
Make sure that there is no mysql process running at this point. Restart MySQL.
service mysqld restart
2. If the above fix is not working, now we can try some more. It can be due to some conflicting entries in my.cnf
Try moving the mysql config file /etc/my.cnf file and restart mysql.
mv /etc/my.cnf /etc/my.cnf.backup
Restart mysql.
service mysqld restart
3. If none of the above is working, try to move the log file named “ib_logfile” in “/var/lib/mysql” and restart mysql. Sometimes mysql will fail because it face difficulty updating the log file.
mv /var/lib/mysql/ib_logfile* /root/
Restart and see the results.
service mysqld restart
You can also remove ib_logfile as it will be created automatically after mysql restart.

Hope this helps.
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg

Running select statement inside an update query

 Anil Joshi     11:25 PM     sql tricks     No comments   


Running select statement inside an update query



You could use a non-correlated subquery to do the work for you:


1.  UPDATE tbl_1 dest,
(SELECT count(*) as cnt_n,userid FROM tbl_2 group by userid) src
 SET dest.cnt = src.cnt_n
where dest.id = src.userid



2. UPDATE  tbl_taxclasses c
INNER JOIN
( SELECT  COUNT(regionsid) AS n  FROM  tbl_taxclasses_regions GROUP BY   classid ) r
USING(classid)
SET
c.regionscount = r.n
WHERE
c.classid = 1



3. UPDATE `tbl_taxclasses`
 SET `regionscount` = (
  SELECT COUNT(regionsid) AS `num`
  FROM `tbl_taxclasses_regions`
  WHERE classid = 1)
 WHERE classid = 1 LIMIT 1



4.UPDATE table1 dest, (SELECT * FROM table2 where id=x) src
  SET dest.col1 = src.col1 where dest.id=x ;




Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg

Mass Killing Mysql Process

 Anil Joshi     11:09 PM     sql tricks     No comments   


  Mass Killing Mysql Process

mysql> select concat('KILL ',id,';') from information_schema.processlist where user='root';

+------------------------+
| concat('KILL ',id,';') |
+------------------------+
| KILL 3101;             |
| KILL 2946;             |
+------------------------+

2 rows in set (0.00 sec)

mysql> select concat('KILL ',id,';') from information_schema.processlist where user='root' into outfile '/tmp/a.txt';

Query OK, 2 rows affected (0.00 sec)

mysql> source /tmp/a.txt;


Query OK, 0 rows affected (0.00 sec)

That's It.

Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg
Home

Deals on Amazon

Mobiles on Amazon

About Me

My photo
Anil Joshi
New Delhi, Delhi, India
I am a simple man with little knowledge of computer software and Tehnology.
View my complete profile

Popular Posts

  • Complete uninstallation of Mysql Server on centos
    Step 1: Check list the mysql rpm which is installed on server rpm -qa | grep mysql or yum list installed | grep mysql Step 2 : Re...
  • SELinux mechanism in linux environment.
    Security-Enhanced Linux (SELinux) is a mandatory access control (MAC) security mechanism implemented in the kernel. SELinux was first ...
  • Finding and preventing ddos attack on Linux Server
    A distributed denial-of-service (DDoS) attack occurs when multiple systems flood the bandwidth or resources of a targeted system, usual...
  • Tracing the execution of processes using strace
    Strace is quite simply a tool that traces the execution of system calls. It can trace the execution of a binary from start to end, and ou...
  • Filter Replication Variables
    Filtering can be done on the Master or on the Slave. Filtering on the Slave is normally preferred as in this case you get full binary lo...
  • Mysql Installation using rpm package
        1.Download the required rpm filess MySQL-client-community-5.1.25-0.rhel5.i386.rpm MySQL-server-community-5.1.25-0.rhel5.i386....
  • Dual Master Replication(Master-Master)
    MySQL replication is the most flexible way to deal with scalability and availability. The most common problem with replication is ...
  • Master-Slave Replication without downtime
    First, we configure master’s   /etc/mysql/my.cnf   by including these lines in the [mysqld] section: server - id = 1 bin...
  • Mysql Security
    Improved  MySQL Database Se curity 1. Secure your server Many known attacks are possible only once physical access to a machi...
  • Running multiple mysql instance on single server(Centos&Red Hat)
     Follow Below Steps:- Step (1):  Create a new MySQL data directory. mkdir - p / var / lib / mysql2 Step 2:   Give permission an...
Best Shoes on amazon

Categories

  • Backups
  • Database Maintainance
  • Installation
  • Linux
  • Mysql
  • Recovery
  • Replication
  • Scripts
  • Security
  • SELinux
  • sql tricks
  • Strace

Pages

  • Home

Blog Archive

  • ▼  2016 (19)
    • ▼  June (1)
      • Complete uninstallation of Mysql Server on centos
    • ►  May (18)

Sample Text

Copyright © Cool Dba | Powered by Blogger
Design by Hardeep Asrani | Blogger Theme by NewBloggerThemes.com | Distributed By Gooyaabi Templates