Sie sind auf Seite 1von 8

06/06/2017 A Basic MySQL Tutorial | DigitalOcean

Community Menu

By: Etel Sverdlov Subscribe

A Basic MySQL Tutorial


Posted June 12, 2012 1m MYSQL UBUNTU CENTOS 167

About MySQL
MySQL is an open source database management software that helps users store, organize, and retrieve data. It is a very powerful program
with a lot of flexibilitythis tutorial will provide the simplest introduction to MySQL

How to Install MySQL on Ubuntu and CentOS


If you don't have MySQL installed on your droplet, you can quickly download it.

Ubuntu:

sudo apt-get install mysql-server

Centos:

sudo yum install mysql-server


/etc/init.d/mysqld start

How to Access the MySQL shell


Once you have MySQL installed on your droplet, you can access the MySQL shell by typing the following command into terminal:

mysql -u root -p

After entering the root MySQL password into the prompt (not to be confused with the root droplet password), you will be able to start building
your MySQL database.

Sign up for our newsletter. Get the latest tutorials on SysAdmin and open source topics.
Two points to keep in mind: S C R O L L TO TO P

Enter your email address Sign Up

https://www.digitalocean.com/community/tutorials/a-basic-mysql-tutorial 1/8
06/06/2017 A Basic MySQL Tutorial | DigitalOcean
All MySQL commands end with a semicolon; if the phrase does not end with a semicolon, the command will not execute.
Also, although it is not required, MySQL commands are usually written in uppercase and databases, tables, usernames, or text are in
lowercase to make them easier to distinguish. However, the MySQL command line is not case sensitive.

How to Create and Delete a MySQL Database


MySQL organizes its information into databases; each one can hold tables with specific data.

You can quickly check what databases are available by typing:

SHOW DATABASES;

Your screen should look something like this:

mysql> SHOW DATABASES;


+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
+--------------------+
4 rows in set (0.01 sec)

Creating a database is very easy:

CREATE DATABASE database name;

In this case, for example, we will call our database "events."

mysql> SHOW DATABASES;


+--------------------+
| Database |
+--------------------+
| information_schema |
| events |
| mysql |
| performance_schema |
| test |
+--------------------+
5 rows in set (0.00 sec)

In MySQL, the phrase most often used to delete objects is Drop. You would delete a MySQL database with this command:

DROP DATABASE database name;

How to Access a MySQL Database


Once we have a new database, we can begin to fill it with information.

The first step is to create a new table within the larger database.

Lets open up the database we want to use:

USE events;

In the same way that you could check the available databases, you can also see an overview of the tables that the database contains.

SHOW tables;
Sign up for our newsletter. Get the latest tutorials on SysAdmin and open source topics.
S C R O L L TO TO P

Enter
Sinceyour
thisemail
is a new
address
database, MySQL has nothing to show, and you
Sign Upwill get a message that says, Empty set

https://www.digitalocean.com/community/tutorials/a-basic-mysql-tutorial 2/8
06/06/2017 A Basic MySQL Tutorial | DigitalOcean

How to Create a MySQL Table


Lets imagine that we are planning a get together of friends. We can use MySQL to track the details of the event.

Lets create a new MySQL table:

CREATE TABLE potluck (id INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(20),
food VARCHAR(30),
confirmed CHAR(1),
signup_date DATE);

This command accomplishes a number of things:

1. It has created a table called potluck within the directory, events.


2. We have set up 5 columns in the tableid, name, food, confirmed, and signup date.
3. The id column has a command (INT NOT NULL PRIMARY KEY AUTO_INCREMENT) that automatically numbers each row.
4. The name column has been limited by the VARCHAR command to be under 20 characters long.
5. The food column designates the food each person will bring. The VARCHAR limits text to be under 30 characters.
6. The confirmed column records whether the person has RSVPd with one letter, Y or N.
7. The date column will show when they signed up for the event. MySQL requires that dates be written as yyyy-mm-dd

Lets take a look at how the table appears within the database using the "SHOW TABLES;" command:

mysql> SHOW TABLES;


+------------------+
| Tables_in_events |
+------------------+
| potluck |
+------------------+
1 row in set (0.01 sec)

We can remind ourselves about the tables organization with this command:

DESCRIBE potluck;

Keep in mind throughout that, although the MySQL command line does not pay attention to cases, the table and database names are case
sensitive: potluck is not the same as POTLUCK or Potluck.

mysql>DESCRIBE potluck;
+-------------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+-------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(20) | YES | | NULL | |
| food | varchar(30) | YES | | NULL | |
| confirmed | char(1) | YES | | NULL | |
| signup_date | date | YES | | NULL | |
+-------------+-------------+------+-----+---------+----------------+
5 rows in set (0.01 sec)

How to Add Information to a MySQL Table


We have a working table for our party. Now its time to start filling in the details.

Use this format to insert information into each row:

