Showing posts with label mysql. Show all posts
Showing posts with label mysql. Show all posts

Monday, 14 December 2015

perl mysql connection note

### From mysql side ###

NOTE:
1. Install mysql server.
2. update /etc/mysql/my.cnf to listen outside from the localhost.
3. You should have a user with the access to a database, By default you can connect to remote mysql database as root user.
4. The user should have the access to connect from remote host.

https://www.digitalocean.com/community/tutorials/how-to-create-a-new-user-and-grant-permissions-in-mysql

# creating a test database
CREATE DATABASE test; 

# created a mysql user [ For this example, I have created an mysql user ]
CREATE USER ‘mysql'@'%' IDENTIFIED BY 'password';

# Grant the mysql user to access database [ Not secure example but, just for example ]
GRANT ALL PRIVILEGES ON * . * TO 'mysql@'%';

# Enable the mysql server to listen from outside
/etc/mysql/my.cnf
#bind-address           = 127.0.0.1


##########################################################
##########################################################

### From perl side ###
#!/usr/bin/perl

use DBI;
use strict;

my $host = ‘mysql_db_server';
my $driver = "mysql";
my $database = "test";
my $dsn = "DBI:$driver:$database;host=$host";
my $userid = "mysql";
my $password = "password";

my $dbh = DBI->connect($dsn, $userid, $password ) or die $DBI::errstr;

NOTE: you need to have DBI::mysql module in your host.


####### another example ######

#!/usr/bin/perl

use DBI;
use strict;

my $host = ‘mysql_db_server';
my $driver = "mysql"; 
my $database = "test";
my $dsn = "DBI:$driver:$database;host=$host";
my $userid = "mysql";
my $password = "password";

my $dbh = DBI->connect($dsn, $userid, $password ) or die $DBI::errstr;


#
#

my $query="show databases";
print "\n\UFollowing is the output of query: \"$query\"\n\n";
my $sth=$dbh->prepare($query);
$sth->execute();

while (my @row=$sth->fetchrow_array)
{
  print $row[0]."\n";
}

#
#
$query="show tables";
print "\n\UFollowing is the output of query: \"$query\"\n\n";

$sth=$dbh->prepare($query);
$sth->execute();

while (my @row=$sth->fetchrow_array)
{
  print $row[0]."\n";
}


#
#
$query="describe domains";
print "\n\UFollowing is the output of query: \"$query\"\n\n";

$sth=$dbh->prepare($query);
$sth->execute();
while (my @row=$sth->fetchrow_array)
{
  print $row[0]."\n";
}

$sth->finish;

## Note: close, only at the end. No need to close in middle.

### output of the above example ###


FOLLOWING IS THE OUTPUT OF QUERY: "SHOW DATABASES"

information_schema
mysql
performance_schema
test

FOLLOWING IS THE OUTPUT OF QUERY: "SHOW TABLES"

domains

FOLLOWING IS THE OUTPUT OF QUERY: "DESCRIBE DOMAINS"

id
name
url
descr


Ref links
http://aruljohn.com/code/perl/mysqlselect.html

Saturday, 5 July 2014

mysql quick learning

Primary Keys:
  - all tables should have "primary keys"
  - column that is 100% unique
  - no rows can have some primary key.

SHOW databases;

USE <db_name>

SHOW tables;

SHOW COLUMNS FROM customers;

DESC customers;

SELECT * FROM customers LIMIT 0,30;

SELECT city FROM customers;


; is must at the end of the query.
white space & multiple line is allowed.
Its a company standard to use "standard CAPS"

SELECT name,city FROM customers;

SELECT * FROM customers;

SELECT DISTINCT state FROM customers;

SELECT COUNT(state) FROM customers;

SELECT DISTINCT state FROM customers LIMIT 5;

SELECT id,name FROM customers LIMIT 5;

[ its ordering using PRIMARY KEYS by default? ]

SELECT id,name FROM customers LIMIT 5,10

[ From id 6 to 15. (total 10 rows) ]
[In mysql db record start with 0]


Fully qualify name:

SELECT customers.address FROM customers;
SELECT name FROM customers ORDER BY name;
SELECT name,address FROM customers ORDER BY id;

SELECT state,city,name FROM customers ORDER BY state,name,

SELECT name,zip FROM customers ORDER BY zip DESC;

[ASC: for ascending, but this is by default. ]

SELECT name,id FROM customers ORDER BY id DESC LIMIT 1;

SELECT name FROM customers ORDER BY name LIMIT 1;


* BASIC DATA FILTERING:

SELECT id,name FROM customers WHERE id=54;
SELECT id,name FROM customers WHERE id!=54;
SELECT id,name FROM customers WHERE id<8;
SELECT id,name FROM customers WHERE id<=8;

SELECT id,name FROM customers WHERE id BETWEEN 25 AND 30;

[ in WHERE with char,use 'SINGLE QUOTATION' mark. ]

SELECT name,state FROM customers WHERE state='CA';

SELECT name,state,city FROM customers WHERE state='ca' AND city='Hollywood';

SELECT name,state,city FROM customers WHERE city='Boston' OR state='CA';

