INTRODUCTION
Many of the applications that a Web developer wants to use can be made
easier by the use of a standardized database to store, organize, and
access information. MySQL is an Open Source (GPL) Standard Query
Language (SQL) database that is fast, reliable, easy to use, and suitable
for applications of any size. SQL is the ANSI-standard database query
language used by most databases (though all have their nonstandard
extensions).
MySQL can easily be integrated into Perl programs by using the Perl
DBI (DataBase Independent interface) module. DBI is an Application Program
Interface (API) that allows Perl to connect to and query a number of
SQL databases (among them MySQL, mSQL, PostgreSQL, Oracle, Sybase,
and Informix).
If you installed Linux as suggested in Chapter 2, MySQL and DBI are
already installed.
****************************************************************************
Install MySQL
yum install mysql-server
Configuration file for MySQL is /etc/my.cnf and Port Number : 3306
****************************************************************************
Start mysql service
service mysqld start or /etc/init.d/mysqld start
****************************************************************************
mysql -u root -p password or #mysql -u mydbadmin -p
****************************************************************************
First, try to make a connection to our MySQL server as the root
MySQL user:
$ mysql -u root
If you see the following output:
ERROR 2002: Can’t connect to local MySQL server through socket
´/var/lib/mysql/mysql.sock´(2)
it likely means the MySQL server is not running. If your system is set up
securely, it shouldn’t be running, because you had no reason, before now,
for it to be running. Use chkconfig as root to make sure it starts the next
time the machine boots, and then start it by hand as follows:
# chkconfig mysqld on
# /etc/init.d/mysqld start
****************************************************************************
[root@localhost ~]# /usr/bin/mysql_secure_installation
Enter current password for root (enter for none): enter
Set root password? [Y/n] Y
New password: ******
Re-enter new password: ******
Password updated successfully!
****************************************************************************
mysql> describe [table name];
mysql> DESCRIBE age;
+----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| f_name | varchar(20) | YES | | NULL | |
| m_name | varchar(3) | YES | | NULL | |
| l_name | varchar(35) | YES | | NULL | |
| userid | varchar(15) | YES | | NULL | |
| username | varchar(8) | YES | | NULL | |
| email | varchar(35) | YES | | NULL | |
| phone | varchar(25) | YES | | NULL | |
+----------+-------------+------+-----+---------+-------+
7 rows in set (0.00 sec)
mysql> DESCRIBE age_information;
+-----------+----------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------+----------+------+-----+---------+-------+
| lastname | char(20) | YES | | NULL | |
| firstname | char(20) | YES | | NULL | |
| age | int(11) | YES | | NULL | |
+-----------+----------+------+-----+---------+-------+
3 rows in set (0.01 sec)
The command SHOW COLUMNS FROM age information; gives the same information
****************************************************************************
The DELETE Command
Sometimes we need to delete a record from the table (don’t assume the
worst—perhaps the person just asked to be removed from a mailing list,
which was opt-in in the first place, of course). This is done with the DELETE
command:
Delete Multiple Queries,
mysql>select concat('CALL mysql.rds_kill(',id,');') from information_schema.processlist where user='user name';
mysql> drop table [table name];
mysql> show full processlist;
mysql> show processlist;
****************************************************************************
check the status of the slave
1 row in set (0.00 sec)
Give Privileges
mysql> FLUSH PRIVILEGES;
Show all data in a table.
mysql> SELECT * FROM [table name];
mysql> SELECT * FROM information_schema.user_privileges;
mysql> GRANT permission ON database.table TO 'user'@'localhost' IDENTIFIED BY 'mypass';;
ALL – Allow complete access to a specific database. If a database is not specified, then allow complete access to the entirety of MySQL.
CREATE – Allow a user to create databases and tables.
DELETE – Allow a user to delete rows from a table.
DROP – Allow a user to drop databases and tables.
EXECUTE – Allow a user to execute stored routines.
GRANT OPTION – Allow a user to grant or remove another user’s privileges.
INSERT – Allow a user to insert rows from a table.
SELECT – Allow a user to select data from a database.
SHOW DATABASES- Allow a user to view a list of all databases.
UPDATE – Allow a user to update rows in a table.
****************************************************************************
mysqladmin -u root password “newpassword”
mysqladmin -u root -h host_name password “newpassword”
****************************************************************************
Create user for client
CREATE USER 'dev'@'192.168.x.x' IDENTIFIED BY 'dev123';
***************************************************************************
check io status on mysql
#mysql -u root -p xxxxx
#show slave status \G;
#check Slave_IO_Running: Yes or not
#mysql> show full processlist \G; ------ check full process list
****************************************************************************
Many of the applications that a Web developer wants to use can be made
easier by the use of a standardized database to store, organize, and
access information. MySQL is an Open Source (GPL) Standard Query
Language (SQL) database that is fast, reliable, easy to use, and suitable
for applications of any size. SQL is the ANSI-standard database query
language used by most databases (though all have their nonstandard
extensions).
MySQL can easily be integrated into Perl programs by using the Perl
DBI (DataBase Independent interface) module. DBI is an Application Program
Interface (API) that allows Perl to connect to and query a number of
SQL databases (among them MySQL, mSQL, PostgreSQL, Oracle, Sybase,
and Informix).
If you installed Linux as suggested in Chapter 2, MySQL and DBI are
already installed.
****************************************************************************
Install MySQL
yum install mysql-server
Configuration file for MySQL is /etc/my.cnf and Port Number : 3306
****************************************************************************
Start mysql service
service mysqld start or /etc/init.d/mysqld start
****************************************************************************
mysql -u root -p password or #mysql -u mydbadmin -p
****************************************************************************
First, try to make a connection to our MySQL server as the root
MySQL user:
$ mysql -u root
If you see the following output:
ERROR 2002: Can’t connect to local MySQL server through socket
´/var/lib/mysql/mysql.sock´(2)
it likely means the MySQL server is not running. If your system is set up
securely, it shouldn’t be running, because you had no reason, before now,
for it to be running. Use chkconfig as root to make sure it starts the next
time the machine boots, and then start it by hand as follows:
# chkconfig mysqld on
# /etc/init.d/mysqld start
****************************************************************************
Set mysql password and secure mysql
[root@localhost ~]# /usr/bin/mysql_secure_installation
Enter current password for root (enter for none): enter
Set root password? [Y/n] Y
New password: ******
Re-enter new password: ******
Password updated successfully!
Remove anonymous users? [Y/n] Y
Disallow root login remotely? [Y/n] Y
Remove test database and access to it? [Y/n] Y
Reload privilege tables now? [Y/n] Y
All done! If you've completed all of the above steps, your MySQL
installation should now be secure.
****************************************************************************
What if i forgot the root PWD of mysql? OR how to recover mysql root password?
1) #cd /usr/bin/ (from this path type CMD of mysqld_safe)
2) #/etc/init.d/mysqld stop (before running below CMD we have to stop mysql )
3) #mysqld_safe --skip-grant-tables & (grant tables allows you to do password less login into mysql)
4) #mysql -u root (by this CMD we will go to mysql CMD line)
5) >use mysql;
6) > update user set password=password("abc@123") where user='root';
(This is the CMD by which you can change the PWD of the specific user)
7) > flush privileges; (this CMD will syncall the data from RAM to HDD)
8) > quit;
****************************************************************************
The SHOW DATABASES and CREATE DATABASE Commands
First, we need to create the new database. Check the current databases to
make sure a database of that name doesn’t already exist; then create the
new one, and verify the existence of the new database:
mysql> SHOW DATABASES;
+----------+
| Database |
+----------+
| mysql |
| test |
+----------+
2 rows in set (0.00 sec)
mysql> CREATE DATABASE people;
Query OK, 1 row affected (0.00 sec)
mysql> SHOW DATABASES;
+----------+
| Database |
+----------+
| mysql |
| people |
| test |
+----------+
3 rows in set (0.00 sec)
SQL commands and subcommands (in the previous example, CREATE is
a command; DATABASE is its subcommand) are case-insensitive. The name
of the database (and table and field) are case sensitive. It’s a matter of
style whether one uses uppercase or lowercase, but traditionally the SQL
commands are distinguished by uppercase.
One way to think of a database is as a container for related tables. A
table is a collection of rows, each row holding data
First, we need to create the new database. Check the current databases to
make sure a database of that name doesn’t already exist; then create the
new one, and verify the existence of the new database:
mysql> SHOW DATABASES;
+----------+
| Database |
+----------+
| mysql |
| test |
+----------+
2 rows in set (0.00 sec)
mysql> CREATE DATABASE people;
Query OK, 1 row affected (0.00 sec)
mysql> SHOW DATABASES;
+----------+
| Database |
+----------+
| mysql |
| people |
| test |
+----------+
3 rows in set (0.00 sec)
SQL commands and subcommands (in the previous example, CREATE is
a command; DATABASE is its subcommand) are case-insensitive. The name
of the database (and table and field) are case sensitive. It’s a matter of
style whether one uses uppercase or lowercase, but traditionally the SQL
commands are distinguished by uppercase.
One way to think of a database is as a container for related tables. A
table is a collection of rows, each row holding data
****************************************************************************
The USE Command
Before anything can be done with the newly created database, MySQL has
to connect to it. That’s done with the USE command:
mysql> USE people;
Before anything can be done with the newly created database, MySQL has
to connect to it. That’s done with the USE command:
mysql> USE people;
The CREATE TABLE and SHOW TABLES Commands
Each table within the database must be defined and created. This is done
with the CREATE TABLE command.
Create a table named age information to contain an individual’s first
name, last name, and age. MySQL needs to know what kind of data can
be stored in these fields. In this case, the first name and the last name are
character strings of up to 20 characters each, and the age is an integer:
mysql>CREATE TABLE age_information (lastname CHAR(20),firstname CHAR(20),age INT);
Query OK, 0 rows affected (0.00 sec)
mysql> CREATE TABLE info (f_name VARCHAR(20),m_name VARCHAR(3),l_name VARCHAR(35),userid VARCHAR(15),username VARCHAR(8),email VARCHAR(35),phone VARCHAR(25));
Query OK, 0 rows affected (0.06 sec)
It appears that the table was created properly (it says OK after all),
but this can be checked by executing the SHOW TABLES command. If an error
is made, the table can be removed with DROP TABLE.
When a database in MySQL is created, a directory is created with the
same name as the database (people, in this example):
[root@localhost ~]# ls -l /var/lib/mysql
total 20488
-rw-rw----. 1 mysql mysql 10485760 Sep 6 15:35 ibdata1
-rw-rw----. 1 mysql mysql 5242880 Sep 6 15:35 ib_logfile0
-rw-rw----. 1 mysql mysql 5242880 Sep 6 15:35 ib_logfile1
drwx------. 2 mysql mysql 4096 Sep 6 15:35 mysql
srwxrwxrwx. 1 mysql mysql 0 Sep 6 15:35 mysql.sock
drwx------. 2 mysql mysql 4096 Sep 6 16:16 people
Within that directory, each table is implemented with three files:
[root@localhost ~]# ls -l /var/lib/mysql/people
total 20
-rw-rw----. 1 mysql mysql 65 Sep 6 16:03 db.opt
-rw-rw----. 1 mysql mysql 8768 Sep 6 16:16 info.frm
-rw-rw----. 1 mysql mysql 0 Sep 6 16:16 info.MYD
-rw-rw----. 1 mysql mysql 1024 Sep 6 16:16 info.MYI
mysql> SHOW TABLES;
+------------------+
| Tables_in_people |
+------------------+
| age_information |
+------------------+
1 row in set (0.00 sec)
This example shows two MySQL datatypes: character strings and
integers. Other MySQL data types include several types of integers
(for a complete discussion of MySQL’s data types, see www.mysql.com/
documentation/mysql/bychapter/manual Reference.html#Column types):
TINYINT −128 to 127 (signed)
or 0 to 255 (unsigned)
SMALLINT −32768 to 32767 (signed)
or 0 to 65535 (unsigned)
MEDIUMINT −8388608 to 8388607 (signed)
or 0 to 16777215 (unsigned)
INTEGER −2147483648 to 2147483647 (signed)
(same as INT) or 0 to 4294967295 (unsigned)
BIGINT −9223372036854775808 to 9223372036854775807 (signed)
or 0 to 18446744073709551615 (unsigned)
and floating points:
FLOAT
DOUBLE
REAL (same as DOUBLE)
DECIMAL
NUMERIC (same as DECIMAL)
There are several data types to represent a date:
DATE YYYY-MM-DD
DATETIME YYYY-MM-DD HH:MM:SS
TIMESTAMP YYYYMMDDHHMMSS
or YYMMDDHHMMSS
or YYYYMMDD or YYMMDD
TIME HH:MM:SS
YEAR YYYY or YY
The table age information used the CHAR character data type. The following
are the other character data types. Several have BLOB in their name—
a BLOB is a Binary Large OBject that can hold a variable amount of data.
The types with TEXT in their name are just like their corresponding BLOBs
except when matching is involved: The BLOBs are case-sensitive, and the
TEXTs are case-insensitive.
VARCHAR variable-length string up to 255 characters
TINYBLOB maximum length 255 characters
TINYTEXT
BLOB maximum length 65535 characters
TEXT
MEDIUMBLOB maximum length 16777215 characters
MEDIUMTEXT
LONGBLOB maximum length 4294967295 characters
LONGTEXT
****************************************************************************Each table within the database must be defined and created. This is done
with the CREATE TABLE command.
Create a table named age information to contain an individual’s first
name, last name, and age. MySQL needs to know what kind of data can
be stored in these fields. In this case, the first name and the last name are
character strings of up to 20 characters each, and the age is an integer:
mysql>CREATE TABLE age_information (lastname CHAR(20),firstname CHAR(20),age INT);
Query OK, 0 rows affected (0.00 sec)
mysql> CREATE TABLE info (f_name VARCHAR(20),m_name VARCHAR(3),l_name VARCHAR(35),userid VARCHAR(15),username VARCHAR(8),email VARCHAR(35),phone VARCHAR(25));
Query OK, 0 rows affected (0.06 sec)
It appears that the table was created properly (it says OK after all),
but this can be checked by executing the SHOW TABLES command. If an error
is made, the table can be removed with DROP TABLE.
When a database in MySQL is created, a directory is created with the
same name as the database (people, in this example):
[root@localhost ~]# ls -l /var/lib/mysql
total 20488
-rw-rw----. 1 mysql mysql 10485760 Sep 6 15:35 ibdata1
-rw-rw----. 1 mysql mysql 5242880 Sep 6 15:35 ib_logfile0
-rw-rw----. 1 mysql mysql 5242880 Sep 6 15:35 ib_logfile1
drwx------. 2 mysql mysql 4096 Sep 6 15:35 mysql
srwxrwxrwx. 1 mysql mysql 0 Sep 6 15:35 mysql.sock
drwx------. 2 mysql mysql 4096 Sep 6 16:16 people
[root@localhost ~]# ls -l /var/lib/mysql/people
total 20
-rw-rw----. 1 mysql mysql 65 Sep 6 16:03 db.opt
-rw-rw----. 1 mysql mysql 8768 Sep 6 16:16 info.frm
-rw-rw----. 1 mysql mysql 0 Sep 6 16:16 info.MYD
-rw-rw----. 1 mysql mysql 1024 Sep 6 16:16 info.MYI
mysql> SHOW TABLES;
+------------------+
| Tables_in_people |
+------------------+
| age_information |
+------------------+
1 row in set (0.00 sec)
This example shows two MySQL datatypes: character strings and
integers. Other MySQL data types include several types of integers
documentation/mysql/bychapter/manual Reference.html#Column types):
TINYINT −128 to 127 (signed)
or 0 to 255 (unsigned)
SMALLINT −32768 to 32767 (signed)
or 0 to 65535 (unsigned)
MEDIUMINT −8388608 to 8388607 (signed)
or 0 to 16777215 (unsigned)
INTEGER −2147483648 to 2147483647 (signed)
(same as INT) or 0 to 4294967295 (unsigned)
BIGINT −9223372036854775808 to 9223372036854775807 (signed)
or 0 to 18446744073709551615 (unsigned)
and floating points:
FLOAT
DOUBLE
REAL (same as DOUBLE)
DECIMAL
NUMERIC (same as DECIMAL)
There are several data types to represent a date:
DATE YYYY-MM-DD
DATETIME YYYY-MM-DD HH:MM:SS
TIMESTAMP YYYYMMDDHHMMSS
or YYMMDDHHMMSS
or YYYYMMDD or YYMMDD
TIME HH:MM:SS
YEAR YYYY or YY
The table age information used the CHAR character data type. The following
are the other character data types. Several have BLOB in their name—
a BLOB is a Binary Large OBject that can hold a variable amount of data.
The types with TEXT in their name are just like their corresponding BLOBs
except when matching is involved: The BLOBs are case-sensitive, and the
TEXTs are case-insensitive.
VARCHAR variable-length string up to 255 characters
TINYBLOB maximum length 255 characters
TINYTEXT
BLOB maximum length 65535 characters
TEXT
MEDIUMBLOB maximum length 16777215 characters
MEDIUMTEXT
LONGBLOB maximum length 4294967295 characters
LONGTEXT
The DESCRIBE Command
The DESCRIBE command gives information about the fields in a table. The
fields created earlier—lastname, firstname, and age—appear to have been
created correctly
The DESCRIBE command gives information about the fields in a table. The
fields created earlier—lastname, firstname, and age—appear to have been
created correctly
mysql> describe [table name];
mysql> DESCRIBE age;
+----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| f_name | varchar(20) | YES | | NULL | |
| m_name | varchar(3) | YES | | NULL | |
| l_name | varchar(35) | YES | | NULL | |
| userid | varchar(15) | YES | | NULL | |
| username | varchar(8) | YES | | NULL | |
| email | varchar(35) | YES | | NULL | |
| phone | varchar(25) | YES | | NULL | |
+----------+-------------+------+-----+---------+-------+
7 rows in set (0.00 sec)
mysql> DESCRIBE age_information;
+-----------+----------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------+----------+------+-----+---------+-------+
| lastname | char(20) | YES | | NULL | |
| firstname | char(20) | YES | | NULL | |
| age | int(11) | YES | | NULL | |
+-----------+----------+------+-----+---------+-------+
3 rows in set (0.01 sec)
The command SHOW COLUMNS FROM age information; gives the same information
as DESCRIBE age information; but DESCRIBE involves less typing.
(If you’re really trying to save keystrokes, you could abbreviate DESCRIBE
as DESC.)
To delete a database.
mysql> drop database [database name];
***************************************************************************
The INSERT Command
For the table to be useful, we need to add information to it. We do so with
the INSERT command:
mysql>INSERT INTO age_information (lastname, firstname, age) VALUES ('Wall', 'Larry','46');
Query OK, 1 row affected (0.00 sec)
mysql> INSERT INTO info (f_name,m_name,l_name,userid,username,email,phone) values ('sachin','M','Walunj','109','rwalunj','rwalunj@gmail.com','123456789');
SELECT * FROM info WHERE f_name = "sachin";
The syntax of the command is INSERT INTO, followed by the table in
which to insert, a list within parentheses of the fields into which information
is to be inserted, and the qualifier VALUES followed by the list of values
in parentheses in the same order as the respective fields.1
****************************************************************************
The SELECT Command
SELECT selects records from the database. When this command is executed
from the command line, MySQL prints all the records that match the query.
The simplest use of SELECT is shown in this example:
mysql> SELECT * FROM age_information;
+----------+-----------+------+
| lastname | firstname | age |
+----------+-----------+------+
| Wall | Larry | 46 |
+----------+-----------+------+
1 row in set (0.00 sec)
mysql> SELECT * FROM info;
+--------+--------+--------+--------+----------+-------------------+-----------+
| f_name | m_name | l_name | userid | username | email | phone |
+--------+--------+--------+--------+----------+-------------------+-----------+
| sachin | M | Walunj | 109 | rwalunj | rwalunj@gmail.com | 123456789 |
+--------+--------+--------+--------+----------+-------------------+-----------+
1 row in set (0.00 sec)
The * means “show values for all fields in the table”; FROM specifies the
table from which to extract the information.
The previous output shows that the record for Larry Wall was added
successfully. To experiment with the SELECT command, we need to add a
few more records, just to make things interesting:
INSERT INTO age_information (lastname, firstname, age) VALUES ('Torvalds', 'Linus','31');
Query OK, 1 row affected (0.00 sec)
INSERT INTO age_information (lastname, firstname, age) VALUES ('aymond', 'Eric','40');
Query OK, 1 row affected (0.00 sec)
mysql> SELECT * FROM age_information;
+----------+-----------+------+
| lastname | firstname | age |
+----------+-----------+------+
| Wall | Larry | 46 |
| Torvalds | Linus | 31 |
| aymond | Eric | 40 |
+----------+-----------+------+
3 rows in set (0.00 sec)
****************************************************************************
mysql> SELECT * FROM age_information;
+----------+-----------+------+
| lastname | firstname | age |
+----------+-----------+------+
| Wall | Larry | 46 |
| Torvalds | Linus | 31 |
| aymond | Eric | 40 |
+----------+-----------+------+
3 rows in set (0.00 sec)
mysql> UPDATE age_information SET age = 47 WHERE lastname = 'Wall';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
Be sure to use that WHERE clause; otherwise, if we had only entered UPDATE
age information SET age = 47, all the records in the database would have
been given the age of 47!
Although this might be good news for some people in these records
(how often have the old-timers said “Oh, to be 47 years old again”—OK,
probably not), it might be shocking news to others.
This method works, but it requires the database to know that Larry is
46, turning 47. Instead of keeping track of this, for Larry’s next birthday
we simply increment his age:
mysql> SELECT * FROM age_information;
mysql> UPDATE age_information SET age = age + 1 WHERE lastname = 'Wall';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> SELECT * FROM age_information;
+----------+-----------+------+
| lastname | firstname | age |
+----------+-----------+------+
| Wall | Larry | 48 |
| Torvalds | Linus | 31 |
| aymond | Eric | 40 |
+----------+-----------+------+
3 rows in set (0.00 sec)
update info set l_name = 'Patil' where f_name = 'sachin';
update info set userid = '100' where phone = '123456789';
mysql> SELECT * FROM info WHERE f_name != "sachin" order by userid;
For the table to be useful, we need to add information to it. We do so with
the INSERT command:
mysql>INSERT INTO age_information (lastname, firstname, age) VALUES ('Wall', 'Larry','46');
Query OK, 1 row affected (0.00 sec)
mysql> INSERT INTO info (f_name,m_name,l_name,userid,username,email,phone) values ('sachin','M','Walunj','109','rwalunj','rwalunj@gmail.com','123456789');
SELECT * FROM info WHERE f_name = "sachin";
The syntax of the command is INSERT INTO, followed by the table in
which to insert, a list within parentheses of the fields into which information
is to be inserted, and the qualifier VALUES followed by the list of values
in parentheses in the same order as the respective fields.1
****************************************************************************
The SELECT Command
SELECT selects records from the database. When this command is executed
from the command line, MySQL prints all the records that match the query.
The simplest use of SELECT is shown in this example:
mysql> SELECT * FROM age_information;
+----------+-----------+------+
| lastname | firstname | age |
+----------+-----------+------+
| Wall | Larry | 46 |
+----------+-----------+------+
1 row in set (0.00 sec)
mysql> SELECT * FROM info;
+--------+--------+--------+--------+----------+-------------------+-----------+
| f_name | m_name | l_name | userid | username | email | phone |
+--------+--------+--------+--------+----------+-------------------+-----------+
| sachin | M | Walunj | 109 | rwalunj | rwalunj@gmail.com | 123456789 |
+--------+--------+--------+--------+----------+-------------------+-----------+
1 row in set (0.00 sec)
The * means “show values for all fields in the table”; FROM specifies the
table from which to extract the information.
The previous output shows that the record for Larry Wall was added
successfully. To experiment with the SELECT command, we need to add a
few more records, just to make things interesting:
INSERT INTO age_information (lastname, firstname, age) VALUES ('Torvalds', 'Linus','31');
Query OK, 1 row affected (0.00 sec)
INSERT INTO age_information (lastname, firstname, age) VALUES ('aymond', 'Eric','40');
Query OK, 1 row affected (0.00 sec)
mysql> SELECT * FROM age_information;
+----------+-----------+------+
| lastname | firstname | age |
+----------+-----------+------+
| Wall | Larry | 46 |
| Torvalds | Linus | 31 |
| aymond | Eric | 40 |
+----------+-----------+------+
3 rows in set (0.00 sec)
1We did extensive research to determine that none of the names used in this chapter belong to
real people
There are many ways to use the SELECT command—it’s very flexible.
First, sort the table based on lastname:
mysql> SELECT * FROM age_information ORDER BY lastname;
+----------+-----------+------+
| lastname | firstname | age |
+----------+-----------+------+
| aymond | Eric | 40 |
| Torvalds | Linus | 31 |
| Wall | Larry | 46 |
+----------+-----------+------+
3 rows in set (0.00 sec)
Now show only the lastname field, sorted by lastname:
mysql> SELECT lastname FROM age_information ORDER BY lastname;
+----------+
| lastname |
+----------+
| aymond |
| Torvalds |
| Wall |
+----------+
3 rows in set (0.00 sec)
Show the ages in descending order:
mysql> SELECT age FROM age_information ORDER BY age DESC;
+------+
| age |
+------+
| 46 |
| 40 |
| 31 |
+------+
3 rows in set (0.00 sec);
Show all the last names for those who are older than 35:
mysql> SELECT lastname FROM age_information WHERE age > 35;
+----------+
| lastname |
+----------+
| Wall |
| aymond |
+----------+
2 rows in set (0.00 sec)
Do the same, but sort by lastname:
mysql> SELECT lastname FROM age_information WHERE age > 35 ORDER BY lastname;
+----------+
| lastname |
+----------+
| aymond |
| Wall |
+----------+
2 rows in set (0.00 sec)
The UPDATE Command
Since the database is about people, information in it can change (people
are unpredictable like that). For instance, although a person’s birthday is
static, their age changes. To change the value in an existing record,we can
UPDATE the table. Let’s say the fictional Larry Wall has turned 47:
mysql> SELECT * FROM age_information;
+----------+-----------+------+
| lastname | firstname | age |
+----------+-----------+------+
| Wall | Larry | 46 |
| Torvalds | Linus | 31 |
| aymond | Eric | 40 |
+----------+-----------+------+
3 rows in set (0.00 sec)
mysql> UPDATE age_information SET age = 47 WHERE lastname = 'Wall';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> SELECT * FROM age_information;
+----------+-----------+------+
| lastname | firstname | age |
+----------+-----------+------+
| Wall | Larry | 47 |
| Torvalds | Linus | 31 |
| aymond | Eric | 40 |
+----------+-----------+------+
3 rows in set (0.00 sec)
age information SET age = 47, all the records in the database would have
been given the age of 47!
Although this might be good news for some people in these records
(how often have the old-timers said “Oh, to be 47 years old again”—OK,
probably not), it might be shocking news to others.
This method works, but it requires the database to know that Larry is
46, turning 47. Instead of keeping track of this, for Larry’s next birthday
we simply increment his age:
mysql> SELECT * FROM age_information;
+----------+-----------+------+
| lastname | firstname | age |
+----------+-----------+------+
| Wall | Larry | 47 |
| Torvalds | Linus | 31 |
| aymond | Eric | 40 |
+----------+-----------+------+
3 rows in set (0.00 sec)
mysql> UPDATE age_information SET age = age + 1 WHERE lastname = 'Wall';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> SELECT * FROM age_information;
+----------+-----------+------+
| lastname | firstname | age |
+----------+-----------+------+
| Wall | Larry | 48 |
| Torvalds | Linus | 31 |
| aymond | Eric | 40 |
+----------+-----------+------+
3 rows in set (0.00 sec)
update info set l_name = 'Patil' where f_name = 'sachin';
update info set userid = '100' where phone = '123456789';
mysql> SELECT * FROM info WHERE f_name != "sachin" order by userid;
****************************************************************************
Sometimes we need to delete a record from the table (don’t assume the
worst—perhaps the person just asked to be removed from a mailing list,
which was opt-in in the first place, of course). This is done with the DELETE
command:
Delete Multiple Queries,
mysql>select concat('CALL mysql.rds_kill(',id,');') from information_schema.processlist where user='user name';
mysql> drop table [table name];
mysql> DELETE FROM age_information WHERE lastname = 'aymond';
Query OK, 1 row affected (0.00 sec)
mysql> SELECT * FROM age_information;
+----------+-----------+------+
| lastname | firstname | age |
+----------+-----------+------+
| Wall | Larry | 48 |
| Torvalds | Linus | 31 |
+----------+-----------+------+
2 rows in set (0.00 sec)
Query OK, 1 row affected (0.00 sec)
mysql> SELECT * FROM age_information;
+----------+-----------+------+
| lastname | firstname | age |
+----------+-----------+------+
| Wall | Larry | 48 |
| Torvalds | Linus | 31 |
+----------+-----------+------+
2 rows in set (0.00 sec)
Eric is in good company here, so put him back:
****************************************************************************
mysql> INSERT INTO age_information (lastname, firstname, age) VALUES ('Raymond', 'Eric', 40);
Query OK, 1 row affected (0.00 sec)
mysql> SELECT * FROM age_information;
+----------+-----------+------+
| lastname | firstname | age |
+----------+-----------+------+
| Wall | Larry | 48 |
| Torvalds | Linus | 31 |
| Raymond | Eric | 40 |
+----------+-----------+------+
3 rows in set (0.00 sec)
****************************************************************************
To show full process list
mysql> show processlist;
****************************************************************************
mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.122.1
Master_User: replication
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000003
Read_Master_Log_Pos: 286
Relay_Log_File: mysqld-relay-bin.000008
Relay_Log_Pos: 431
Relay_Master_Log_File: mysql-bin.000003
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 286
Relay_Log_Space: 732
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
1 row in set (0.00 sec)
****************************************************************************
mysql> FLUSH PRIVILEGES;
***************************************************************************
mysql> SELECT * FROM [table name];
mysql> SELECT * FROM information_schema.user_privileges;
***************************************************************************
ALL – Allow complete access to a specific database. If a database is not specified, then allow complete access to the entirety of MySQL.
CREATE – Allow a user to create databases and tables.
DELETE – Allow a user to delete rows from a table.
DROP – Allow a user to drop databases and tables.
EXECUTE – Allow a user to execute stored routines.
GRANT OPTION – Allow a user to grant or remove another user’s privileges.
INSERT – Allow a user to insert rows from a table.
SELECT – Allow a user to select data from a database.
SHOW DATABASES- Allow a user to view a list of all databases.
UPDATE – Allow a user to update rows in a table.
****************************************************************************
Create password for root user
mysqladmin -u root password “newpassword”
mysqladmin -u root -h host_name password “newpassword”
****************************************************************************
Create user for client
CREATE USER 'dev'@'192.168.x.x' IDENTIFIED BY 'dev123';
***************************************************************************
check io status on mysql
#mysql -u root -p xxxxx
#show slave status \G;
#check Slave_IO_Running: Yes or not
#mysql> show full processlist \G; ------ check full process list
****************************************************************************
LOADING AND DUMPING A DATABASE
We can load a database or otherwise execute SQL commands from a file.We
simply put the commands or database into a file—let’s call it mystuff.sql—
and load it in with this command:
$ mysql people < mystuff.sql
We can also dump out a database into a file with this command:
$ mysqldump people > entiredb.sql
For fun, try the mysqldump command with the people database (a gentle
reminder: the password is LampIsCool):
$ mysqldump -uapache -p people
Enter password:
$ mysqldump -uapache -p people > apache.sql
Enter password:
Notice that this outputs all the SQL needed to create the table and
insert all the current records. For more information, see man mysqldump.
MySQL is a powerful, sophisticated, and easy-to-use SQL database program.
Using Perl and DBI, one can easily create programs to automate
database management tasks. With this knowledge, the prospective web designer
should be able to construct a database-based (for lack of a better
term) web site that is portable, sophisticated, easy to manage, and professional
appearing. We have examined only a small subset of all that MySQL
provides (our 80/20 rule in effect).
We can load a database or otherwise execute SQL commands from a file.We
simply put the commands or database into a file—let’s call it mystuff.sql—
and load it in with this command:
$ mysql people < mystuff.sql
We can also dump out a database into a file with this command:
$ mysqldump people > entiredb.sql
For fun, try the mysqldump command with the people database (a gentle
reminder: the password is LampIsCool):
$ mysqldump -uapache -p people
Enter password:
$ mysqldump -uapache -p people > apache.sql
Enter password:
Notice that this outputs all the SQL needed to create the table and
insert all the current records. For more information, see man mysqldump.
****************************************************************************
SUMMARYMySQL is a powerful, sophisticated, and easy-to-use SQL database program.
Using Perl and DBI, one can easily create programs to automate
database management tasks. With this knowledge, the prospective web designer
should be able to construct a database-based (for lack of a better
term) web site that is portable, sophisticated, easy to manage, and professional
appearing. We have examined only a small subset of all that MySQL
provides (our 80/20 rule in effect).
No comments:
Post a Comment