Mysql Making a Differential or Incremental Backup

Mysql Making a Differential or Incremental Backup

https://dev.mysql.com/doc/mysql-enterprise-backup/8.0/en/mysqlbackup.incremental.html

https://snapshooter.com/learn/mysql/mysql-incremental-backup

https://www.cloudpanel.io/tutorial/how-to-set-up-mysql-incremental-backups/

 

Note: mysql 8

 

Đọc thêm..

Fix – MySQL ERROR 1819 (HY000): Your password does not satisfy the current policy requirements

Fix – MySQL ERROR 1819 (HY000): Your password does not satisfy the current policy requirements.


As you might have noticed, you will be prompted to enable VALIDATE PASSWORD component while setting up password for MySQL root user. If enabled, the Validate Password component will automatically check the strength of the given password and enforce the users to set only the passwords that are secure enough. If you provide a weak password, you will encounter with an error like this - ERROR 1819 (HY000): Your password does not satisfy the current policy requirements.

Technically speaking, it is not actually an error. This is a built-in security mechanism that forces the users to provide only the strong passwords based on the current password policy requirements.

Let me show you an example. I log in to MySQL server as root user using command:

$ mysql -u root -p

Create a database user with a weak password:

mysql> create user 'ostechnix'@'localhost' identified by 'mypassword';

And I encounter with the following error:

ERROR 1819 (HY000): Your password does not satisfy the current policy requirements
 

See? The Validate Password component doesn't allow me to create a user with a weak password (i.e. mypassword in this case).

You will keep getting this error until the password meets the requirements of the current password policy or you disable the Validate Password component. We will see how to do it in the following section.

Fix - MySQL ERROR 1819 (HY000): Your password does not satisfy the current policy requirements

There are three levels of password validation policy enforced when Validate Password plugin is enabled:

  • LOW Length >= 8 characters.
  • MEDIUM Length >= 8, numeric, mixed case, and special characters.
  • STRONG Length >= 8, numeric, mixed case, special characters and dictionary file.

Based on these policy levels, you need to set an appropriate password. For example, if the password validation policy is set to Medium, you must set a password that has at least 8 characters including a number, lowercase, uppercase and special characters.

First we need to  find the current password policy level. To do so, run the following command to show Password Validation Plugin system variables:

mysql> SHOW VARIABLES LIKE 'validate_password%';

Sample output:

+--------------------------------------+--------+
| Variable_name                        | Value  |
+--------------------------------------+--------+
| validate_password.check_user_name    | ON     |
| validate_password.dictionary_file    |        |
| validate_password.length             | 8      |
| validate_password.mixed_case_count   | 1      |
| validate_password.number_count       | 1      |
| validate_password.policy             | MEDIUM |
| validate_password.special_char_count | 1      |
+--------------------------------------+--------+
7 rows in set (0.09 sec)

As you can see, the currently enforced password level is Medium. So our password should be 8  characters long with a number, mixed case and special characters.

I am going to set this password - Password123#@! using command:

mysql> create user 'ostechnix'@'localhost' identified by 'Password123#@!'; Query OK, 0 rows affected (0.36 sec)

 

See? It works now! So, in order to fix the "ERROR 1819 (HY000)..." error, you need to enter a password as per the current password validation policy.

Change password validation policy in MySQL

You can also solve the "ERROR 1819 (HY000)..." by setting up a lower level password policy.

To do so, run the following command from the mysql prompt:

mysql> SET GLOBAL validate_password.policy = 0;

Or,

mysql> SET GLOBAL validate_password.policy=LOW;

Then check if the password validation policy has been changed to low:

mysql> SHOW VARIABLES LIKE 'validate_password%';

Sample output:

+--------------------------------------+-------+
| Variable_name                        | Value |
+--------------------------------------+-------+
| validate_password.check_user_name    | ON    |
| validate_password.dictionary_file    |       |
| validate_password.length             | 8     |
| validate_password.mixed_case_count   | 1     |
| validate_password.number_count       | 1     |
| validate_password.policy             | LOW   |
| validate_password.special_char_count | 1     |
+--------------------------------------+-------+
7 rows in set (0.00 sec)

Now you can create a user with a weak password like below:

mysql> create user 'senthil'@'localhost' identified by 'password';

To revert back to MEDIUM level policy, simply run this command from mysql prompt:

mysql> SET GLOBAL validate_password.policy=MEDIUM;

If the password policy doesn't change, exit from the mysql prompt and restart mysql service from your Terminal window:

$ sudo systemctl restart mysql

Now it should work.

Heads Up: One of our reader has pointed out that there is typo in the following command:

mysql> SET GLOBAL validate_password.policy=LOW;

It should be:

mysql> SET GLOBAL validate_password_policy=LOW;

Note the underscore in the above command. Since I have deleted the setup, I have no way of verifying this command. But I assume the command has been changed in the newer versions of MySQL.

Disable password validation policy

If you like to create users with weak password, simply disable the Validate Password component altogether and re-enable it back after creating the users.

Log in to the MySQL server:

$ mysql -u root -p

To temporarily disable Validate Password component, run the following command from MySQL prompt:

mysql> UNINSTALL COMPONENT "file://component_validate_password";

Create the users with any password of your choice:

mysql> create user 'kumar'@'localhost' identified by '123456';

Finally, enable Validate Password component:

mysql> INSTALL COMPONENT "file://component_validate_password";
 

 

 

 

 

 

Đọc thêm..

Mysql Drop multi tables

- Drop multi table:

SET @tables = (SELECT CONCAT('DROP TABLE ', GROUP_CONCAT(table_name) , ';')
  FROM INFORMATION_SCHEMA.TABLES
  WHERE table_name LIKE 'tbl_data_9%');

PREPARE dynStmt FROM @tables;
EXECUTE dynStmt;
DEALLOCATE PREPARE dynStmt;

- Show table like:

SHOW TABLES LIKE 'tbl_data%';

- Create table like;

 create table tbl_data_1 like tbl_exam_data_1;
Đọc thêm..

Command for user in mysql

- Show user:
SELECT user FROM mysql.user;
or
SELECT user,host FROM mysql.user;
or
SELECT user,host,password FROM mysql.user;
or
SELECT host,user,authentication_string FROM mysql.user; 
 
- Show permission: 
SHOW GRANTS FOR 'root'@'localhost';
or 
SHOW GRANTS FOR CURRENT_USER;
or 
SHOW GRANTS FOR CURRENT_USER(); 

- Create user:
CREATE USER 'newuser'@'localhost' IDENTIFIED BY 'password';
GRANT ALL PRIVILEGES ON * . * TO 'newuser'@'localhost';
FLUSH PRIVILEGES;
 
- Show tables crash:
show table status where comment like '%crash%';
 
 Mysql User native password:

Here's the solution: (from the mysql command-line client)

# If you don't have a 127.0.0.1 equivalent user:  

CREATE USER 'root'@'127.0.0.1' IDENTIFIED WITH mysql_native_password BY 'password';  

# If you already have the user, reset its password:  

ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password';  

# Redo your grants on the 127.0.0.1 user:  

GRANT ALL PRIVILEGES ON *.* TO 'root'@'127.0.0.1'

FLUSH PRIVILEGES;

   
Đọc thêm..

Setup Master-Slave Replication in MySQL Server

MySQL replication allows you to have multiple copies of data on many systems and data is automatically copied from one database (Master) to another database (Slave). If one server goes down, the clients still can access the data from another (Slave) server database.
In this article, let us see how to configure MySQL Master-Slave replication. I am using the following two systems to in this how-to:
MySQL Master system : CentOS 6.4
Master IP Address : 192.168.1.250/24
MySQL Slave system : CentOS 6.4
IP Address: 192.168.1.150/24
Setting up MySQL Master
Adjust iptables to allow 3306 port:
[root@server ~]# vi /etc/sysconfig/iptables
# Firewall configuration written by system-config-firewall
# Manual customization of this file is not recommended.
*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
-A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
-A INPUT -p icmp -j ACCEPT
-A INPUT -i lo -j ACCEPT
-A INPUT -p udp -m state --state NEW --dport 3306 -j ACCEPT
-A INPUT -p tcp -m state --state NEW --dport 3306 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT
-A INPUT -j REJECT --reject-with icmp-host-prohibited
-A FORWARD -j REJECT --reject-with icmp-host-prohibited
COMMIT
Save and restart iptables:
root@server ~]# service iptables restart
Now install MySQL packages using the following command:
[root@server ~]# yum install mysql-server mysql -y
Start mysqld service.
[root@server ~]# service mysqld start
[root@server ~]# chkconfig mysqld on
Setup MySQL Root password:
[root@server ~]# /usr/bin/mysql_secure_installation
NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MySQL
 SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!

In order to log into MySQL to secure it, we'll need the current
 password for the root user.  If you've just installed MySQL, and
 you haven't set the root password yet, the password will be blank,
 so you should just press enter here.

Enter current password for root (enter for none):
 OK, successfully used password, moving on...

Setting the root password ensures that nobody can log into the MySQL
 root user without the proper authorisation.