SELECT id,name,ciry FROM customers WHERE (id=1 OR id=2) AND city='Raleigh';


[ if you have multiple AND or OR statement please use () to group them. ]


*) IN or NOT IN:

SELECT name,state FROM customers WHERE state='CA' OR state='NC' OR state='NY';

(for bunch of OR...)

SELECT name,state FROM customers WHERE state IN('CA','NC','NY') ORDER BY state;

[ its a good idea to show your record in some order. ]

SELECT name,state FROM customers WHERE state NOT IN('CA','NC','NY') ORDER BY state;

% -> wild card -> any things.

SELECT name FROM items WHERE  name LIKE 'name%';

SELECT name FROM items WHERE name LIKE '%couputer%';

[ mysql is not case sensitive. ]

-   -> only a single character.


*) Regular Expression in mysql:


we can use the same regular expression.

SELECT name FROM items WHERE name REGEXP 'abc|xyz';

SELECT name FROM items WHERE name REGEXP '[0-9]boxes';



* when you want to include your own character in your sql output:

SELECT CONCAT(city,',',state) FROM customers;

SELECT CONCAT(city,','state) AS new_address FROM customers;

SELECT name,cost,cost-1 AS sale_proce FROM items;


* MYSQL FUNCTION:

SELECT name,UPPER(name) FROM customers;
SELECT AVG(cost) FROM items;
SELECT SUM(bids) FROM items;

SELECT COUNT(name)  FROM items WHERE seller_id=6;

SELECT seller_id,COUNT(NAME) FROM items GROUP BY seller_id ORDER BY seller_id;

SELECT AVG(cost) FROM items WHERE seller_id=6;

SELECT COUNT(*) AS item_count, MAX(cost) AS max, AVG(cost) AS avg FROM items WHERE seller_id=12;


* GROUP BY:

(instead of WHERE multiple time, we can use GROUP BY )

SELECT seller_id,COUNT(*) AS item_count FROM items GROUP BY seller_id;

[ HAVING is something like WHERE in GROUP BY, so we have to use HAVING in a GROUP BY query. ]

SELECT seller_id,COUNT(*) AS item_count FROM items GROUP BY seller_id HAVING COUNT(*) >=3;

[ So, in GROUP BY use HAVING ]

SELECT seller_id,COUNT(*) AS item_count FROM items GROUP BY seller_id HAVING COUNT(*) >=3 ORDER BY items_count DESC;


*) subquery:

Lets say list those items that's cost is more then the AVG(cost);

- Mysql work like inside-out, so first () and then rest;

SELECT name,cost FROM items WHERE cost > (SELECT AVG(cost)FROM items) ORDER By cost DESC;

query(subquery)


*) mysql join tables:

SELECT cousters.id,customers.name,items.name,item.cost FROM customers,items WHERE customers.id=items.seller_id ORDER BY customers.id;

* using AS we can also give a table 'nick name' not just column.

SELECT i.seller_id,i.name,c.id FROM customers AS C, items AS i WHERE i.seller_id=c.id


* Outer joins:

Inner join: when we have to column(from different table) and we want to match them together.

[ customers.id=items.seller_id => both should have values. ]

Outer joins:

example: I want to list the customers.name  even if they are not selling or list even items, that are not getting sold by any customers now.

SELECT customers.name, items.name FROM customers LEFT OUTER JOIN  items ON customers.id=items.seller_id;

[ will list coustomers even if its not there in items table. (because customers is in the left side of LEFT OUTER JOIN)

To include all the rows from the table in LEFT.

The other one:

SELECT customers.name, items.name FROM customers RIGHT OUTER JOIN items ON customers.id=seller_id;


*) UNION:

SELECT name,cost,bids FROM items WHERE bids > 190 OR cost>1000 ORDER BY cost;


But if its become more complex:

SELECT name,cost,bids FROM items WHERE bids>190
UNION
SELECT name,cost,bids FROM items WHERE cost>1000 ORDER BY cost;


taking multiple queries & getting them into one result set. (UNION):
for every UNION column's need to be same.
By default is removes the duplicate entries.
If you don't want to remove duplicate then use UNION ALL in place of UNION


* Fully-text searching:

ALTER TABLE items ADD FULLTEXT(name);

DESC items;  -> it you see KEY column for name row we have an INDEX having MUL.


SELECT name,cost FROM items WHERE MATCH(name) AGAINST('baby');

[no regexp or wild card over here. ]

IT do the ranking too & faster.

SELECT name,cost FROM items WHERE MATCH(name) AGAINST('+baby -coat' IN BOOLEAN MODE)




################################################

*) INSERT:

INSERT INTO items(id,name,cost,seller_id,bids) VALUES('102','fish','10','1',0');

*)  Multiple Row Insert:

INSERT INTO items(id,name,cost,seller_id,bids)
VALUES
('103','apple','1','1','0'),
('104','shoe','ro','1','0'),
('105','ring','100','1','0');

INSERT INTO items(id,name,cost,seller_id,bids) SELECT id,name,cost,seller_id,bids FROM AnotherTable;


*) UPDATE:

(Best to have LIMIT too) for to be safe.

