2014年6月26日星期四

MySQL的表錯誤1064

Original post : http://anothermysqldba.blogspot.com/2014/06/mysql-table-error-1064.html

所以,我碰到一個奇怪的局面今天就來了。 

我有一個使用PHP $ _COOKIE ['PHPSESSID']的值創建內存表的系統。 

一旦有些工作已經完成它,然後刪除表。 

兩個示例表下面是我的榜樣。 


@@VERSION: 5.6.19-log 
CREATE TABLE `f7a7a8d3a7ba75b5eb1712864c9b27eb` ( 
-> `id` int(11) NOT NULL AUTO_INCREMENT, 
-> PRIMARY KEY (`id`) 
-> ) ENGINE=MEMORY; 

CREATE TABLE `8865e52c7e1bea515e7156f240729275` ( 
-> `id` int(11) NOT NULL AUTO_INCREMENT, 
-> PRIMARY KEY (`id`) 
-> ) ENGINE=MEMORY; 


現在,一些簡單的處理發生在我用那麼就像我說我放棄了他們的真實的表。 


DROP TABLE IF EXISTS f7a7a8d3a7ba75b5eb1712864c9b27eb; 
Query OK, 0 rows affected (0.09 sec) 


很簡單的,直到我跑過這個.. 


desc 8865e52c7e1bea515e7156f240729275; 
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '8865e52c7e1bea515e7156f240729275' at line 1 

DROP TABLE IF EXISTS 8865e52c7e1bea515e7156f240729275; 
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '8865e52c7e1bea515e7156f240729275' at line 1 


現在,修復其實很簡單。 但問題是,為什麼一部作品和一個失敗? 

為了解決這個問題只是反引號的表名。 我應該從一開始無論如何都做。 這僅僅是一個良好的習慣進入。 


DROP TABLE IF EXISTS `8865e52c7e1bea515e7156f240729275`; 
Query OK, 0 rows affected (0.02 sec) 


現在,我首先想到的,也許這個問題是字母數字的名稱,但我們可以看到,一個版本的作品。 所不同的不過是表名的開頭。 之一是一個整數和一個是字母字符。 如此反复..遵循最佳實踐和報價表名,以避免這種愚蠢的錯誤。 


在所有的錯誤代碼是解析錯誤.. 


perror 1064 
MySQL error code 1064 (ER_PARSE_ERROR): %s near '%-.80s' at line %d 

2014年6月17日星期二

MySQL的隨機整數

Original post: http://anothermysqldba.blogspot.com/2014/06/mysql-random-integers.html

這是不以任何手段的一項新功能,但它是一個問題,我也碰巧看到彈出飄飛。 所以,一個簡單的例子是下面的。 

到MySQL中生成一個隨機整數您可以使用地板和蘭德功能。 MySQL手冊此文件在這裡:http://dev.mysql.com/doc/refman/5.5/en/mathematical-functions.html#function_rand 

 為了獲得範圍內的隨機整數R I <= R <J2,使用表達式FLOOR(i + RAND()*(十-一)) “ 

所以,舉個例子: 

> SET @I = 3; # lower range 
> SET @J = 43 - @I; # max range minus lower range 

