Archive for July 15, 2011

Space into the database via SSH

Space Support SSH,It can be easily imported into the database via SSH

1. The database file format export to .sql,Do not compressed into .zip or .tar.gz file

2. .sql files uploaded to the server using FTP

3. Create a database,If the database does not exist

4. By SSH login server

5. In the SSH terminal,.sql file into the directory where

6. Run the following command:
# mysql -u username -p database_name < file.sql7. 然后,会提示输入数据库的密码,输入密码后就开始导入数据库的过程。在SSH中,使用命令行的方式,只能导入.sql文件

Comments

mysql master-slave synchronization principle

Replication thread

The Mysql Replication is an asynchronous replication process,From a Mysql instace(We call Master)Copied to another Mysql instance(We call it Slave)。Realization of the entire replication process between Master and Slave is done mainly by the three threads,Two threads(Sql threads and IO thread)In the end Slave,Another thread(IO thread)At the end of Master。

To achieve the MySQL Replication ,You must first open the Master end of the Binary Log(mysql-bin.xxxxxx)Features,Otherwise it is impossible to achieve。Various operations because the whole process is actually a copy Slave from the Master side to obtain the log and then himself completely in the implementation of the log in the order recorded。Open the MySQL Binary Log can be used in the process of starting MySQL Server "-log-bin" parameter options,Or my.cnf configuration file mysqld parameter set([mysqld]Section identifies the parameters after)Increase the "log-bin" Parameter。

The basic process is as follows MySQL replication:

  1. Slave IO above the thread connecting the Master,And requests the specified log file from the specified location(Or from the beginning of the log)After the contents of the log;

   2. After receiving the request from the Master of Slave IO thread,Log information is read by the specified log specify the location of the IO thread is responsible for copying the information after the request,Back to the Slave IO thread ends。Addition to the information contained in the log, in addition to the return information,This also includes the name of the information returned by the end of the Master Binary Log file and its location in the Binary Log in;

  3. Slave IO thread after receiving the information,The contents of the log of the received file are sequentially written to the Slave Relay Log end(mysql-relay-bin.xxxxxx)The very end,And records the file name and location of the read end of the Master bin-log to the master- info file,So that when the next time can be clearly read speed Master "I need a position from which bin-log log content beginning later,please send to me"

   4. Slave SQL thread detected Relay Log in newly added content after,Parses the contents of the Log file immediately becomes a real implementation of the Master side when those Query executable statement,And their implementation of these Query。such,Actually end in Master and Slave implementation of the same end Query,So the data is exactly the same at both ends。

Actually,In the old version,MySQL replication is not achieved at the end Slave to work together by the IO thread and the SQL thread two threads completed,But by a separate thread to do all the work。But MySQL's engineers soon discovered,Doing so there is a big risk and performance problems,Notably the following:

First of all,If you do this work independently, then by a single thread,Causes the end of the Master Copy,Binary Log Log,And parsing these logs,Then the execution of this process itself becomes a serial process,Performance will naturally be subject to greater restrictions,Replication of this architecture naturally relatively long delay。

Secondly,After this replication thread Slave side get up from Master Binary Log end,Then we need to resolve these,Restore the original end Query Master performed,Then in its own execution。During this process,Master end and is likely to have generated a lot of changes and generate a large amount of information Binary Log。If there is a fault can not be repaired at the end of this storage system Master stage,So at this stage all the changes arising will be lost forever,I can not get it back。This potential risk is particularly prominent in the Slave-side pressure is relatively large when,If the pressure is relatively large because the Slave,Log analysis and application of these logs time spent will naturally be longer,The data will also be lost more。

and so,In the latter part of the transformation,The new version of MySQL in order to minimize this risk,And improve the performance of replication,Copy the Slave-side instead of two threads to finish,That is, the aforementioned IO thread and the SQL thread。The first proposed improvement program is Yahoo!An engineer "Jeremy Zawodny"。Through this transformation,This will not only largely solve the performance problems,Shorten the delay time of asynchronous,While reducing the potential amount of data loss。

of course,Even replaced the two threads so now to collaborate after,Also there is still the possibility of Slave data latency and data loss is,After all, this replication is asynchronous。Whenever you change the data in a transaction not,These problems are there。