UPDATE items SET name='applecake' WHERE id=103;
UPDATE items SET name='bananna',cost='2' WHERE id=103 LIMIT 1;

UPDATE user SET Password=PASSWORD('new-password') WHERE User='root';


DELETE FROM items WHERE id=103 LIMIT 1;

*) use PRIMARY KEY in UPDATE or DELETE to be safe.





#####################################

CREATE TABLE:

CREATE TABLE user(
id int,
Username varchar(30),
Password varchar(20),
PRIMARY KEY(id)
);



*)

CREATE TABLE classics(
auther varchar(128),
title varchar(128),
category varchar(16),
year smallINT,
isbn char(13),
INDEX (auther(20)),
INDEX (title(20)),
INDEX (category(4)),
INDEX (year),
PRIMARY KEY (isbn)
) ENGINE MyISAM;


#####################################


*) ALTER/DROP/RENAME Table:

ALTER TABLE user ADD address varchar(30);

ALTER TABLE user DROP  column address;


*) TO DROP A TABLE:

 DROP TABLE user_old1;

*) Too Rename A TABLE:

RENAME TABLE user1 to users;



###########################################

*) views:

  - temporary  table;
  - don't carry any own data, its a tempory table created by other tables;

CREATE VIEW mostbids AS
SELECT id,name,bids FROM items ORDER BY bids DESC LIMIT 10;

[ top 10 bids, dynamic tables. ]
[NOTE: After AS is query and the view name over here its before AS. ]


CREATE VIEW customers AS SELECT * FROM user;

[ Over here if you delete some thing from this view table, then it will update as original table too. like sym link? ]

CREATE VIEW address AS SELECT CONCAT(city,',',state) AS fulladdress FROM users;

