All posts by mesala

Creating users to database

Now that you’ve seen how easy it is to use MySQL and created your first database, it’s time to look at how you create users, as you probably won’t want to grant your PHP scripts root access to MySQL—it could cause a real headache should you get hacked :).

To create a user, issue the GRANT command, which takes the following form (don’t type this in; it’s not an actual working command):
GRANT PRIVILEGES ON database.object TO ‘username’@’hostname’
IDENTIFIED BY ‘password’;

This should all look pretty straightforward, with the possible exception of the database.object part, which refers to the database itself and the objects it contains, such as tables.

  • *.* All databases and all their objects
  • database.* Only the database called database and all its objects
  • database.object Only the database called database and its object called object

So, let’s create a new user who can access just the new tietokantadb database and all its objects, by entering the following (replacing the username asentaja and also the password passwordi with ones of your choosing):
GRANT ALL ON tietokantadb.* TO ‘asentaja’@’localhost’
IDENTIFIED BY ‘passwordi’;
What this does is allow the user asentaja@localhost full access to the tietokantadb database using the password passwordi. You can test whether this step has worked by entering
quit to exit and then rerunning MySQL the way you did before, but instead of entering ->
-u root -p, type -u asentaja -p, or whatever username you created.

Creating a database


a new database called tietokantadb:
CREATE DATABASE tietokantadb;
A successful command will return a message that doesn’t mean much yet—Query OK,
1 row affected (0.00 sec)—but will make sense soon. Now that you’ve created the database, you want to work with it, so issue the following command:
USE tietokantadb;

MySQL Commands

Yleiset komennot:
ALTER Alter a database or table
BACKUP Back up a table
\c Cancel input
CREATE Create a database
DELETE Delete a row from a table
DESCRIBE Describe a tables columns
DROP Delete a database or table
EXIT (Ctrl-C) Exit
GRANT Change user privileges
HELP (\h, \?) Display help
INSERT Insert data
LOCK Lock table(s)
QUIT (\q) Same as EXIT

RENAME Rename a table
SHOW List details about an object
SOURCE Execute a file
STATUS (\s) Display the current status
TRUNCATE Empty a table
UNLOCK Unlock table(s)
UPDATE Update an existing record
USE Use a database

MySQL tietokannan poistaminen

Tietokanta voidaan poistaa jos on esimerkiksi tehnyt väärän nimisen tietokannan. Tietokannan poistaminen tapahtuu Mysql:ssä ja sinne kirjaudutaan ensiksi mysql -u root -p.
Kirjautumisen jälkeen voi katsella mitä tietokantoja on show databases; komennolla.

Tietokannan poistaminen tapahtuu komennolla DROP DATABASE tietokannan nimi. Jos esimerkiksi on tietokanta blogi ja se halutaan poistaa se onnistuu komennolla DROP DATABASE blogi;

Käyttäjätunnuksen liittäminen tietokantaan

Jotta tietokantaa voidaa käyttää käyttäjätunnuksella ja salasanalla, ne tulee liittää oikeuksilla tietokantaan. Se tehdään seuraavasti ->
GRANT ALL PRIVILEGES ON tietokannan-nimi.* to ‘käyttäjätunnus’ identified by ‘salasana’; Esimerkiksi tehdään wpdata tietokantaan liittäminen dbmekaanikolle. -> GRANT ALL PRIVILEGES ON wpdata.*to ‘dbmekaanikko’ identified by ‘passwordi’;

MySql dump

Tietokannan backup on tärkee ottaa varsinkin kun päivittää wordpress moottoria uuteen versioon tai phpbb3 foorumia päivittää uuteen versioon.

Yksinkertainen tietokannan varmuuskopio voidaan tehdä mysqldump komennolla. Jos esimerkiksi on wpdb tietokanta olemassa ja siitä halutaan varmuuskopio, voidaan käyttää seuraavaa komentoa:
mysqldump -u root -p wpdb > dump-wpdb.sql
dump-wpdb.sql muodostuu nyt varmuuskopioksi tietokannasta