> SELECT FLOOR( @I + (RAND() * @J )) as lottery_numbers\G 
*************************** 1. row *************************** 
lottery_numbers: 4 
1 row in set (0.00 sec) 
> SELECT FLOOR( @I + (RAND() * @J )) as lottery_numbers\G 
*************************** 1. row *************************** 
lottery_numbers: 8 
1 row in set (0.00 sec 
> SELECT FLOOR( @I + (RAND() * @J )) as lottery_numbers\G 
*************************** 1. row *************************** 
lottery_numbers: 15 
1 row in set (0.00 sec 
> SELECT FLOOR( @I + (RAND() * @J )) as lottery_numbers\G 
*************************** 1. row *************************** 
lottery_numbers: 16 
1 row in set (0.00 sec 
> SELECT FLOOR( @I + (RAND() * @J )) as lottery_numbers\G 
*************************** 1. row *************************** 
lottery_numbers: 23 
1 row in set (0.00 sec 
> SELECT FLOOR( @I + (RAND() * @J )) as lottery_numbers\G 
*************************** 1. row *************************** 
lottery_numbers: 42 
1 row in set (0.00 sec 

2014年6月15日星期日

安裝Percona的XtraDB集群

Original post: http://anothermysqldba.blogspot.com/2014/06/installing-percona-xtradb-cluster.html

所以當然Percona的都有解釋的過程文檔。 這個博客的目的是進入了更多的細節,希望能幫助別人。 

超鏈接的點評: 
先決條件 
  • 防火牆已經被設置為允許連接到端口3306,4444,4567和4568
  • 停止iptables的內部局域網絡或調整iptable的規則。
/etc/init.d/iptables stop 

  • 的SELinux被禁用
echo 0 >/selinux/enforce 
vi /etc/selinux/config 

  • 設置SSH密鑰,並放入authorized_keys文件,這樣所有的id_rsa.pub文件值在所有服務器上的authorized_keys。
# ssh-keygen -t rsa 
# cd /root/.ssh/ 
# cp id_rsa.pub authorized_keys 
# chmod 600 /root/.ssh/authorized_keys 
# chmod 700 /root/.ssh/ 


於是我開始了一個基本的服務器安裝CentOS 6.5。 
# yum -y install http://www.percona.com/downloads/percona-release/percona-release-0.0-1.x86_64.rpm 
# yum -y install http://mirror.pnl.gov/epel/6/x86_64/epel-release-6-8.noarch.rpm 
# wget http://www.percona.com/downloads/RPM-GPG-KEY-percona /etc/pki/rpm-gpg/RPM-GPG-KEY-percona 
# wget http://www.percona.com/downloads/RPM-GPG-KEY-percona /etc/pki/rpm-gpg/RPM-GPG-KEY-percona 
# yum -y install socat 


為了避免我刪除了mysql-庫和相關的依賴任何衝突 
# rpm -e mysql-libs postfix cronie redhat-lsb-core redhat-lsb-printing redhat-lsb-graphics libcgroup numad redhat-lsb sysstat crontabs cronie-anacron redhat-lsb-compat


然後我安裝了Percona的集群包。 
# yum -y install Percona-XtraDB-Cluster-full-56 
[root@node1 ~]# /etc/init.d/mysql start 
Starting MySQL (Percona XtraDB Cluster)......... SUCCESS! 
mysql -e "CREATE FUNCTION fnv1a_64 RETURNS INTEGER SONAME 'libfnv1a_udf.so'" 
mysql -e "CREATE FUNCTION fnv_64 RETURNS INTEGER SONAME 'libfnv_udf.so'" 
mysql -e "CREATE FUNCTION murmur_hash RETURNS INTEGER SONAME 'libmurmur_udf.so'" 


所以我們可以更換,我們每個節點中刪除的項目.. 
yum -y install postfix cronie redhat-lsb-core redhat-lsb-printing redhat-lsb-graphics libcgroup numad redhat-lsb sysstat crontabs cronie-anacron redhat-lsb-compat 


因此,重複上面的步驟將安裝包,以便我們可以配置下一個集群。 

[root@node2 ~]# /etc/init.d/mysql start 
Starting MySQL (Percona XtraDB Cluster)......... SUCCESS! 
[root@node3 ~]# /etc/init.d/mysql start 
Starting MySQL (Percona XtraDB Cluster)........ SUCCESS! 

雖然我們的MySQL運行著三個實例這還不是一個集群。 

配置節點

節點1的/ etc / my.cnf中 
[mysqld] 

datadir=/var/lib/mysql 
user=mysql 

# Path to Galera library 
wsrep_provider=/usr/lib64/libgalera_smm.so 

# Cluster connection URL contains the IPs of node#1, node#2 and node#3 
# node 1 192.168.0.33 
# nod3 2 192.168.0.34 
# nod3 3 192.168.0.35 
wsrep_cluster_address=gcomm://192.168.0.33,192.168.0.34,192.168.0.35 

# In order for Galera to work correctly binlog format should be ROW 
binlog_format=ROW 

# MyISAM storage engine has only experimental support 
default_storage_engine=InnoDB 

# This changes how InnoDB auto increment locks are managed and is a requirement for Galera 
innodb_autoinc_lock_mode=2 

# Node #1 address 
wsrep_node_address=192.168.0.33 

# SST method 
#wsrep_sst_method=xtrabackup 
wsrep_sst_method=rsync # 
# wsrep_sst_method=rsync_wan # 
# wsrep_sst_method=mysqldump # SLOW 

# Cluster name 
wsrep_cluster_name=percona_cluster 

# Authentication for SST method 
wsrep_sst_auth="root:<password_here>" 

# server_id 
server_id=3232235553 #SELECT INET_ATON('192.168.0.33') 

#[client] 
socket=/var/lib/mysql/mysql.sock 


啟動第一個群集節點 
/etc/init.d/mysql start --wsrep-cluster-address="gcomm://" 
Starting MySQL (Percona XtraDB Cluster)...................................... SUCCESS! 

[root@node1 mysql]# cat grastate.dat 
# GALERA saved state 
version: 2.1 
uuid: 97c457f8-f3d2-11e3-9b4e-374ebb7427e6 
seqno: -1 
cert_index: 


集群是目前唯一的一個節點。 
mysql> select @@hostname\G show global status like 'wsrep_cluster_size' \G 
*************************** 1. row *************************** 
@@hostname: node1.localdomain 
1 row in set (0.01 sec) 

*************************** 1. row *************************** 
Variable_name: wsrep_cluster_size 
Value: 1 


現在好了現在一個是啟動並運行,我們就可以開始節點2 
節點2的/ etc / my.cnf中 
[mysqld] 

datadir=/var/lib/mysql 
user=mysql 

# Path to Galera library 
wsrep_provider=/usr/lib64/libgalera_smm.so 

# Cluster connection URL contains the IPs of node#1, node#2 and node#3 
# node 1 192.168.0.33 
# nod3 2 192.168.0.34 
# nod3 3 192.168.0.35 
wsrep_cluster_address=gcomm://192.168.0.33,192.168.0.34,192.168.0.35 

# In order for Galera to work correctly binlog format should be ROW 
binlog_format=ROW 

# MyISAM storage engine has only experimental support 
default_storage_engine=InnoDB 

# This changes how InnoDB auto increment locks are managed and is a requirement for Galera 
innodb_autoinc_lock_mode=2 

# Node #1 address 
wsrep_node_address=192.168.0.34 

# SST method 
#wsrep_sst_method=xtrabackup 
wsrep_sst_method=rsync # 
# wsrep_sst_method=rsync_wan # 
# wsrep_sst_method=mysqldump # SLOW 


# Cluster name 
wsrep_cluster_name=percona_cluster 

# Authentication for SST method 
wsrep_sst_auth="root:" 

# to enable debug level logging, set this to 1 
wsrep_debug=1 

# server_id 
server_id=3232235554 # SELECT INET_ATON('192.168.0.34') 

#[client] 
socket=/var/lib/mysql/mysql.sock 

[root@node2 mysql]#/etc/init.d/mysql start 
Starting MySQL (Percona XtraDB Cluster)........................... SUCCESS! 


現在,在每個節點上比較我們的價值觀。 
mysql> select @@hostname\G show global status like 'wsrep_cluster_size' \G 
*************************** 1. row *************************** 
@@hostname: node1.localdomain 
1 row in set (0.01 sec) 

*************************** 1. row *************************** 
Variable_name: wsrep_cluster_size 
Value: 2 

mysql> select @@hostname\G show global status like 'wsrep_cluster_size' \G 
*************************** 1. row *************************** 
@@hostname: node2.localdomain 
1 row in set (0.00 sec) 

*************************** 1. row *************************** 
Variable_name: wsrep_cluster_size 
Value: 2 
1 row in set (0.18 sec) 


現在我們添加節點3到組合。 

節點3的/ etc / my.cnf中 
[mysqld] 

datadir=/var/lib/mysql 
user=mysql 

# Path to Galera library 
wsrep_provider=/usr/lib64/libgalera_smm.so 

# Cluster connection URL contains the IPs of node#1, node#2 and node#3 
# node 1 192.168.0.33 
# nod3 2 192.168.0.34 
# nod3 3 192.168.0.35 
wsrep_cluster_address=gcomm://192.168.0.33,192.168.0.34,192.168.0.35 

# In order for Galera to work correctly binlog format should be ROW 
binlog_format=ROW 

# MyISAM storage engine has only experimental support 
default_storage_engine=InnoDB 

# This changes how InnoDB auto increment locks are managed and is a requirement for Galera 
innodb_autoinc_lock_mode=2 

# Node #1 address 
wsrep_node_address=192.168.0.35 

# SST method 
# wsrep_sst_method=xtrabackup 
wsrep_sst_method=rsync # 
# wsrep_sst_method=rsync_wan # 
# wsrep_sst_method=mysqldump # SLOW 


# Cluster name 
wsrep_cluster_name=percona_cluster 

# Authentication for SST method 
wsrep_sst_auth="root:" 

# to enable debug level logging, set this to 1 
wsrep_debug=1 

# server_id 
server_id=3232235555 # SELECT INET_ATON('192.168.0.35') 

#[client] 
socket=/var/lib/mysql/mysql.sock 

[root@node3 mysql]#/etc/init.d/mysql start 
Starting MySQL (Percona XtraDB Cluster)........................... SUCCESS! 

[root@node3 mysql]# cat grastate.dat 
# GALERA saved state 
version: 2.1 
uuid: 97c457f8-f3d2-11e3-9b4e-374ebb7427e6 
seqno: -1 
cert_index: 


那麼,如何我們的所有節點的樣子了。 
mysql> select @@hostname\G show global status like 'wsrep_cluster_size' \G 
*************************** 1. row *************************** 
@@hostname: node1.localdomain 
1 row in set (0.01 sec) 

*************************** 1. row *************************** 
Variable_name: wsrep_cluster_size 
Value: 3 

mysql> select @@hostname\G show global status like 'wsrep_cluster_size' \G 
*************************** 1. row *************************** 
@@hostname: node2.localdomain 
1 row in set (0.00 sec) 

*************************** 1. row *************************** 
Variable_name: wsrep_cluster_size 
Value: 3 

mysql> select @@hostname\G show global status like 'wsrep_cluster_size' \G 
*************************** 1. row *************************** 
@@hostname: node3.localdomain 
1 row in set (0.00 sec) 

*************************** 1. row *************************** 
Variable_name: wsrep_cluster_size 
Value: 3 

測試節點 
所以,現在我們可以加載一些數據,並對其進行測試.. 
[root@node2 ~]# wget http://downloads.mysql.com/docs/world_innodb.sql.gz 
[root@node2 ~]# gzip -d world_innodb.sql.gz 
[root@node2 ~]# mysql -e "create database world" 
[root@node2 ~]# mysql world < world_innodb.sql 


所以,現在一切都被加載...它是所有在集群? 
@@hostname: node1.localdomain 
DATABASE_SCHEMA: world 
ENGINE: InnoDB 
count_tables: 3 
TOTAL_DB_GB: 0.001 

@@hostname: node2.localdomain 
DATABASE_SCHEMA: world 
ENGINE: InnoDB 
count_tables: 3 
TOTAL_DB_GB: 0.001 

@@hostname: node3.localdomain 
DATABASE_SCHEMA: world 
ENGINE: InnoDB 
count_tables: 3 
TOTAL_DB_GB: 0.001 

看起來這是工作。

2014年5月22日星期四

MySQL的幻方吉普車:套路,循環,變量

Original post: http://anothermysqldba.blogspot.com/2014/05/mysql-magic-square-4x4-routines-loop.html

我得到了今天突發奇想用魔方玩,認為這可能是一個很好的機會,讓使用MySQL例程,循環和if檢查的一個例子。 

所以,如果你不知道的幻方是什麼我已經包含了幾個環節。 它可能會為您節省了谷歌搜索,但其他認為數獨是一個例子。 
同樣,這更多的是如何使用的例程和loops和中頻檢查的一個例子,所以我限制了幻方以一個4x4的方形。 
我傳遞的值4到例程來說明如何這樣做。 這是一個不必要的步驟,因為我是硬編碼的廣場上4,但無論如何,它的工作原理的例子。 

DROP PROCEDURE IF EXISTS magic_sq; 
DROP TABLE IF EXISTS `magic_sq` ; 

delimiter // 
CREATE PROCEDURE magic_sq( N int(11)) 
BEGIN 
DECLARE nXn INT; 
DECLARE SQ_SUM INT; 
DECLARE _passfail_ INT; 
DECLARE min INT; 
DECLARE max INT; 

-- DRAW THE TEMPLATE FOR THE SQUARE MAX of 8 FOR NOW 
DROP TABLE IF EXISTS `magic_sq` ; 
CREATE TABLE `magic_sq` ( 
`xy_id` int(11) NOT NULL AUTO_INCREMENT, 
`1` int(11) NULL DEFAULT NULL, 
`2` int(11) NULL DEFAULT NULL, 
`3` int(11) NULL DEFAULT NULL, 
`4` int(11) NULL DEFAULT NULL, 
PRIMARY KEY (`xy_id`) , 
UNIQUE KEY `Y1` (`1`), 
UNIQUE KEY `Y2` (`2`), 
UNIQUE KEY `Y3` (`3`), 
UNIQUE KEY `Y4` (`4`) 
) ENGINE= MEMORY; 


-- n X n 
SET @nXn = N + N; 
-- SQ_SUM This is the formula for what the total should equal 
SET @SQ_SUM = ( N * (POW(N,2) + 1) ) / 2; 
-- MIN Value 
SET @min=1; 
-- MAX Value 
SET @max=POW(N,2); 

-- BUILD THE SQUARE 
WHILE ( @min <= @nXn ) 
DO 

-- TEST VALUES 
SET _passfail_ = IF ( (@min + (@min +1) + ( @max - 1) +@max) = @SQ_SUM ,1 , 0 ) ; 

-- IF VALID RESULTS THEN SAVE THEM 
IF _passfail_ = 1 THEN 
INSERT INTO magic_sq VALUES (NULL,@min ,(@min +1),( @max - 1) , @max );
END IF; 

-- CONTINUE 
SET @min= @min +2; 
SET @max= @max -2; 

END WHILE; 

END// 
delimiter ; 


現在,我建立了常規,我能得到有效的結果? 請記住,這是不是所有可能的選項,這可能會奏效。只是選擇924選擇一個4x4正方形存在。 

CALL magic_sq(4); 
select * from magic_sq; 
+-------+------+------+------+------+ 
| xy_id | 1 | 2 | 3 | 4 | 
+-------+------+------+------+------+ 
| 1 | 1 | 2 | 15 | 16 | 
| 2 | 3 | 4 | 13 | 14 | 
| 3 | 5 | 6 | 11 | 12 | 
| 4 | 7 | 8 | 9 | 10 | 
+-------+------+------+------+------+
 

+-------+---------+ 
| xy_id | per_row | 
+-------+---------+ 
| 1 | 34 | 
| 2 | 34 | 
| 3 | 34 | 
| 4 | 34 | 
+-------+---------+ 
不! 他們的工作為行而不是列。 雖然這顯示了如何使用程序,循環和IF語句,示例失敗,它並沒有提供我想要的東西。 因此,我不得不返工插入更多的IF檢查以及掉期幾號的結尾。 

DROP PROCEDURE IF EXISTS magic_sq; 
DROP TABLE IF EXISTS `magic_sq` ; 

delimiter // 
CREATE PROCEDURE magic_sq( N int(11)) 
BEGIN 
DECLARE nXn INT; 
DECLARE SQ_SUM INT; 
DECLARE _passfail_ INT; 
DECLARE _io_ INT; 
DECLARE min INT; 
DECLARE max INT; 

-- DRAW THE TEMPLATE FOR THE SQUARE MAX of 8 FOR NOW 
DROP TABLE IF EXISTS `magic_sq` ; 
CREATE TABLE `magic_sq` ( 
`xy_id` int(11) NOT NULL AUTO_INCREMENT, 
`1` int(11) NULL DEFAULT NULL, 
`2` int(11) NULL DEFAULT NULL, 
`3` int(11) NULL DEFAULT NULL, 
`4` int(11) NULL DEFAULT NULL, 
PRIMARY KEY (`xy_id`) , 
UNIQUE KEY `Y1` (`1`), 
UNIQUE KEY `Y2` (`2`), 
UNIQUE KEY `Y3` (`3`), 
UNIQUE KEY `Y4` (`4`) 
) ENGINE= MEMORY; 


-- n X n 
SET @nXn = N + N; 
-- SQ_SUM This is the formula for what the total should equal 
SET @SQ_SUM = ( N * (POW(N,2) + 1) ) / 2; 
-- MIN Value 
SET @min=1; 
-- MAX Value 
SET @max=POW(N,2); 

-- insert_options 
SET _io_ =0; 

-- BUILD THE SQUARE 
WHILE ( @min <= @nXn ) 
DO 

-- TEST VALUES 
SET _passfail_ = IF ( (@min + (@min +1) + ( @max - 1) +@max) = @SQ_SUM ,1 , 0 ) ; 

-- IF VALID RESULTS THEN SAVE THEM 
IF _passfail_ = 1 THEN 

IF _io_ = 0 THEN 
INSERT INTO magic_sq VALUES (NULL,@min ,(@min +1),( @max - 1) , @max );
SET _io_ =1; 
ELSEIF _io_ = 1 THEN 
INSERT INTO magic_sq VALUES (NULL,( @max - 1),@max , @min , (@min +1) ); 
SET _io_ =2; 
ELSEIF _io_ = 2 THEN 
INSERT INTO magic_sq VALUES (NULL,@max ,(@min +1) , ( @max - 1) , @min ); 
SET _io_ =4; 
ELSEIF _io_ = 4 THEN 
INSERT INTO magic_sq VALUES (NULL, (@min +1) , @max , @min ,( @max - 1) ); 
SET _io_ =0; 
END IF; 

END IF; 

-- CONTINUE 
SET @min= @min +2; 
SET @max= @max -2; 

END WHILE; 
SELECT @x3y2 := `2` FROM magic_sq WHERE xy_id = 3; 
SELECT @x3y3 := `3` FROM magic_sq WHERE xy_id = 3; 
SELECT @x4y2 := `2` FROM magic_sq WHERE xy_id = 4; 
SELECT @x4y3 := `3` FROM magic_sq WHERE xy_id = 4; 


UPDATE magic_sq SET `2` = @x4y3 , `3` = @x4y2 WHERE xy_id = 3; 
UPDATE magic_sq SET `2` = @x3y3 , `3` = @x3y2 WHERE xy_id = 4; 
select * from magic_sq; 
select SUM(`1`),SUM(`2`),SUM(`3`),SUM(`4`) from magic_sq; 
select xy_id, SUM(`1` +`2` +`3` + `4`) as per_row from magic_sq GROUP BY xy_id; 

END// 
delimiter ;


現在工作的呢? 

CALL magic_sq(4); 
+-------+------+------+------+------+ 
| xy_id | 1 | 2 | 3 | 4 | 
+-------+------+------+------+------+ 
| 1 | 1 | 2 | 15 | 16 | 
| 2 | 13 | 14 | 3 | 4 | 
| 3 | 12 | 7 | 10 | 5 | 
| 4 | 8 | 11 | 6 | 9 | 
+-------+------+------+------+------+ 
4 rows in set (0.22 sec) 

+----------+----------+----------+----------+ 
| SUM(`1`) | SUM(`2`) | SUM(`3`) | SUM(`4`) | 
+----------+----------+----------+----------+ 
| 34 | 34 | 34 | 34 | 
+----------+----------+----------+----------+ 
1 row in set (0.22 sec) 

+-------+---------+ 
| xy_id | per_row | 
+-------+---------+ 
| 1 | 34 | 
| 2 | 34 | 
| 3 | 34 | 
| 4 | 34 | 
+-------+---------+ 
好吧,我騙了一點只是走動的列,但你的想法。