INSERT INTO `potluck` (`id`,`name`,`food`,`confirmed`,`signup_date`) VALUES (NULL, "John", "Casserole","Y", '2012-04-11');

Once you input that in, you will see the words:

Sign up for our newsletter. Get the latest tutorials on SysAdmin and open source topics.
S C R O L L TO TO P

EnterQuery OK, 1
your email row affected (0.00 sec)
address Sign Up

https://www.digitalocean.com/community/tutorials/a-basic-mysql-tutorial 3/8
06/06/2017 A Basic MySQL Tutorial | DigitalOcean
Lets add a couple more people to our group:

INSERT INTO `potluck` (`id`,`name`,`food`,`confirmed`,`signup_date`) VALUES (NULL, "Sandy", "Key Lime Tarts","N", '2012-04-14');
INSERT INTO `potluck` (`id`,`name`,`food`,`confirmed`,`signup_date`) VALUES (NULL, "Tom", "BBQ","Y", '2012-04-18');
INSERT INTO `potluck` (`id`,`name`,`food`,`confirmed`,`signup_date`) VALUES (NULL, "Tina", "Salad","Y", '2012-04-10');

We can take a look at our table:

mysql> SELECT * FROM potluck;


+----+-------+----------------+-----------+-------------+
| id | name | food | confirmed | signup_date |
+----+-------+----------------+-----------+-------------+
| 1 | John | Casserole | Y | 2012-04-11 |
| 2 | Sandy | Key Lime Tarts | N | 2012-04-14 |
| 3 | Tom | BBQ | Y | 2012-04-18 |
| 4 | Tina | Salad | Y | 2012-04-10 |
+----+-------+----------------+-----------+-------------+
4 rows in set (0.00 sec)

How to Update Information in the Table


Now that we have started our potluck list, we can address any possible changes. For example: Sandy has confirmed that she is attending, so
we are going to update that in the table.

UPDATE `potluck`
SET
`confirmed` = 'Y'
WHERE `potluck`.`name` ='Sandy';

You can also use this command to add information into specific cells, even if they are empty.

How to Add and Delete a Column


We are creating a handy chart, but it is missing some important information: our attendees emails.

We can easily add this:

ALTER TABLE potluck ADD email VARCHAR(40);

This command puts the new column called "email" at the end of the table by default, and the VARCHAR command limits it to 40 characters.

However, if you need to place that column in a specific spot in the table, we can add one more phrase to the command.

ALTER TABLE potluck ADD email VARCHAR(40) AFTER name;

Now the new email column goes after the column name.

Just as you can add a column, you can delete one as well:

ALTER TABLE potluck DROP email;

I guess we will never know how to reach the picnickers.

How to Delete a Row


If needed, you can also delete rows from the table with the following command:

DELETE from [table name] where [column name]=[field text];


Sign up for our newsletter. Get the latest tutorials on SysAdmin and open source topics.
S C R O L L TO TO P

For example,
Enter if Sandy
your email suddenly realized that she will not be Sign
address able Up
to participate in the potluck after all, we could quickly eliminate her details.

https://www.digitalocean.com/community/tutorials/a-basic-mysql-tutorial 4/8
06/06/2017 A Basic MySQL Tutorial | DigitalOcean

mysql> DELETE from potluck where name='Sandy';


Query OK, 1 row affected (0.00 sec)

mysql> SELECT * FROM potluck;


+----+------+-----------+-----------+-------------+
| id | name | food | confirmed | signup_date |
+----+------+-----------+-----------+-------------+
| 1 | John | Casserole | Y | 2012-04-11 |
| 3 | Tom | BBQ | Y | 2012-04-18 |
| 4 | Tina | Salad | Y | 2012-04-10 |
+----+------+-----------+-----------+-------------+
3 rows in set (0.00 sec)

Notice that the id numbers associated with each person remain the same.

By Etel Sverdlov

By: Etel Sverdlov Upvote (167) Subscribe

DigitalOcean Monitoring
Collect metrics, monitor Droplet
performance, and receive alerts when
problems arise in your infrastructure
no configuration required. Coming 2017.

READ MORE

Related Tutorials
Understanding SQL And NoSQL Databases And Different Database Models
How To Migrate a MySQL Database Between Two Servers
How To Set Up Master Slave Replication in MySQL
How To Import and Export Databases and Reset a Root Password in MySQL
How To Create a New User and Grant Permissions in MySQL

46 Comments

Leave a comment...

Log In to Comment

Signerikchacon
up for our newsletter.
February 18, 2013 Get the latest tutorials on SysAdmin and open source topics.
S C R O L L TO TO P

Enter
0 your email address Sign Up

https://www.digitalocean.com/community/tutorials/a-basic-mysql-tutorial 5/8
06/06/2017 A Basic MySQL Tutorial | DigitalOcean
Very good quick tutorial to mysql !!

