Ero sivun ”Mysql/en” versioiden välillä
(Ak: Uusi sivu: When synchronizing both ways, you must define the role for both servers, one Slave and one Master.) |
|||
Rivi 131: | Rivi 131: | ||
When synchronizing both ways, you must define the role for both servers, one Slave and one Master. | When synchronizing both ways, you must define the role for both servers, one Slave and one Master. | ||
− | == MYSQL | + | == MYSQL and PHP5 == |
Voit hakea ja lähettää MySQL tietokantaan tietoa PHP:n avulla. Tämä onnistuu yksinkertaisesti tälläisellä funktiolla: | Voit hakea ja lähettää MySQL tietokantaan tietoa PHP:n avulla. Tämä onnistuu yksinkertaisesti tälläisellä funktiolla: |
Versio 23. marraskuuta 2015 kello 20.56
Mysql is a handy database which is easy to manage using the phpmyadmin application. Using phpmyadmin requires HTTP server software as well for instance Apache2 or Nginx and Php5.
Sisällysluettelo
Installation
aptitude install mysql-server
Give the mysql root user login password in installation. Do not forget the password. You can create more user accounts and change passwords later.
You can also use a mysql database without phpmyadmin but it helps a lot with managing it.
aptitude install mysql-client
MySQL configuration
Configuration file can be found in Ubuntu and Debian under:
/etc/mysql/my.cnf
The port the MySQL server listens to. By default it is set to 3306.
[client] port 3306
Address the server will listen to. Put a # number sign in front of the line and it will listen to all IP addresses.
bind-address = 127.0.0.1
Command Line
Mysql databases can also be managed by command line.
You can connect to a mysql server with command line by:
mysql -u root -p
Give the mysql root user password.
User Accounts
List user accounts:
select host, user, password from mysql.user;
You can create a new user account with the command:
CREATE USER 'newuser'@'localhost' IDENTIFIED BY 'password';
Give privileges for the new user account. The following command gives pretty much all privileges except the ones for modifying user accounts.
GRANT ALL PRIVILEGES ON * . * TO 'newuser'@'localhost';
This command gives the user all privileges including databases and users accounts.
GRANT ALL PRIVILEGES ON * . * TO 'newuser'@'%' WITH GRANT OPTION;
Confirm privileges:
FLUSH PRIVILEGES;
Add certain privileges to a user account:
GRANT [type of permission] ON [database name].[table name] TO ‘[username]’@'localhost’;
Remove certain privileges from a user account:
REVOKE [type of permission] ON [database name].[table name] FROM ‘[username]’@‘localhost’;
Remove user account:
DROP USER ‘demo’@‘localhost’;
Master and Slave
Asynchronous synchronization of a MySQL server from Master server to Slave servers.
Master
This goes in the Master server's configuration:
nano /etc/mysql/my.cnf
Add these to the end of mysqld
[mysqld] ... log-bin=mysql-bin server-id = 1 auto_increment_increment = 10 auto_increment_offset = 1
Form connection to MySQL server:
mysql -u root -p
We create the user account replicant, with login rights from wherever % and password is just "password"
create user replicant@'%' identified by 'password';
Define privileges for the user:
GRANT SELECT, PROCESS, FILE, SUPER, REPLICATION CLIENT, REPLICATION SLAVE, RELOAD ON *.* TO replicant@'%'; Flush Privileges;
Slave
This goes in the Slave server's configuration:
Establish a connection to the database server and give the Master server's IP-address, username and password.
HANGE MASTER TO MASTER_HOST='10.10.10.100', MASTER_USER='replicant', MASTER_PASSWORD='password';
Start the Slave server
Start slave;
Make sure that everything works:
show slave status\G
When synchronizing both ways, you must define the role for both servers, one Slave and one Master.
MYSQL and PHP5
Voit hakea ja lähettää MySQL tietokantaan tietoa PHP:n avulla. Tämä onnistuu yksinkertaisesti tälläisellä funktiolla:
<?php mysqli_connect(host,username,password,dbname); mysqli_close($con); ?>
Muokkaa tätä esimerkiksi:
<?php //Muodostetaan yheyttä tietokantaan $con = mysqli_connect(127.0.0.1,root,Qwerty123,database); //Suljetaan yhteys tietokantaan mysqli_close($con); ?>
Eli palvelimen osoite, käyttäjätunnus, salasana ja tietokanta joka valitaan.
MySQL tietokannasta tulostaminen
<?php $con=mysqli_connect("127.0.0.1","root","Qwerty123","database"); if (mysqli_connect_errno()) { echo "Yhdistäminen epäonnistui " . mysqli_connect_error(); } $result = mysqli_query($con,"SELECT * FROM data"); while($row = mysqli_fetch_array($result)) { echo $row['mysql']; } mysqli_close($con); ?>
Huomaa, että tietokantaan täytyy kirjoittaa jotakin että voit tulostaa tietoa sieltä.
Talllenna edelliset konfiguroinnit mysql.php ja laita sitten esim. index.php tiedostoon:
<?php include_once ('mysql.php'); ?>
Näin sinun täytyy laittaa kaikkiin haluamaasi php tiedostoon, jotka käyttää tietokantaa.