[ view don't take any memory. ]


#################################################

Few more topics:

trigger, cursors, store procedures.


##################################################















Tuesday, 17 June 2014

quick mysql master slave configuration

NOTE: I am using ubuntu as my OS with mysql 5.5.
On master server:
[1] sudo vi /etc/mysql/my.cnf
# under [mysqld] add the following line, considering you have the default lines:
log-bin = master-bin
log-bin-index = master-bin.index
server_id = 1
[2] # Update your bind-address so that any one can access the db. You can also update your firewall accordingly:
bind-address = 0.0.0.0         # NOTE: you can comment the bind-address line too.

[3] mysql> create user replicaiton_user
  grand replication slave on *.*
  to replication_user identified by 'your_password';

[4]: Restart the mysql:  " sudo service mysql restart "
[ You can have a look at /var/log/mysql/error/log mean while if you face any issue. ]

Configuring SLAVE:
[1]: sudo vi /etc/mysql/my.cnf
# under [mysqld] add the following line, considering you have the default lines:
relay_log = slave-relay-bin
relay_log_index = slave-relay-bin.index
server-id = 2

[2] Can comment the bind-address = 127.0.0.1 [ so that your application can access the slave system as a read only access. ]

[3] sudo service mysql restart

[4] mysql> change master to
  master_host = 'your_master_host',
  master_port = 'port_number_where_master_db_is_running',
  master_user = 'replication_user',
  master_password = 'your_replication_user's_password'
[5] > start slave;
[6] > show slave status;
[7]: Any further issue, check: /var/log/mysql/error.log

Wednesday, 28 May 2014

python for mysql

# You might need the following module to be installed in the system:
MySQL-python

So you can try using " sudo pip install MySQL-python " or " sudo easy-install MySQL-python"

if you are getting error while installing MySQL-python [ apt-get install python-mysqldb ]

#!/usr/bin/env python
import MySQLdb

db = MySQLdb.connect(host="hostname", # your host, usually localhost
                     user="username", # your username
                      passwd="password", # your password
                      db="database") # name of the data base

# you must create a Cursor object. It will let
#  you execute all the queries you need
cur = db.cursor()

# Use all the SQL you like
cur.execute("select * from Country where Code2 = 'IN';")

# print all the first cell of all the rows
for row in cur.fetchall() :
#    print row[0]
    rows = row   
    print(type(rows))  
    MyList=list(rows)
    print(type(MyList))
    print('Following is in tuple format')
    print(rows)
    print('Following is the list format')
    print(MyList)
#    print row


############ There are other module for connecting the mysql, with different classes." ####

mysql-connector-python


you can try, in python prompt:

>>> dir(MySQLdb) ## to see the modules classes.

## to see installed python module: ## pip list


external link:
http://mysql-python.sourceforge.net/MySQLdb.html

Sunday, 26 January 2014

mysql Notes1

mysql Notes1:

001. To Login to mysql: By default there is no username and password require for normal login:
$ mysql [ This will give you the mysql prompt like: mysql> ]
$ mysql -u mysql [ login as mysql user, by default mysql user don't have any password ]

002. To list the databases;
$ show databases;
NOTE: Every mysql command need to finish with ; or with \G
NOTE: [ This will list the databases on your DataBase Management System -> DBMS ]


003. Log in as mysql root user:
$ mysql -u root

004. To change the root password: [ By default root don't have password. ] [ Login to mysql, first. ]
mysql> UPDATE mysql.user SET Password = PASSWORD('fooBar') WHERE User = 'root';
mysql> FLUSH PRIVILEGES;
NOTE:
a. mysql is the database name and user is the table where the user's informations are there.
b. PASSWORD is the function that encript the password.
c. There are more then one root by default: [ localhost, hostname and so on... like ipv4 and like that. ]
d. FLUSH PRIVILEGES [ To read the mysql permission. ]

005. Creating a user:
mysql> CREATE USER username@Hostname
mysql> CREATE USER apache@localhost;
mysql> CREATE USER rootBackup@localhost;
NOTE: This will allow that user only from that Host.

006. Granting the access:
mysql> GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, FILE, INDEX, ALTER, CREATE TEMPORARY TABLES, CREATE VIEW, SHOW VIEW, CREATE ROUTINE, ALTER ROUTINE, EXECUTE ON *.* TO apache@localhost;
mysql> GRANT ALL ON *.* TO rootBackup@localhost WITH GRANT OPTION;
mysql> FLUSH PRIVILEGES;

007. Now setting the password:
mysql> UPDATE mysql.user SET Password = PASSWORD('fooBar') WHERE user = 'apache';
mysql> UPDATE mysql.user SET Password = PASSWORD('fooBar') WHERE user = 'rootBackup';



Friday, 14 June 2013

Retrive mysql root login


Retrive_mysql_root_login

Recover mysql root login:
1.
# /etc/init.d/mysql stop
or service mysqld stop
2.
# mysqld_safe --skip-grant-tables &
3. 
# mysql -u root
4.
mysql> use mysql;
mysql> update user set password=PASSWORD("NEW-ROOT-PASSWORD") where User='root';
mysql> flush privileges;
mysql> quit
5.
# /etc/init.d/mysql stop
or service mysqld stop
6.
# /etc/init.d/mysql start or service mysqld start
# mysql -u root -p
<enter the new password> 

*) How to change mysql root password:
$ mysqladmin -u root password NEWPASSWORD
to update the password: $ mysqladmin -u root -p'oldpassword' password newpass

example: $ mysqladmin -u root -p'abc' password '123456'
changing the password of the other user:
$ mysqladmin -u <username> -p oldpassword password newpass

Changing MySQL root user password using MySQL sql command
$ mysql -u root -p
mysql> use mysql;
mysql> update user set password=PASSWORD("NEWPASSWORD") where User='user_name';
mysql> flush privileges;
mysql> quit



*) original link from:

http://www.cyberciti.biz/faq/mysql-change-root-password/
http://www.cyberciti.biz/tips/recover-mysql-root-password.html


mysql perfomance_tunning


perfomance_tunning

Do check things on key_buffer_size query_check_size and query_cache_type.

For a 8GB RAM system we can set the following to get few improvements:

to check the current values (at) mysql prompt:
show variables like 'key_buffer_size';
show variables like 'query_cache_size';
show variables like 'query_cache_type';

to update the values:
set global key_buffer_size=3221225472;                         [ 3 GB ]
set global query_cache_size=268435456;                        [256 MB ]
set global query_cache_type=ON;                

Do monitor the memory information such as swap and physical memory after changes and you might like to check the slow query log file to find still the same query is taking long time or not.

Mysql_Notes2


Mysql_Notes

Note: On a System we can have multiple DB [ database ] and the default one is mysql. Let's say along with mysql we have one more database known as "MYDB" and if you need to grant some permission to someone on the "MYDB" then all those credentials stays as "mysql" database. 

The username and password stay at "user" table where as the database information stays at "db" tables.

1. mysql -uroot -p -h localhost
2. show databases;
3. create database world;
4. use world;
5. source /home/amit/world.sql [ download the same database from http://dev.mysql.com/doc/index-other.html -> MyISAM or Innodb]
6. show tables;

NOTE: "$" -> unix console and ">" -> mysql console [ default is mysql console ]

7. Communication protocols: [ protocol - Types of connections - OS ]
7a. TCP/IP - local,remote - All
7b. Unix socket file - local only - Unix only
7b. Shared memory - local only - windows only
7d. Named pipes - local only - windows only 

8. Table names are case sensitivity.

9. Default installation location: /usr/local/mysql [ currently in my system its at: /var/lib/mysql] and the files I can see with in /var/lib/mysql/world: 
a. City.frm
b. City.MYD
c. City.MYI
d. Country.frm
e. Country.MYD
f. Country.MYI
g. CountryLanguage.frm
h. CountryLanguage.MYD
i. CountryLanguage.MYI
j. db.opt

10. Basic commands that must be executed to install  a MySQL source distribution:
shell> groupadd mysql 
shell> useradd -g mysql mysql 
shell> gunzip < mysql-VERSION.tar.gz | tar -xvf - 
shell> cd mysql-VERSION 
shell> ./configure --prefix=/usr/local/mysql 
shell> make 
shell> make install 
shell> cp support-files/my-medium.cnf /etc/my.cnf 
shell> cd /usr/local/mysql 
shell> chown -R mysql . 
shell> chgrp -R mysql . 
shell> scripts/mysql_install_db --user=mysql 
shell> chown -R root . 
shell> chown -R mysql var 
shell> bin/mysqld_safe --user=mysql &

11. mysql> select @@global.time_zone, @@session.time_zone;  [ to know the current values of the global and client-specific time zones ]


12. mysql -uroot -p
13. use mysql
14. show tables like 'time_zone%' [ show the system tables that have somethings to do with time zones ]

mysql> show tables like 'time_zone%';
+------------------------------+
| Tables_in_mysql (time_zone%) |
+------------------------------+
| time_zone                    | 
| time_zone_leap_second        | 
| time_zone_name               | 
| time_zone_transition         | 
| time_zone_transition_type    | 
+------------------------------+
5 rows in set (0.00 sec)

15. describe time_zone\G [ display the design of the tabl time_zone ] -> this specific listing shows that there are two columns in the "time_zone" table with the details of what each column stores and how it stores it.

mysql> describe time_zone\G;
*************************** 1. row ***************************
  Field: Time_zone_id
   Type: int(10) unsigned
   Null: NO
    Key: PRI
Default: NULL
  Extra: auto_increment
*************************** 2. row ***************************
  Field: Use_leap_seconds
   Type: enum('Y','N')
   Null: NO
    Key: 
Default: N
  Extra: 
2 rows in set (0.00 sec)


16. select * from time_zone; [ display the contents of the time_zone ]

17. Different method of starting mysql server on linux:

a. mysqld: Invoke manually for debugging.
b. mysqld_safe: launches, monitors and restarts mysqld if needed.
c. mysql.server: wrapper for mysqld_safe for O/S's using system V
d. mysqld_multy: perl script for managing myltiple servers.

18. On linux: to start and stop: 

a. /etc/rc.d/init.d/mysql start
b. /etc/rc.d/init.d/mysql stop

19. If the server does not start properly, look in the error log. The default error log name on Linux is host_name.err in the data directory, where host_name is the name of the server host.

20. Method and Descriptions:
a. mysqladmin: Connects to server as client to shutdown server local or remote.
b. mysql.server: Will stop and/or shutdown the local server
c. mysqld_multi: Invokes mysqladmin to stop and/or shutdown servers it manages.

21. mysqlcheck --check-upgrade --all-databases --auto-repair mysql_fix_privilege_tables [ To check and repair tables and to upgrade the system tables ]
22. help <mysql_command> [ to get help on the mysql_command ]
23. mysql_upgrade --help

24. $mysql --help;

25. Few and/or default mysql server configuration:

[on windows]
a. basedir:
b. datadir:
c. shared-memory
d. enable-named-pipe
e. general_log
f. log-bin
g. slow_query_log=[1|0] -> [ 1=enable, 0=disable]
h. default-storage-engine=InnoDB
i. max_connections=200
j. key_buffer_size=128M
k. slow_query_log_file

26. $my_print_defaults [ display the options that are present in option group of the option files]
27. $mysql --print-defaults [ the same option file information can also be listed from the command ]

28. show variables like 'bulk%';
29. set global
30. set session
31. set bulk_insert_buffer_size=4100000;

32. show global status; [ display the status values for all connections to mysql]
33. show status [ provides server status information ]
34. show session status; [ display the status values for the current connection]

35. sql-mode=IGNORE_SPACE [ Setting the SQL mode ]
36. set [session|global] sql_mode='mode_value'
37. select @@sql_mode; [ to check the current sql_mode settings ] 
38. set sql_mode='TRADITIONAL'; [ to set sql_mode to traditional ]

39. SQL MODE Values:
a. ANSI_QUOTES: This mode causes the double quote character (‘"’) to be interpreted as an identifier- quoting character rather than as a string-quoting character.

b. IGNORE_SPACE: By default, functions must be written with no space between the function name and the following parenthesis. Enabling this mode causes the server to ignore spaces after function names. This allows spaces to appear between the name and the parenthesis, but also causes function names to be reserved words.

c. ERROR_FOR_DIVISION_BY_ZERO: By default, division by zero produces a result of NULL and is not treated specially. Enabling this mode causes division by zero in the context of inserting data into tables to produce a warning, or an error in strict mode.

d. STRICT_TRANS_TABLES, STRICT_ALL_TABLES: These values enable "strict mode", which imposes certain restrictions on what values are acceptable as database input. By default, MySQL is forgiving about accepting values that are missing, out of range, or malformed. Enabling strict mode causes bad values to be treated as erroneous. STRICT_TRANS_TABLES enables strict mode for transactional tables, and STRICT_ALL_TABLES enables strict mode for all tables.

e. TRADITIONAL: Enables strict modes plus several restrictions on acceptance of input data. Enforces restrictions on input data values that are like other database servers, rather than MySQL's more forgiving behavior. Allows user accounts to be created only with the GRANT statement when a password is specified

f. ANSI: This is a composite mode that causes MySQL server to be more "ANSI-like". That is, it enables behaviors that are more like standard SQL, such as ANSI_QUOTES (described earlier) and PIPES_AS_CONCAT, which causes || to be treated as the string concatenation operator rather than logical OR.

command mysqldumpslow


mysqldumpslow

syntax: 
mysqldumpslow <mysql_slow_log_file>

mysqldumpslow -s at <mysql_slow_log_file>

Count: 438  Time=5.80s (2540s)  Lock=0.00s (0s)  Rows=4331.9 (1897393), moviesfe[moviesfe]@4hosts

Notes on mysqldumpslow:
NOTE: -s t [ t for count => sort on hight count of a same query ]
NOTE: -s at [ at for time => sort on hight time take query ]
NOTE: -s l [ sort bashed on the query those lock the table ]
NOTE: -s al [ Sort on lock and large query size -> do a recheck, might be large query size ]
NOTE: -s r [ bashw=ed on row]

is in order:
t=count, at=time, l=lock, al=rows, r=

$mysqldumpslow --help
Usage: mysqldumpslow [ OPTS... ] [ LOGS... ]

Parse and summarize the MySQL slow query log. Options are

  --verbose    verbose
  --debug      debug
  --help       write this text to standard output

  -v           verbose
  -d           debug
  -s ORDER     what to sort by (t, at, l, al, r, ar etc), 'at' is default
  -r           reverse the sort order (largest last instead of first)
  -t NUM       just show the top n queries
  -a           don't abstract all numbers to N and strings to 'S'
  -n NUM       abstract numbers with at least n digits within names
  -g PATTERN   grep: only consider stmts that include this string
  -h HOSTNAME  hostname of db server for *-slow.log filename (can be wildcard),
               default is '*', i.e. match all
  -i NAME      name of server instance (if using mysql.server startup script)
  -l           don't subtract lock time from total time

command mysqlbinlog

mysqlbinlog

1. --start-datetime and --stopdatetime syntax:

 mysqlbinlog --start-datetime="11/09/06 04:00" --stop-datetime="11/09/06 05:00" <binfile_name> 

Following command also works:

 mysqlbinlog <binfile_name> --start-datetime="11/09/06 04:00" --stop-datetime="11/09/06 05:00"

 mysqlbinlog -d <dbname> <binfile_name> 


mysqlbinlog -help
mysqlbinlog Ver 3.0 for unknown-linux-gnu at x86_64
By Monty and Sasha, for your professional use
This software comes with NO WARRANTY:  This is free software,
and you are welcome to modify and redistribute it under the GPL license

Dumps a MySQL binary log in a format usable for viewing or for piping to
the mysql command line client

Usage: mysqlbinlog [options] log-files
  -d, --database=name List entries for just this database (local log only).
  -D, --disable-log-bin 
                      Disable binary log. This is useful, if you enabled
                      --to-last-log and are sending the output to the same
                      MySQL server. This way you could avoid an endless loop.
                      You would also like to use it when restoring after a
                      crash to avoid duplication of the statements you already
                      have. NOTE: you will need a SUPER privilege to use this
                      option.
  -f, --force-read    Force reading unknown binlog events.
  -?, --help          Display this help and exit.
  -h, --host=name     Get the binlog from server.
  -o, --offset=#      Skip the first N entries.
  -p, --password[=name] 
                      Password to connect to remote server.
  -P, --port=#        Use port to connect to the remote server.
  -j, --position=#    Deprecated. Use --start-position instead.
  --protocol=name     The protocol of connection (tcp,socket,pipe,memory).
  -r, --result-file=name 
                      Direct output to a given file.
  -R, --read-from-remote-server 
                      Read binary logs from a MySQL server
  --open_files_limit=# 
                      Used to reserve file descriptors for usage by this
                      program
  --set-charset=name  Add 'SET NAMES character_set' to the output.
  -s, --short-form    Just show the queries, no extra info.
  -S, --socket=name   Socket file to use for connection.
  --start-datetime=name 
                      Start reading the binlog at first event having a datetime
                      equal or posterior to the argument; the argument must be
                      a date and time in the local time zone, in any format
                      accepted by the MySQL server for DATETIME and TIMESTAMP
                      types, for example: 2004-12-25 11:25:56 (you should
                      probably use quotes for your shell to set it properly).
  --stop-datetime=name 
                      Stop reading the binlog at first event having a datetime
                      equal or posterior to the argument; the argument must be
                      a date and time in the local time zone, in any format
                      accepted by the MySQL server for DATETIME and TIMESTAMP
                      types, for example: 2004-12-25 11:25:56 (you should
                      probably use quotes for your shell to set it properly).
  --start-position=#  Start reading the binlog at position N. Applies to the
                      first binlog passed on the command line.
  --stop-position=#   Stop reading the binlog at position N. Applies to the
                      last binlog passed on the command line.
  -t, --to-last-log   Requires -R. Will not stop at the end of the requested
                      binlog but rather continue printing until the end of the
                      last binlog of the MySQL server. If you send the output
                      to the same MySQL server, that may lead to an endless
                      loop.
  -u, --user=name     Connect to the remote server as username.
  -l, --local-load=name 
                      Prepare local temporary files for LOAD DATA INFILE in the
                      specified directory.
  -V, --version       Print version and exit.

Variables (--variable-name=value)
and boolean options {FALSE|TRUE}  Value (after reading options)
--------------------------------- -----------------------------
database                          (No default value)
disable-log-bin                   FALSE
force-read                        FALSE
host                              elp
offset                            0
port                              3306
position                          4
read-from-remote-server           FALSE
open_files_limit                  64
set-charset                       (No default value)
short-form                        FALSE
socket                            /tmp/mysql.sock
start-datetime                    (No default value)
stop-datetime                     (No default value)
start-position                    4
stop-position                     18446744073709551615
to-last-log                       FALSE
user                              (No default value)
local-load                        (No default value)



mysql simple table creation


Creating a new table with the time value: and then entering value of the current time too:

* create table <table_name>  (TIMESTAMP timestamp NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP, TRAFFIC int(4), RES_TIME_MS int(5));
* insert into <table_name> value(now(), 17,65);
- From command line updating the value:
* mysql -uroot -p<password> -e "use <database_name>; insert into <table_name> value(now(), 17,65)"
* mysql -uroot -e "insert into <database_name>.<table_name> vaule(now(),17,70)"
* mysql -uroot -e "select * from <database_name>.<table_name>" 

mysql QA2


6) How to create MYSQL new users?
There are many different ways to establish users and privileges in MYSQL. Client and GRANT command assure you about a safe connection. The syntax for establishing new users and privileges is as follows
GRANT privileges ON database.* TO
‘username’@’hostname’ This can be identified by the password. Privileges can be assigned one by one or by specifying all.

7) Explain about the rules which should be followed while assigning a username?
A username has a maximum length of 16 characters. Spaces should be avoided while creating username because they are case sensitive. Hostname will be the computer from which you are going to connect. The best way to specify a username is to connect through a local host.

8) Explain about a security flaw which is present while creating a username?
Naming MYSQL databases has to be very careful because any database starting with the test name can be accessed by every one on the network. Make sure that you don’t start the databases naming with test. It should be used only for experimental purposes only.