Thanks

ruz.irani February 27, 2013

0 I kept getting errors when trying to create my first table. It finally worked when I excluded these quotation marks `potluck`
(`id`,`name`,`food`,`confirmed`,`signup_date`). Only the VALUES need quotation marks. Not sure if that is just my version of mySQL. I followed the
tutorials on here to install it

ionstream May 10, 2013

1 If you're trying to allow remote connections to the MySQL server, then open /etc/mysql/my.conf and comment out the line "bind-address
127.0.0.1". This will cause MySQL to bind to any address, allowing for remote connections. Then you can use iptables in combination with
CREATE USER username@'some-ip-address-or-%-wildcard' to control access to the server.

kamaln7 MOD May 12, 2013

1 @ruz.irani Table/column names should be escaped with backticks (`), while values and strings should be escape with single-quotation marks (').

@ionstream Awesome, thank you! :]

svarapu July 24, 2013

0 It is really useful. Some more if you explain about mysql would be more helpful. Thanks a lot
S.Sivarama Sarma

212nath July 30, 2013

0 How do I import a database from phpmyadmin from another server, If I have the sql file, how do I upload that?

kamaln7 MOD July 31, 2013

0 @212nath: See this article: https://www.digitalocean.com/community/articles/how-to-migrate-mysql-between-two-servers

iamkingsleyf January 5, 2015

0 404 error

nguyentien.jobs August 25, 2013

0 When I try accessing SQL shell, I got this error:


root@tien:~# mysql -u root -p
Enter password:
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: Y
ES)

kamaln7 MOD August 25, 2013

0 @nguyentien.jobs: The password you entered is incorrect -- are you sure it's the correct password?

robinrishi September 7, 2013

0 Do, What is the default password set for Mysql server when we create the droplet with one click application, LAMP on UBUNTU

kamaln7 MOD September 8, 2013

0 @robinrishi: The default password is 'password'

Sign up for our newsletter. Get the latest tutorials on SysAdmin and open source topics.
S C R O L L TO TO P

bda.awk
Enter October
your email 8, 2013
address Sign Up
0
https://www.digitalocean.com/community/tutorials/a-basic-mysql-tutorial 6/8
06/06/2017 A Basic MySQL Tutorial | DigitalOcean
0 Hello
How do I create mysql accounts for each domain in my drop?

And how do I get remote access?

Thanks!

kamaln7 MOD October 9, 2013

0 @bda.awk: Take a look at https://www.digitalocean.com/community/articles/how-to-create-a-new-user-and-grant-permissions-in-mysql

How To Create a New User and Grant Permissions in MySQL


by Etel Sverdlov

MySQL is a powerful database management system used for organizing and retrieving data. This tutorial explains how to to create new MySQL
users and how to grant them the appropriate permissions.

tugrulasik October 25, 2013

0 Really good tutorial. Everything is so clear. Thx :)

richardcarl.mendoza December 7, 2013

0 Hi, I'm having a problem on mysqld. after I reboot my laptop, I got this issue.
"ERROR 2002 (HY000): can't connect to local mysql server through socket /var/lib/mysql/mysql.socket (1111)"

I already tried to search the issue on google but it didn't gave the answer. I hope someone could help. Thanks a lot in advance!

kamaln7 MOD December 18, 2013

0 @richardcarl.mendoza: That means MySQL isn't running -- did you configure it to start on boot?

yazirarafath January 28, 2014

0 Thank you!

dipakhack February 5, 2014

1 GOOD MORNING;
VERY GOOD MY FRIEND I LIKE IT.
THANKS FOR TUTORIAL.

dipakhack February 5, 2014

Sign up create
0 how for ourtable
newsletter.
and insert
Get values
the latestinto thaton
tutorials using c programming
SysAdmin in mysql
and open source
topics. on linux OS.
S C R O L L TO TO P
please give solution in as simple as possible.
Enter your email address Sign Up

https://www.digitalocean.com/community/tutorials/a-basic-mysql-tutorial 7/8
06/06/2017 A Basic MySQL Tutorial | DigitalOcean

kamaln7 MOD February 8, 2014

0 @dipakhack: Check out http://www.cyberciti.biz/tips/linux-unix-connect-mysql-c-api-program.html.

Load More Comments

This work is licensed under a Creative


Commons Attribution-NonCommercial-
ShareAlike 4.0 International License.


Copyright 2017 DigitalOcean Inc.

Community Tutorials Questions Projects Tags Newsletter RSS

Distros & One-Click Apps Terms, Privacy, & Copyright Security Report a Bug Get Paid to Write Shop

Sign up for our newsletter. Get the latest tutorials on SysAdmin and open source topics.
S C R O L L TO TO P

Enter your email address Sign Up

https://www.digitalocean.com/community/tutorials/a-basic-mysql-tutorial 8/8

Das könnte Ihnen auch gefallen