Set root password? [Y/n] y
New password:
Re-enter new password:
Password updated successfully!
Reloading privilege tables..
... Success!

By default, a MySQL installation has an anonymous user, allowing anyone
 to log into MySQL without having to have a user account created for
 them.  This is intended only for testing, and to make the installation
 go a bit smoother.  You should remove them before moving into a
 production environment.

Remove anonymous users? [Y/n]
... Success!
Normally, root should only be allowed to connect from 'localhost'.  This
ensures that someone cannot guess at the root password from the network.

Disallow root login remotely? [Y/n]
... Success!

By default, MySQL comes with a database named 'test' that anyone can
access.  This is also intended only for testing, and should be removed
before moving into a production environment.

Remove test database and access to it? [Y/n]
 - Dropping test database...
 ... Success!
 - Removing privileges on test database...
 ... Success!

Reloading the privilege tables will ensure that all changes made so far
 will take effect immediately.

Reload privilege tables now? [Y/n]
 ... Success!

Cleaning up...

All done!  If you've completed all of the above steps, your MySQL
installation should now be secure.

Thanks for using MySQL!
Configure MySQL Master
Open /etc/my.cnf file and add the following lines under [mysqld] section:
[root@server ~]# vi /etc/my.cnf
[mysqld]
server-id = 1
binlog-do-db=unixmen
expire-logs-days=7
relay-log = /var/lib/mysql/mysql-relay-bin
relay-log-index = /var/lib/mysql/mysql-relay-bin.index
log-error = /var/lib/mysql/mysql.err
master-info-file = /var/lib/mysql/mysql-master.info
relay-log-info-file = /var/lib/mysql/mysql-relay-log.info
log-bin = mysql-bin

datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
user=mysql
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0

[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
Here unixmen is the database name to be replicated to the Slave system.
Once you are done, restart MySQL service:
[root@server ~]# service mysqld restart
Stopping mysqld:                                           [  OK  ]
Starting mysqld:                                           [  OK  ]
Now login to MySQL and create a Slave user and password. For instance, we will use sk as Slave username and centos as password:
[root@server ~]# mysql -u root -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.1.69-log Source distribution

Copyright (c) 2000, 2013, 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> STOP SLAVE;
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> GRANT REPLICATION SLAVE ON *.* TO 'sk'@'%' IDENTIFIED BY 'centos';
Query OK, 0 rows affected (0.00 sec)

mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)

mysql> FLUSH TABLES WITH READ LOCK;
Query OK, 0 rows affected (0.00 sec)

mysql> SHOW MASTER STATUS;
 +------------------+----------+--------------+------------------+
 | File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |
 +------------------+----------+--------------+------------------+
 | mysql-bin.000001 |      106 | unixmen      |                  |
 +------------------+----------+--------------+------------------+
 1 row in set (0.01 sec)

mysql> exit
Bye
Note down the file(mysql-bin.000001) and position number (106), you may need these values later.
Backup Master server database
Enter the following command to dump all Master databases and save them. We will transfer these databases to Slave server later:
[root@server ~]# mysqldump --all-databases --user=root --password --master-data > masterdatabase.sql
This will create a file called masterdatabase.sql. This will take some time depending upon the databases size.
Again login to MySQL as root user and unlock the tables:
[root@server ~]# mysql -u root -p
 Enter password:
 Welcome to the MySQL monitor.  Commands end with ; or \g.
 Your MySQL connection id is 4
 Server version: 5.1.69-log Source distribution

Copyright (c) 2000, 2013, 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> UNLOCK TABLES;
Query OK, 0 rows affected (0.01 sec)

mysql> quit
Bye
Copy the masterdatabase.sql file to your Slave server. Here, I copy this file to /home folder. So the command should be:
[root@server ~]# scp masterdatabase.sql root@192.168.1.150:/home
root@192.168.1.150's password: 
masterdatabase.sql                            100%  507KB 506.7KB/s   00:00
Setting up MySQL Slave
We have done Master side installation. Now we have to start on Slave side. Install MySQL packages on Slave server:
[root@server ~]# yum install mysql-server mysql -y
Start mysqld service:
[root@server ~]# service mysqld start
[root@server ~]# chkconfig mysqld on
Seting up MySQL Root password:
[root@server ~]# /usr/bin/mysql_secure_installation
NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MySQL
 SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!

In order to log into MySQL to secure it, we'll need the current
 password for the root user.  If you've just installed MySQL, and
 you haven't set the root password yet, the password will be blank,
 so you should just press enter here.

Enter current password for root (enter for none):
 OK, successfully used password, moving on...

Setting the root password ensures that nobody can log into the MySQL
 root user without the proper authorisation.