9) State some security recommendations while using MYSQL?
Some of the security recommendations which should be followed while using MYSQL are as follows: -
1) Minimal privileges to users in the network.
2) Super and process privileges should be granted minimally.
3) File privileges should be granted minimally to administrators.
4) Validation of data and queries should be thoroughly checked.

10) Explain about database design?
Database design is also called as Data modeling. It is used for long-term management of database. This process is used to store information and to keep data for long term. Creating an efficient structure helps you to channelize information into good channels.

11) Explain about creating database?
CREATE DATABASE command will create you a database with the assigned name by the user. This is an optional statement but when you actually assign a name it checks for similarity and gives error if it encounters one. CREATE DATABASE models help you to create classic models.

12) Explain about primary keys?
MYSQL allows only one primary key. This primary key can be used on multiple tables. There are many rules which should be followed such as it shouldn’t be null and it can never change. Primary key assigned should be unique it cannot be matched with any other keys.

13) Explain about normalization?
Applying specific rules (normal forms) to the database is the primary process. These rules should be applied in the order specified starting with the first normal form. These rules should be adhered by every database they are
1) Each column should have only one value
2) Repeating columns of data cannot be done.

14) State two considerations which can improve the performance of MYSQL?
Two considerations which can improve the performance of MYSQL are as follows: -
1) Fixed length fields take up more space than variable length fields but they are a bit faster.
2) Size of the field should be restricted to the smallest possible value based upon the largest input value.

