Archive for the ‘MySQL’ Category

How to Back Up and Restore a MySQL Database on command line

By Automater on July 15th, 2010

If you’re storing anything in MySQL databases that you do not want to lose, it is very important to make regular backups of your data to protect it from loss. This tutorial will show you two easy ways to backup and restore the data in your MySQL database. You can also use this process to move your data to a new web server.

* Back up From the Command Line (using mysqldump)
* Back up your MySQL Database with Compress
* Restoring your MySQL Database
* Backing Up and Restoring using PHPMyAdmin

Back up From the Command Line (using mysqldump)

If you have shell or telnet access to your web server, you can backup your MySQL data by using the mysqldump command. This command connects to the MySQL server and creates an SQL dump file. The dump file contains the SQL statements necessary to re-create the database. Here is the proper syntax:

1
$ mysqldump --opt -u [uname] -p[pass] [dbname] > [backupfile.sql]

* [uname] Your database username
* [pass] The password for your database (note there is no space between -p and the password)
* [dbname] The name of your database
* [backupfile.sql] The filename for your database backup
* [--opt] The mysqldump option

For example, to backup a database named ‘Tutorials’ with the username ‘root’ and with no password to a file tut_backup.sql, you should accomplish this command:

1
$ mysqldump -u root -p Tutorials > tut_backup.sql

This command will backup the ‘Tutorials’ database into a file called tut_backup.sql which will contain all the SQL statements needed to re-create the database.

With mysqldump command you can specify certain tables of your database you want to backup. For example, to back up only php_tutorials and asp_tutorials tables from the ‘Tutorials’ database accomplish the command below. Each table name has to be separated by space.

1
$ mysqldump -u root -p Tutorials php_tutorials asp_tutorials > tut_backup.sql

Sometimes it is necessary to back up more that one database at once. In this case you can use the –database option followed by the list of databases you would like to backup. Each database name has to be separated by space.

1
$ mysqldump -u root -p --databases Tutorials Articles Comments > content_backup.sql

If you want to back up all the databases in the server at one time you should use the –all-databases option. It tells MySQL to dump all the databases it has in storage.

1
$ mysqldump -u root -p --all-databases > alldb_backup.sql

The mysqldump command has also some other useful options:

–add-drop-table: Tells MySQL to add a DROP TABLE statement before each CREATE TABLE in the dump.

–no-data: Dumps only the database structure, not the contents.

–add-locks: Adds the LOCK TABLES and UNLOCK TABLES statements you can see in the dump file.

The mysqldump command has advantages and disadvantages. The advantages of using mysqldump are that it is simple to use and it takes care of table locking issues for you. The disadvantage is that the command locks tables. If the size of your tables is very big mysqldump can lock out users for a long period of time.

Back up your MySQL Database with Compress

If your mysql database is very big, you might want to compress the output of mysqldump. Just use the mysql backup command below and pipe the output to gzip, then you will get the output as gzip file.

1
$ mysqldump -u [uname] -p[pass] [dbname] | gzip -9 > [backupfile.sql.gz]

If you want to extract the .gz file, use the command below:

1
$ gunzip [backupfile.sql.gz]

Restoring your MySQL Database

Above we backup the Tutorials database into tut_backup.sql file. To re-create the Tutorials database you should follow two steps:

* Create an appropriately named database on the target machine
* Load the file using the mysql command:

1
$ mysql -u [uname] -p[pass] [db_to_restore] < [backupfile.sql]

Have a look how you can restore your tut_backup.sql file to the Tutorials database.

1
$ mysql -u root -p Tutorials < tut_backup.sql

To restore compressed backup files you can do the following:

1
gunzip < [backupfile.sql.gz] | mysql -u [uname] -p[pass] [dbname]

If you need to restore a database that already exists, you’ll need to use mysqlimport command. The syntax for mysqlimport is as follows:

1
mysqlimport -u [uname] -p[pass] [dbname] [backupfile.sql]

Backing Up and Restoring using PHPMyAdmin

It is assumed that you have phpMyAdmin installed since a lot of web service providers use it. To backup your MySQL database using PHPMyAdmin just follow a couple of steps:

