Installing MySQL on Ubuntu 14.04
Apr 21, 2015
warning
This post is more than 5 years old. While math doesn't age, code and operating systems do. Please use the code/ideas with caution and expect some issues due to the age of the content. I am keeping these posts up for archival purposes because I still find them useful for reference, even when they are out of date!
In this post I will cover installing MySQL on Ubuntu 14.04, using the repository available at the MySQL apt repo . The idea is to setup a repository that allows our MySQL to be current-- in this case MySQL 5.6. The instructions I'm documenting here are derived from the above link, as well as the nice book Jump Start MySQL (April 2015). If this is of interest to you, follow along and be sure checkout the resources mentioned above.
1. First we download a deb file that will setup the repository information from MySQL apt repo -- look for the Ubuntu 14.04 download button. As noted in Jump Start MySQL , it looks like you have to login or sign up, but you don't. Just click No thanks, just start my download near the bottom of the page and choose a convenient place to save the file.
Next, start the terminal and go to the save location. To install, simply type (your file name might be different, so adjust accordingly):
$ sudo dpkg -i mysql-apt-config_0.3.3-1ubuntu14.04_all.deb
This will bring up a menu that asks which MySQL product we'd like to configure:
-
We leave the selection as Server, the default, and hit
Enter
. -
Next, we select mysql-5.6, again the default, and hit
Enter
. This takes us back to main menu. -
Finally, select Apply and hit
Enter
-- this should exit to the terminal with a simple OK.
2. Now that the repository is setup we can use the usual install commands:
$ sudo apt-get update
$ sudo apt-get install mysql-server
In our Ubuntu install of MySQL we will be prompted to choose a root passsword. Note, just to be clear, that this root account and password apply only to the MySQL server and not to the Ubuntu OS. As always, choose a strong password and record it using a suitable tool.
3. We can check the status of the MySQL server using
$ service mysql status
This should tell us the server is running. To start and stop the server, use
$ sudo service mysql start
and
$ sudo service mysql stop
That's it, MySQL is up and running.
Beginnings
To connect with the server, try the following, using the root password setup above:
$ mysql -u root -p
Enter password:
The response should be something like:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.6.24 MySQL Community Server (GPL)
Copyright (c) 2000, 2015, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
The mysql>
prompt is where we type all of
our commands. It is conventional to use CAPS for SQL keywords like so:
mysql> SHOW DATABASES;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
+--------------------+
3 rows in set (0.00 sec)
These are the existing DBs after the initial install. To exit, simply type:
mysql> exit
Bye
or, hit cntl-d
.
User Accounts
As suggested in Jump Start MySQL it is a good idea to create user accounts for daily use instead of using the root account. This can be done with a command like
mysql> CREATE USER 'username'@'localhost' IDENTIFIED BY 'password';
where username and password are substituted with the desired values. The hostname, in this case localhost, can also be changed if connections off of the local machine are needed. However, that's beyond the scope of this post. Finally, the new user can connect to the MySQL server using:
$ mysql -u username -p
where the appropriate username and password are used. Of course, privileges can be granted and revoked in quite specific ways to databases, tables, etc. Again following Jump Start MySQL, a quick example of this might go:
-
Create
testdb
database using root account:mysql> CREATE DATABASE testdb;
-
Enable privileges:
mysql> GRANT CREATE, DROP, ALTER, INSERT, UPDATE, SELECT,
-> INDEX ON testdb.* TO 'username'@'localhost'; -
Flush privileges to make them active without restarting MySQL server:
With these commands, the user username should be able to access the newmysql> FLUSH PRIVILEGES;
testdb
. To revoke some of the privileges, the command would be something like the following:
Of course, this would be followed by amysql> REVOKE CREATE, DROP, ALTER, INDEX
-> INDEX ON testdb.* TO 'username'@'localhost';FLUSH PRIVILIGES;
to make the changes active.
Final Thoughts
That's all the basics. I'll be posting more on SQL, covering various topics in the coming weeks and months, so check back. As always, corrections, comments and questions are welcome.