15) Explain about the time stamp field?
TIMESTAMP filed occurs when an INSERT and UPDATE field occurs when there is no value specified for the field. There are many behaviors for TIMESTAMP field and it depends upon the version of MYSQL.

16) Explain about MyISAM table?
This feature is a default type for tables. This table is not so much considered for transactions because it is not considered as safe but this kind of table is very fast in execution. The maximum key length is 1024 bytes and 64 keys per table. Size of this table entirely depends upon the operating system.

17) Explain about HEAP table?
This type of table is stored in the memory. Speed of execution of this table is very commendable. There are associated disadvantages associated with this table the primary one being loss of stored memory which occurs when there is power failure and can cause the server to run out of memory. Columns with AUTO_INCREMENT, TEXT characteristics and BLOB are not supported. 


Mysql QA1


1. How to log-in to mysql server?
mysql -h<hostname> -u<username> -p [ for the password]

   2. How do you start MySQL on Linux? 
/etc/init.d/mysql start

   3. Explain the difference between mysql and mysqli interfaces in PHP? 
mysql is the object-oriented version of mysql library functions.

   4. What’s the default port for MySQL Server?
 3306

   5. What does tee command do in MySQL? 
tee followed by a filename turns on MySQL logging to a specified file. It can be stopped by command notee.

   6. Can you save your connection settings to a conf file?