Set root password? [Y/n] y
New password:
Re-enter new password:
Password updated successfully!
Reloading privilege tables..
... Success!

By default, a MySQL installation has an anonymous user, allowing anyone
 to log into MySQL without having to have a user account created for
 them.  This is intended only for testing, and to make the installation
 go a bit smoother.  You should remove them before moving into a
 production environment.

Remove anonymous users? [Y/n]
... Success!
Normally, root should only be allowed to connect from 'localhost'.  This
ensures that someone cannot guess at the root password from the network.

Disallow root login remotely? [Y/n]
... Success!

By default, MySQL comes with a database named 'test' that anyone can
access.  This is also intended only for testing, and should be removed
before moving into a production environment.

Remove test database and access to it? [Y/n]
 - Dropping test database...
 ... Success!
 - Removing privileges on test database...
 ... Success!

Reloading the privilege tables will ensure that all changes made so far
 will take effect immediately.

Reload privilege tables now? [Y/n]
 ... Success!

Cleaning up...

All done!  If you've completed all of the above steps, your MySQL
installation should now be secure.

Thanks for using MySQL!
Configure MySQL Slave
Open the file /etc/my.cnf and add the following entries under [mysqld] section as shown below. Replace the database name and master server IP Address with your own:
[root@server ~]# vi /etc/my.cnf 
[mysqld]
server-id = 2     
master-host=192.168.1.250  
master-connect-retry=60
master-user=sk
master-password=centos
replicate-do-db=unixmen
relay-log = /var/lib/mysql/mysql-relay-bin
relay-log-index = /var/lib/mysql/mysql-relay-bin.index
log-error = /var/lib/mysql/mysql.err
master-info-file = /var/lib/mysql/mysql-master.info
relay-log-info-file = /var/lib/mysql/mysql-relay-log.info
log-bin = mysql-bin
[...]
Here 192.168.1.200 is Master server IP address, sk is Master server database user, centos is password of user sk, unixmen is Master database name.
Save and exit the file.
Import the master database:
[root@server ~]# mysql -u root -p < /home/masterdatabase.sql 
Enter password:
[root@server ~]# service mysqld restart
Stopping mysqld:                                           [  OK  ]
Starting mysqld:                                           [  OK  ]
Now log in to MySQL as root user and tell the Slave server to where to look for Master log file which is we have created on Master server using the command SHOW MASTER STATUS; (File – mysql-bin.000001 and Position – 106). Make sure that you changed the Master server IP address, username and password as your own:
[root@server ~]# mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.1.69-log Source distribution

Copyright (c) 2000, 2013, 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> SLAVE STOP;
Query OK, 0 rows affected (0.01 sec)

mysql> CHANGE MASTER TO MASTER_HOST='192.168.1.250', MASTER_USER='sk', MASTER_PASSWORD='centos', MASTER_LOG_FILE='mysql-bin.000001', MASTER_LOG_POS=106;
Query OK, 0 rows affected (0.03 sec)

mysql> SLAVE START;
Query OK, 0 rows affected (0.01 sec)

mysql> SHOW SLAVE STATUS\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.1.250
                  Master_User: sk
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000002
          Read_Master_Log_Pos: 106
               Relay_Log_File: mysql-relay-bin.000003
                Relay_Log_Pos: 251
        Relay_Master_Log_File: mysql-bin.000002
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB: unixmen
          Replicate_Ignore_DB: 
           Replicate_Do_Table: 
       Replicate_Ignore_Table: 
      Replicate_Wild_Do_Table: 
  Replicate_Wild_Ignore_Table: 
                   Last_Errno: 0
                   Last_Error: 
                 Skip_Counter: 0
          Exec_Master_Log_Pos: 106
              Relay_Log_Space: 551
              Until_Condition: None
               Until_Log_File: 
                Until_Log_Pos: 0
           Master_SSL_Allowed: No
           Master_SSL_CA_File: 
           Master_SSL_CA_Path: 
              Master_SSL_Cert: 
            Master_SSL_Cipher: 
               Master_SSL_Key: 
        Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
                Last_IO_Errno: 0
                Last_IO_Error: 
               Last_SQL_Errno: 0
               Last_SQL_Error: 
1 row in set (0.00 sec)
Test MySQL Replication
Master side:
[root@server ~]# mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.1.69-log Source distribution

Copyright (c) 2000, 2013, 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> create database unixmen;
Query OK, 1 row affected (0.04 sec)

mysql> use unixmen;
Database changed

mysql> create table sample (c int);
Query OK, 0 rows affected (0.08 sec)

mysql> insert into sample (c) values (1);
Query OK, 1 row affected (0.01 sec)