If you want to completely avoid these problems,We can only use the MySQL Cluster to solve。But the MySQL Cluster know when I wrote this part of,It is still a number of memory database solutions,That is, all the data including the index will need all Load into memory,This memory requirement is very large large,For the general popularity of applications can be implemented not too much。of course,Before that communicate with the MySQL CTO David when,MySQL is now achieve continuous improvement of its Cluster,One very large data do not allow a change is all the Load into memory,But merely an index of all Load into memory,I would like to believe that after the completion of the transformation will be more popular MySQL Cluster,Can be implemented will be greater。

Comments

mysql-yourself Mathematical Functions

All mathematical functions return NULL in case of an error。


Unary minus。Sign change parameters。
mysql> select – 2;

note,If this operator is used with a BIGINT,The return value is a BIGINT! This means you should avoid using the integer -,That may be the value of -2 ^ 63!
ABS(X)
Returns the absolute value of X。
mysql> select ABS(2);
-> 2
mysql> select ABS(-32);
-> 32

This function can be used to secure value BIGINT。

SIGN(X)
Returns the signs of the parameters,-1、0Or 1,Depending on whether X is negative、Zero or a positive number。
mysql> select SIGN(-32);
-> -1
mysql> select SIGN(0);
-> 0
mysql> select SIGN(234);
-> 1

MOD(N,M)
 
%
mold (Similarly% operators in C)。Returns the remainder being M divided by N。
mysql> select MOD(234, 10);
-> 4
mysql> select 253 % 7;
-> 1
mysql> select MOD(29,9);
-> 2

This function can be used safely BIGINT value。
FLOOR(X)
Returns the maximum integer value not greater than X,。

mysql> select FLOOR(1.23);
-> 1
mysql> select FLOOR(-1.23);
-> -2

Note that the return value is converted to a BIGINT!
CEILING(X)
Returns the smallest integer not less than X,。
mysql> select CEILING(1.23);
-> 2
mysql> select CEILING(-1.23);
-> -1

Note that the return value is converted to a BIGINT!

ROUND(X)
It returns an integer rounding parameter X。
mysql> select ROUND(-1.23);
-> -1
mysql> select ROUND(-1.58);
-> -2
mysql> select ROUND(1.58);
-> 2

Note that the return value is converted into a BIGINT!

ROUND(X,D)
Returns rounding the parameter X has a D decimals。If D is 0,The results will have no decimal point or fractional part。
mysql> select ROUND(1.298, 1);
-> 1.3
mysql> select ROUND(1.298, 0);
-> 1

Note that the return value is converted into a BIGINT!

EXP(X)
Return value e (natural logarithm of the bottom) of the power of X。
mysql> select EXP(2);
-> 7.389056
mysql> select EXP(-2);
-> 0.135335

LOG(X)
Returns the natural logarithm of X。
mysql> select LOG(2);
-> 0.693147
mysql> select LOG(-2);
-> NULL

If you want to count on any of the bottom B of a digital X,Use the formula LOG(X)/LOG(B)。

LOG10(X)
Returns the number X of the base 10。
mysql> select LOG10(2);
-> 0.301030
mysql> select LOG10(100);
-> 2.000000
mysql> select LOG10(-100);
-> NULL

POW(X,Y)
 
POWER(X,Y)
Y return power value X。
mysql> select POW(2,2);
-> 4.000000
mysql> select POW(2,-2);
-> 0.250000
SQRT(X)
Returns the non-negative square root of X。
mysql> select SQRT(4);
-> 2.000000
mysql> select SQRT(20);
-> 4.472136

PI()
PI return value (pi)。
mysql> select PI();
-> 3.141593

COS(X)
Returns the cosine of X, Where X is given in radians。
mysql> select COS(PI());
-> -1.000000

WITHOUT(X)
Returns the sine of X,Here X is given in radians。
mysql> select SIN(PI());
-> 0.000000

TAN(X)
Returns the tangent of X,Here X is given in radians。
mysql> select TAN(PI()+1);
-> 1.557408

ACOS(X)
X returns the inverse cosine,Ie it is a cosine X。If X is not in the range -1 to 1,,Return NULL。
mysql> select ACOS(1);
-> 0.000000
mysql> select ACOS(1.0001);
-> NULL
mysql> select ACOS(0);
-> 1.570796