Yes, and name it ~/.my.conf. You might want to change the permissions on the file to 600, so that it’s not readable by others.

   7. How do you change a password for an existing user via mysqladmin?
mysqladmin -u root -p password "newpassword"

   8. Use mysqldump to create a copy of the database?
mysqldump -h mysqlhost -u username -p mydatabasename > dbdump.sql

   9. Have you ever used MySQL Administrator and MySQL Query Browser? 
Describe the tasks you accomplished with these tools.

  10. What are some good ideas regarding user security in MySQL? 
There is no user without a password. There is no user without a user name. There is no user whose Host column contains % (which here indicates that the user can log in from anywhere in the network or the Internet). There are as few users as possible (in the ideal case only root) who have unrestricted access.

  11. Explain the difference between MyISAM Static and MyISAM Dynamic.
 In MyISAM static all the fields have fixed width. The Dynamic MyISAM table would include fields such as TEXT, BLOB, etc. to accommodate the data types with various lengths. MyISAM Static would be easier to restore in case of corruption, since even though you might lose some data, you know exactly where to look for the beginning of the next record.

  12. What does myisamchk do?
It compressed the MyISAM tables, which reduces their disk usage.

  13. Explain advantages of InnoDB over MyISAM? 
Row-level locking, transactions, foreign key constraints and crash recovery.

  14. Explain advantages of MyISAM over InnoDB?