mysql> select * from sample;
+------+
| c    |
+------+
|    1 |
+------+
1 row in set (0.01 sec)

mysql>
Slave side:
[root@server ~]# mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 5.1.69-log Source distribution

Copyright (c) 2000, 2013, 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> use unixmen;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> select * from sample;
+------+
| c    |
+------+
|    1 |
+------+
1 row in set (0.01 sec)

mysql>
That’s it. Now the tables created in the Master server are automatically replicated to the Slave server.


(unixmen)
Đọc thêm..

Use DRBD to Provide Rock-Solid MySQL Redundancy


we explained various options for clustering and providing HA-MySQL. In the failover category, we mentioned DRBD as the premiere way with which to accomplish a rock-solid redundant MySQL setup. This is how you can implement DRBD with Heartbeat for MySQL.
Before we start, let’s quickly cover the architectural configuration of your two servers, and talk about performance. Firstly, you are going to use two “commodity” servers, which is not to imply slow or generic. You want to dedicate your fastest servers, because this type of configuration comes at a cost; it is doing much more than you may think a primary/secondary failover setup would. Therefore, I/O capabilities are probably the most important aspect. As Florian Haas of LINBIT (the creators of DRBD) points out, you can run two separate instances at once to avoid under-utilizing your secondary server. Each node will be the primary for its main instance, and a secondary for the other node.
Second, conceptually, you will configure MySQL to live on a DRBD replicated device. Heartbeat will monitor MySQL availability, and in the event a failover is necessary, the secondary server will mount the file system, steal the virtual IP, and start up MySQL.
Finally, performance: Do not take shortcuts. Yes, you need to ensure DRBD has a dedicated network interface to use. Also, spend the time optimizing as many aspects of your I/O subsystem as possible. With DRBD, every little bit helps, and cutting corners in the initial setup phase often means that you have to live with your choices (or schedule a downtime).
A deployment my team recently completed used two Dell M600 blades with dual quad-core Xeons, 16GB of RAM, dual 146GB SAS drives and—of course—dual GigE network ports. It is fast, but if great care is not taken this type of configuration can quickly slow down even this hardware.

Down to Business

The steps we must take are:
  1. Create partitions, configure DRBD replication, and create a file system
  2. Make MySQL use the DRBD volume for its DB store location
  3. Configure Heartbeat to monitor MySQL, an IP, and the DRBD volume

Step 1: Configure DRBD

First we need to create a partition. You can do this with LVM to allow future resizing of the DRBD volume, but know that it cannot be done live. This gets a little confusing at times, so here is the summary: you will create a partition and give it to DRBD, which will create its own device. Then, you will create a filesystem on top of the /dev/drbd0 device. So create the first one that we will give to DRBD, ours ended up being: /dev/vg00/drbd0. You must repeat the same steps on the secondary node as well.
Second, we get to configure DRBD. The sample configuration below is the basic set needed to get it working. You will probably want to adjust the sync rate to allow DRBD to use more bandwidth, as well as various timeout settings and buffer tweakables. Maybe we will write a followup article about DRBD tuning, but our scope at this point is to get it working.
global {
  usage-count yes;
}
common {
  protocol C;
  disk {
    on-io-error detach;
  }
  syncer {
    rate 10M;
  }
}

resource mysql {
  startup {
    wfc-timeout 0;
    degr-wfc-timeout 120;
  }
  on host1.fqdn {
        device    /dev/drbd0;
        disk      /dev/vg00/drbd0;
        address  1.1.1.1:8000;
        meta-disk   internal;
  }
  on host2.fqdn {
        device    /dev/drbd0;
        disk      /dev/vg00/drbd0;
        address  1.1.1.2:8000;
        meta-disk   internal;
  }
}
You can now tell DRBD to create its device with drbdadm create-md mysql.
Run this command on both the primary and secondary nodes, and observe /proc/drbd. Both servers should report the state as Secondary/Secondary at this time.
To actually enable replication, you must promote one server to Primary status with drbdadm -- --overwrite-data-of-peer primary mysql.
You can watch /proc/drbd, and when all data is sync’d up, the output will look like this on the Primary node:
0: cs:Connected st:Primary/Secondary ds:UpToDate/UpToDate C r---
    ns:319107812 nr:2680 dw:319109368 dr:6489590 al:3631542 
    bm:122 lo:0 pe:0 ua:0 ap:0 oos:0
Congratulations, you now have a replicated volume!
Finally, create the file system that we will mount. Using /dev/drbd0, go ahead and create an EXT3 file system. Only do this on the primary node, as the changes will start replicating to the other server.

Step 2: Give it to MySQL