ASIN(X)
X returns arcsine,I.e. the sine value which is X。L X is not in the range of -1 to 1 if the,Return NULL。
mysql> select ASIN(0.2);
-> 0.201358
mysql> select ASIN(‘foo’);
-> 0.000000

ATA(X)
X returns the arctangent,That which is tangent X。
mysql> select ATAN(2);
-> 1.107149
mysql> select ATAN(-2);
-> -1.107149
ATAN2(X,Y)
Back two variables X and Y arctangent。It is similar to calculation of Y / X arctangent,In addition to signs of both parameters are used to determine the quadrant of the result。
mysql> select ATAN(-2,2);
-> -0.785398
mysql> select ATAN(PI(),0);
-> 1.570796
COT(X)
Returns the cotangent of X。
mysql> select COT(12);
-> -1.57267341
mysql> select COT(0);
-> NULL

RAND()
 
RAND(N)
Back in the range 0 to 1.0 in the random floating point values。If N is a specified integer parameter,It is used as a seed value。
mysql> select RAND();
-> 0.5925
mysql> select RAND(20);
-> 0.1811
mysql> select RAND(20);
-> 0.1811
mysql> select RAND();
-> 0.2079
mysql> select RAND();
-> 0.7888

You can not use an ORDER BY clause RAND()Value using the column,Because ORDER BY would be repeated many times computed column。However, in the MySQL3.23,you can do: SELECT * FROM table_name ORDER BY RAND(),This is beneficial to get one from SELECT * FROM table1,table2 WHERE a=b AND c
LEAST(X,Y,…)
There are two or more parameters and,The minimum return(Minimum)Parameters。Parameters were compared using the following rules:
If the return value is used in a context INTEGER,Or all of the parameters are integer values,They compared as integers。
If the return value is used in a context REAL,Or all of the real-valued parameters,As they are more real numbers。
If any parameter is sensitive to the size of a string,Parameters are compared as case-sensitive string。
In other cases,Parameters are compared as a case insensitive string。
mysql> select LEAST(2,0);
-> 0
mysql> select LEAST(34.0,3.0,5.0,767.0);
-> 3.0
mysql> select LEAST(“B”,”A”,”C”);
-> “A”

In previous versions of MySQL 3.22.5,You can use MIN()Instead LEAST。

GREATEST(X,Y,…)
Returns the maximum(Maximum)Parameters。LEAST parameters using the same rules as to compare。
mysql> select GREATEST(2,0);
-> 2
mysql> select GREATEST(34.0,3.0,5.0,767.0);
-> 767.0
mysql> select GREATEST(“B”,”A”,”C”);
-> “C”

In the MySQL 3.22.5 previous version, You can use MAX()Instead of GREATEST.
DEGREES(X)
Return parameter X,Converted from radian angle。
mysql> select DEGREES(PI());
-> 180.000000
RADIANS(X)
Return parameter X,Is converted from degrees to radians。
mysql> select RADIANS(90);
-> 1.570796

TRUNCATE(X,D)
Returns X,Truncated to D decimals。If D is 0,The results will have no decimal point or fractional part。
mysql> select TRUNCATE(1.223,1);
-> 1.2
mysql> select TRUNCATE(1.999,1);
-> 1.9
mysql> select TRUNCATE(1.999,0);
-> 1

Comments

CentOS 5 The OpenSSL and OpenSSH upgrade

The OpenSSL and OpenSSH upgraded。CentOS 5.2 is brought OpenSSL 0.9.8b,OpenSSH is 4.0p1。Upgrade follows:

openssh latest version (http://www.openssh.com/portable.html)

wget http://www.openssl.org/source/openssl-0.9.8j.tar.gz

tar zxvf openssl-0.9.8j.tar.gz

cd openssl-0.9.8j

./config –prefix=/usr

make

make test

make install

wget http://openbsd.noc.jgm.gov.ar/pub/OpenBSD/OpenSSH/portable/openssh-5.2p1.tar.gz

tar zxvf openssh-5.2p1.tar.gz

cd openssh-5.2p1

./configure –prefix=/usr –with-pam –with-zlib –sysconfdir=/etc/ssh –with-ssl-dir=/usr –with-md5-passwords

make

make install

Comments