* Open phpMyAdmin.
* Select your database by clicking the database name in the list on the left of the screen.
* Click the Export link. This should bring up a new screen that says View dump of database (or something similar).
* In the Export area, click the Select All link to choose all of the tables in your database.
* In the SQL options area, click the right options.
* Click on the Save as file option and the corresponding compression option and then click the ‘Go’ button. A dialog box should appear prompting you to save the file locally.

Restoring your database is easy as well as backing it up. Make the following:

* Open phpMyAdmin.
* Create an appropriately named database and select it by clicking the database name in the list on the left of the screen. If you would like to rewrite the backup over an existing database then click on the database name, select all the check boxes next to the table names and select Drop to delete all existing tables in the database.
* Click the SQL link. This should bring up a new screen where you can either type in SQL commands, or upload your SQL file.
* Use the browse button to find the database file.
* Click Go button. This will upload the backup, execute the SQL commands and re-create your database.

Posted in

Setup Virtual Hosts on Ubuntu/Debian using Apache2 + MySQL5.X + PHP5

By Automater on May 21st, 2010

This entire tutorial is done after entering the ’sudo -i’ command which allows us to act as the SUPER-USER for the entirety of our terminal session.

1
sudo -i

1) Install the required modules from the command line

1
apt-get install apache2 mysql-server mysql-client php5 php5-cli php5-mysql

2) Change the directory to /etc/apache2/sites-available

1
cd /etc/apache2/sites-available

3) If you run the ‘ls’ command while in the sites-available directory you should see the following

1
ls

Output

1
default  default-ssl

4) Copy the ‘default’ config to a site specific config. For this tutorial I am using dev.scottfaisal.com.

1
cp default dev.scottfaisal.com.conf

5) Make the application directory

1
mkdir /var/dev.scottfaisal.com

6) Open the file with an editor of your choosing. I prefer NANO.

1
nano dev.scottfaisal.com.conf

The output below is the entire file but I will discuss certain parts that we will need to edit.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
<VirtualHost *:80>
ServerAdmin webmaster@localhost

DocumentRoot /var/www
<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>
<Directory /var/www/>
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
allow from all
</Directory>

ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
<Directory "/usr/lib/cgi-bin">
AllowOverride None
Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
Order allow,deny
Allow from all
</Directory>

ErrorLog /var/log/apache2/error.log

# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
LogLevel warn

CustomLog /var/log/apache2/access.log combined

Alias /doc/ "/usr/share/doc/"
<Directory "/usr/share/doc/">
Options Indexes MultiViews FollowSymLinks
AllowOverride None
Order deny,allow
Deny from all
Allow from 127.0.0.0/255.0.0.0 ::1/128
</Directory>

</VirtualHost>

7) Add the name of the server. This will be the name that you type in the URL field of your web browser (IE: Firefox)

1
2
3
4
5
6
7
<VirtualHost *:80>
ServerAdmin webmaster@localhost
ServerName dev.scottfaisal.com
...

8 ) Point the Virtual host to the correct directory

1
2
3
4
5
6
7
8
9
10
11
12
13
...
DocumentRoot /var/dev.scottfaisal.com
<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>
...

9) Also make the change here.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
...
<Directory /var/dev.scottfaisal.com/>
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
allow from all
</Directory>
...

10) So this is what your dev.scottfaisal.com.conf file should look like when you are done. Write/Quite the file and we will move on.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
<VirtualHost *:80>
ServerAdmin webmaster@localhost
ServerName dev.scottfaisal.com

DocumentRoot /var/dev.scottfaisal.com
<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>
<Directory /var/dev.scottfaisal.com/>
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
allow from all
</Directory>

ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
<Directory "/usr/lib/cgi-bin">
AllowOverride None
Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
Order allow,deny
Allow from all
</Directory>

ErrorLog /var/log/apache2/error.log

# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
LogLevel warn

CustomLog /var/log/apache2/access.log combined

Alias /doc/ "/usr/share/doc/"
<Directory "/usr/share/doc/">
Options Indexes MultiViews FollowSymLinks
AllowOverride None
Order deny,allow
Deny from all
Allow from 127.0.0.0/255.0.0.0 ::1/128
</Directory>

</VirtualHost>

10a) IMPORTANT! Make a symbolic link in the sites-enabled directory