MySQL needs to store its data on this DRBD-replicated volume, so ensure it is mounted now. If you wish to use /disk/mysql as the mount point, for example, you would edit my.cnf thusly:
datadir=/disk/mysql
You will want to stop MySQL and rsync the contents of its existing datadir to the new location. After making sure DRBD is caught up (/proc/drbd reports UpToDate/UpToDate), restart MySQL and ensure everything is happy.

Step 3: Heartbeat Failover

Heartbeat can be amazingly complex, or amazingly simple. To just get it working this configuration is fairly simple, but do know that you will want to spend some time with the documentation if this is your first Linux-HA experience.
The haresources file for this configuration needs:
node1.fqdn 1.1.1.3 drbddisk::mysql 
Filesystem::/dev/drbd0::/disk/mysql::ext3 mysqld
This line lists the primary node, virtual IP to use, and other resources that are managed. Order matters, so ensure you list the IP before mysqld (it’s needed to start MySQL), and the file system before mysqld for the same reason. Once this is done, you can start the heartbeat service and everything should be working. Check the logs on both machines to ensure sanity.
Note: we did not mention configuring the IP address at all. Do not be tempted to put it in the normal places, because we want heartbeat alone to manage bringing up and down the network interface. The same applies to the file system: do not put it in/etc/fstab.
The quickest (and safest) way to test a failover is to simply stop the heartbeat service on the primary node.
There are many other ways to configure DRBD, including a Primary/Primary setup if you wish to run GFS and mount the file system on two nodes at the same time. This configuration, however, gets you an extremely robust MySQL setup that is not dependent on any single piece of hardware.
Đọc thêm..

Error privillege mysql

Question:

when I view a database and click "Privileges" I get the following error

"SQL query: DocumentationEdit