Much more conservative approach to disk space management - each MyISAM table is stored in a separate file, which could be compressed then with myisamchk if needed. With InnoDB the tables are stored in tablespace, and not much further optimization is possible. All data except for TEXT and BLOB can occupy 8,000 bytes at most. No full text indexing is available for InnoDB. TRhe COUNT(*)s execute slower than in MyISAM due to tablespace complexity.

  15. What are HEAP tables in MySQL?
HEAP tables are in-memory. They are usually used for high-speed temporary storage. No TEXT or BLOB fields are allowed within HEAP tables. You can only use the comparison operators = and <=>. HEAP tables do not support AUTO_INCREMENT. Indexes must be NOT NULL.
  
  16. How do you control the max size of a HEAP table?
MySQL config variable max_heap_table_size.
  
  17. What are CSV tables?
Those are the special tables, data for which is saved into comma-separated values files. They cannot be indexed.
  
  18. Explain federated tables.
Introduced in MySQL 5.0, federated tables allow access to the tables located on other databases on other servers.
  
  19. What is SERIAL data type in MySQL?
BIGINT NOT NULL PRIMARY KEY AUTO_INCREMENT
  
  20. What happens when the column is set to AUTO INCREMENT and you reach the maximum value for that table?
It stops incrementing. It does not overflow to 0 to prevent data losses, but further inserts are going to produce an error, since the key has been used already.
  
  21. Explain the difference between BOOL, TINYINT and BIT.
Prior to MySQL 5.0.3: those are all synonyms. After MySQL 5.0.3: BIT data type can store 8 bytes of data and should be used for binary data.
  
  22. Explain the difference between FLOAT, DOUBLE and REAL.
FLOATs store floating point numbers with 8 place accuracy and take up 4 bytes. DOUBLEs store floating point numbers with 16 place accuracy and take up 8 bytes. REAL is a synonym of FLOAT for now.
  
  23. If you specify the data type as DECIMAL (5,2), what’s the range of values that can go in this table?
999.99 to -99.99. Note that with the negative number the minus sign is considered one of the digits.
  
  24. What happens if a table has one column defined as TIMESTAMP?
That field gets the current timestamp whenever the row gets altered.
  
  25. But what if you really want to store the timestamp data, such as the publication date of the article? Create two columns of type TIMESTAMP and use the second one for your real data.
  
  26. Explain data type TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
The column exhibits the same behavior as a single timestamp column in a table with no other timestamp columns.
  
  27. What does TIMESTAMP ON UPDATE CURRENT_TIMESTAMP data type do?
On initialization places a zero in that column, on future updates puts the current value of the timestamp in.
  
  28. Explain TIMESTAMP DEFAULT ‘2006:09:02 17:38:44′ ON UPDATE CURRENT_TIMESTAMP.
A default value is used on initialization, a current timestamp is inserted on update of the row.
  
  29. If I created a column with data type VARCHAR(3), what would I expect to see in MySQL table? CHAR(3), since MySQL automatically adjusted the data type.

Tuesday, 25 December 2012

Recovering MySQL root password

You can recover MySQL database server password with following five easy steps.

Step # 1: Stop the MySQL server process.
Step # 2: Start the MySQL (mysqld) server/daemon process with the –skip-grant-tables option so that it will not prompt for password
Step # 3: Connect to mysql server as the root user
Step # 4: Setup new root password
Step # 5: Exit and restart MySQL server

Here are commands you need to type for each step (login as the root user):
Step # 1 : Stop mysql service
# /etc/init.d/mysql stop
Stopping MySQL database server: mysqld.
Step # 2: Start to MySQL server w/o password
# mysqld_safe –skip-grant-tables &

Starting mysqld daemon with databases from /var/lib/mysql
mysqld_safe[PID]: started
Step # 3: Connect to mysql server using mysql client
# mysql -u root

mysql>
Step # 4: Setup new MySQL root user password
mysql> use mysql;
mysql> update user set password=PASSWORD(”NEW-ROOT-PASSWORD“) where User=’root’;
mysql> flush privileges;
mysql> quit

Step # 5: Stop MySQL Server:
# /etc/init.d/mysql stop
Step # 6: Start MySQL server and test it
# /etc/init.d/mysql start
# mysql -u root -p