1
ln -s /etc/apache2/sites-available/dev.scottfaisal.com.conf /etc/apache2/sites-enabled/000-dev.scottfaisal.com.conf

11) We need to edit our /etc/hosts file

1
nano /etc/hosts

Output

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
127.0.0.1       localhost
127.0.1.1       servername

# The following lines are desirable for IPv6 capable hosts
::1     localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
ff02::3 ip6-allhosts

12) Right beneath the ‘localhost’ definition, add the following

1
127.0.0.1      dev.scottfaisal.com

13) The complete file looks like this.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
127.0.0.1       localhost
127.0.0.1       dev.scottfaisal.com
127.0.1.1       servername

# The following lines are desirable for IPv6 capable hosts
::1     localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
ff02::3 ip6-allhosts

14) Create an index.php file in /var/dev.scottfaisal.com for testing purposes.

1
nano /var/dev.scottfaisal.com/index.php

Contents of .php file.

1
2
3
4
5
6
7
8
9
<?php

echo 'This is a test!!!!';
phpinfo();

?>

15) Reboot the apache2 process.

1
/etc/init.d/apache2 restart

16) Now open up your web browser and enter dev.scottfaisal.com into the URL bar and it should work!

UPDATE: I forgot to mention that in this environment, I like to configure log files for each virtual host. This is our current dev.scottfaisal.com file.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
<VirtualHost *:80>
ServerAdmin webmaster@localhost
ServerName dev.scottfaisal.com

DocumentRoot /var/dev.scottfaisal.com
<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>
<Directory /var/dev.scottfaisal.com/>
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
allow from all
</Directory>

ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
<Directory "/usr/lib/cgi-bin">
AllowOverride None
Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
Order allow,deny
Allow from all
</Directory>

ErrorLog /var/log/apache2/error.log

# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
LogLevel warn

CustomLog /var/log/apache2/access.log combined

Alias /doc/ "/usr/share/doc/"
<Directory "/usr/share/doc/">
Options Indexes MultiViews FollowSymLinks
AllowOverride None
Order deny,allow
Deny from all
Allow from 127.0.0.0/255.0.0.0 ::1/128
</Directory>

</VirtualHost>

Just change the ErrorLog and CustomLog names to match the virtual host.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
...
ErrorLog /var/log/apache2/dev.scottfaisal.com_error.log

# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
LogLevel warn

CustomLog /var/log/apache2/dev.scottfaisal.com_access.log combined
...
</VirtualHost>

MySQL – Resetting a lost MySQL root password

By Automater on April 15th, 2010

The MySQL root password allows full access to the MySQL database and allows for all actions to be undertaken including creating new users, new databases, setting access rules and so on.

Losing one can be a difficult issue to encounter. Luckily, resetting the root password is easy as long as you have sudo access to the Server.

Not the Server root user

A common issue is confusing the Server root user with the MySQL root user.

The Server root user is the server’s main user. The MySQL root user has complete control over MySQL only. The two ‘root’ users are not connected in any way.

Stop MySQL

The first thing to do is stop MySQL. Assuming you are using Ubuntu Hardy the command is as follows:

sudo /etc/init.d/mysql stop

Safe mode

Next we need to start MySQL in safe mode – that is to say, we will start MySQL but skip the user privileges table. Again, note that you will need to have sudo access for these commands so you don’t need to worry about any user being able to reset the MySQL root password:

sudo mysqld_safe --skip-grant-tables &

Note: The ampersand (&) at the end of the command is required.

Login

All we need to do now is to log into MySQL and set the password.

mysql -u root

Note: No password is required at this stage as when we started MySQL we skipped the user privileges table.

Next, instruct MySQL which database to use:

use mysql;

Reset Password

Enter the new password for the root user as follows:

update user set password=PASSWORD("mynewpassword") where User='root';

and finally, flush the privileges:

flush privileges;

Restart

Now the password has been reset, we need to restart MySQL by logging out:
quit

and simply stopping and starting MySQL:

sudo /etc/init.d/mysql stop
...
sudo /etc/init.d/mysql start
Done.

Login

Test the new password by logging in:

mysql -u root -p

You will be prompted for your new password.

Posted in
Tags