(SELECT `User`, `Host`, `Select_priv`, `Insert_priv`, `Update_priv`, `Delete_priv`, `Create_priv`, `Drop_priv`, `Grant_priv`, `Index_priv`, `Alter_priv`, `References_priv`, `Create_tmp_table_priv`, `Lock_tables_priv`, `Create_view_priv`, `Show_view_priv`, `Create_routine_priv`, `Alter_routine_priv`, `Execute_priv`, `Event_priv`, `Trigger_priv`, `Db` FROM `mysql`.`db` WHERE 'mysql' LIKE `Db` AND NOT (`Select_priv` = 'N' AND `Insert_priv` = 'N' AND `Update_priv` = 'N' AND `Delete_priv` = 'N' AND `Create_priv` = 'N' AND `Drop_priv` = 'N' AND `Grant_priv` = 'N' AND `References_priv` = 'N' AND `Create_tmp_table_priv` = 'N' AND `Lock_tables_priv` = 'N' AND `Create_view_priv` = 'N' AND `Show_view_priv` = 'N' AND `Create_routine_priv` = 'N' AND `Alter_routine_priv` = 'N' AND `Execute_priv` = 'N' AND `Event_priv` = 'N' AND `Trigger_priv` = 'N')) UNION (SELECT `User`, `Host`, `Select_priv`, `Insert_priv`, `Update_priv`, `Delete_priv`, `Create_priv`, `Drop_priv`, `Grant_priv`, `Index_priv`, `Alte[...]

MySQL said: Documentation
#1054 - Unknown column 'Event_priv' in 'field list' "

Answer:

Run cmd

#mysql_upgrade -u root -p --force
Đọc thêm..

Kiến thức tổng quan về lập trình PHP

1- Cấu trúc cơ bản:

PHP cũng có thẻ bắt đầu và kết thúc giống với ngôn ngữ HTML. Chỉ khác, đối với PHP chúng ta có nhiều cách để thể hiện.



Cách 1 : Cú pháp chính:

<?php Mã lệnh PHP ?>

Cách 2: Cú pháp ngắn gọn

<? Mã lệnh PHP ?>

Cách 3: Cú pháp giống với ASP.

<% Mã lệnh PHP %>

Cách 4: Cú pháp bắt đầu bằng script

<script language=php>

.....

</script>


Mặc dù có 4 cách thể hiện. Nhưng đối với 1 lập trình viên có kinh nghiệm thì việc sử dụng cách 1 vẫn là lựa chon tối ưu.

Trong PHP để kết thúc 1 dòng lệnh chúng ta sử dụng dấu ";"

Để chú thích 1 đoạn dữ liệu nào đó trong PHP ta sử dụng dấu "//" cho từng dòng. Hoặc dùng cặp thẻ "/*……..*/" cho từng cụm mã lệnh.

<?php

echo "Test PHP";  //vi du ve code PHP

/* Ta co the chu thich

mot doan cum tu trong php */

?>


2- Xuất giá trị ra trình duyệt:



Để xuất dữ liệu ra trình duyệt chúng ta có những dòng cú pháp sau :

+ Echo "Thông tin";

+ Printf "Thông tin";

Thông tin bao gồm : biến, chuỗi, hoặc lệnh HTML ….

<?php

echo "Test PHP";

printf ("<font color=red>test</font>");

?>

Nễu giữa hai chuỗi muốn liên kết với nhau ta sử dụng dấu "."

<?php

echo "Test PHP" . "PHP is simple";

?>


3- Khái niệm biến, hằng, chuỗi và các kiểu dữ liệu.


a) Biến trong PHP.



Biến được xem là vùng nhớ dữ liệu tạm thời. Và giá trị có thể thay đổi được. Biến được bắt đầu bằng ký hiệu "$". Và theo sau chúng là 1 từ, 1 cụm từ nhưng phải viết liền hoặc có gạch dưới.

1 biến được xem là hợp lệ khi nó thỏa các yếu tố :

+ Tên của biến phải bắt đầu bằng dấu gạch dưới và theo sau là các ký tự, số hay dấu gạch dưới.

+ Tên của biến không được phép trùng với các từ khóa của PHP.

Trong PHP để sử dụng 1 biến chúng ta thường phải khai báo trước, tuy nhiên đối với các lập trình viên khi sử dụng họ thường xử lý cùng một lúc các công việc, nghĩa là vừa khái báo vừa gán dữ liệu cho biến.

Bản thân biến cũng có thể gãn cho các kiểu dữ liệu khác. Và tùy theo ý định của người lập trình mong muốn trên chúng.

Một số ví dụ về biến :

PHP Example


b) Khái niệm về hằng trong PHP.



Nếu biến là cái có thể thay đổi được thì ngược lại hằng là cái chúng ta không thể thay đổi được. Hằng trong PHP được định nghĩa bởi hàm define theo cú pháp: define (string tên_hằng, giá_trị_hằng ).

Cũng giống với biến hằng được xem là hợp lệ thì chúng phải đáp ứng 1 số yếu tố :

+ Hằng không có dấu "$" ở trước tên.

+ Hằng có thể truy cập bất cứ vị trí nào trong mã lệnh

+ Hằng chỉ được phép gán giá trị duy nhất 1 lần.

+ Hằng thường viết bằng chữ in để phân biệt với biến

Ví dụ :

PHP Example


c) Khái niệm về chuỗi:



Chuỗi là một nhóm các kỹ tự, số, khoảng trắng, dấu ngắt được đặt trong các dấu nháy.

Ví dụ:

‘Huy’

"welcome to VietNam"

Để tạo 1 biễn chuỗi, chúng ta phải gán giá trị chuỗi cho 1 biến hợp lệ.

Ví dụ:

$fisrt_name= "Nguyen";

$last_name= ‘Van A’;

Để liên kết 1 chuỗi và 1 biến chúng ta thường sử dụng dấu "."

Ví dụ:

PHP Example


d) Kiểu dữ liệu trong PHP



Các kiểu dữ liệu khác nhau chiếm các lượng bộ nhớ khác nhau và có thể được xử lý theo cách khác nhau khi chúng được theo tác trong 1 script.

Trong PHP chúng ta có 6 kiểu dữ liệu chính như sau :

PHP Example

Chúng ta có thể sử dụng hàm dựng sẵn gettype() của PHP4 để kiểm tra kiểu của bất kỳ biến.

Ví dụ:

PHP Example
Đọc thêm..

Cơ bản Mysql

Cơ bản về ngôn ngữ SQL và Mysql

Mysql là hệ quản trị dữ liệu miễn phí, được tích hợp sử dụng chung với apache, PHP. Chính yếu tố phát triển trong cộng đồng mã nguồn mở nên mysql đã qua rất nhiều sự hỗ trợ của những lập trình viên yêu thích mã nguồn mở. Mysql cũng có cùng một cách truy xuất và mã lệnh tương tự với ngôn ngữ SQL. Nhưng Mysql không bao quát toàn bộ những câu truy vấn cao cấp như SQL. Về bản chất Mysql chỉ đáp ứng việc truy xuất đơn giản trong quá trình vận hành của website nhưng hầu hết có thể giải quyết các bài toán trong PHP.

1- Cách khởi động và sử dụng MYSQL.

Chúng ta sử dụng command như sau:

mysql –h hostname –u user –p pass

Để truy cập vào cơ sở dữ liệu.

Hoặc sử dụng bộ appserv để vào nhanh hơn theo đường dẫn sau:

Start/ Appserv/ Mysql command Line client

Sau đó nhập password mà chúng ta đã đặt vào.

2- Những định nghĩa cơ bản:

a) Định nghĩa cơ sở dữ liệu, bảng, cột:

Cơ sở dữ liệu: là tên của cơ sở dữ liệu chúng ta muốn sử dụng

Bảng: Là 1 bảng giá trị nằm trong cơ sở dữ liệu.

Cột là 1 giá trị nằm trong bảng. Dùng để lưu trữ các trường dữ liệu.

Thuộc tính

Ví dụ:

PHP Example

Như vậy ta có thể hiểu như sau:

1 cơ sở dữ liệu có thể bao gồm nhiều bảng.

1 bảng có thể bao gồm nhiều cột

1 cột có thể có hoặc không có những thuộc tính.

b) Định nghĩa 1 số thuật ngữ:

NULL : Giá trị cho phép rỗng.

AUTO_INCREMENT : Cho phép giá trị tăng dần (tự động).

UNSIGNED : Phải là số nguyên dương

PRIMARY KEY : Cho phép nó là khóa chính trong bảng.

c)Loại dữ liệu trong Mysql:

Ở đây chúng tả chỉ giới thiệu 1 số loại thông dụng: 1 số dữ liệu khác có thể tham khảo trên trang chủ của mysql.

PHP Example

3- Những cú pháp cơ bản:

Cú pháp tạo 1 cơ sở dữ liệu:

CREATE DATABASE tên_cơ_sở_dữ_liệu;

Cú pháp sử dụng cơ sở dữ liệu: Use tên_database;

Cú pháp thoát khỏi cơ sở dữ liệu: Exit

Cú pháp tạo 1 bảng trong cơ sở dữ liệu:

CREATE TABLE user ( ,…,…..)

Ví dụ:

mysql> create table user(user_id INT(15) UNSIGNED NOT NULL AUTO_INCREMENT, username VARCHAR(255) NOT NULL, password CHAR(50) NOT NULL, email VARCHAR(200) NOT NULL, PRIMARY KEY (user_id));

Hiển thị có bao nhiều bảng: show tables;

Hiển thị có bao nhiêu cột trong bảng: show columns from table;

Thêm 1 cột vào bảng :

ALTER TABLE tên_bảng ADD

AFTER

Ví dụ:

mysql> alter table user add sex varchar(200) NOT NULL after email;

4- Thêm giá trị vào bảng:

Cú pháp:

INSERT INTO Tên_bảng(tên_cột) VALUES(Giá_trị_tương_ứng);

Ví dụ:

mysql> insert into user(user_id,username,password,email,sex) values(1,"user1","123456","user1@test.com","F");

5- Truy xuất dữ liệu:

Cú pháp:

SELECT tên_cột FROM Tên_bảng;

Ví dụ:

mysql> select user_id,username from user;

6- Truy xuất dữ liệu với điều kiện:

Cú pháp:

SELECT tên_cột FROM Tên_bảng WHERE điều kiện;
Ví dụ:

mysql> select user_id,username from user where user_id=2;

7- Truy cập dữ liệu và sắp xếp theo trình tự

Cú pháp:

SELECT tên_cột FROM Tên_bảng

WHERE điều kiện (có thể có where hoặc không)

ORDER BY Theo quy ước sắp xếp.

Trong đó quy ước sắp xếp bao gồm hai thông số là ASC (từ trên xuống dưới), DESC (từ dưới lên trên).

mysql> select user_id,username from user order by username ASC ;

8- Truy cập dữ liệu có giới hạn :

Cú pháp:

SELECT tên_cột FROM Tên_bảng

WHERE điều kiện (có thể có where hoặc không)

LIMIT vị trí bắt đầu, số record muốn lấy ra

Ví dụ:
mysql> select user_id,username from user order by username ASC limit 0,10 ;

9- Cập nhật dữ liệu trong bảng:

Cú pháp:

Update tên_bảng set tên_cột=Giá trị mới

WHERE (điều kiện).

Nếu không có ràng buộc điều kiện, chúng sẽ cập nhật toàn bộ giá trị mới của các record trong bảng.

Ví dụ:

mysql> update user set email="admin@qhonline.info" where user_id=1 ;

10- Xóa dữ liệu trong bảng:

Cú pháp:

DELETE FROM tên_bảng WHERE (điều kiện).

Nếu không có ràng buộc điều kiện, chúng sẽ xó toàn bộ giá trị của các record trong bảng.

Ví dụ

mysql>delete from user where user_id=1 ;
Đọc thêm..