程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 數據庫知識 >> MYSQL數據庫 >> MySQL綜合教程 >> mysql開發之---每日一得01

mysql開發之---每日一得01

編輯:MySQL綜合教程

mysql開發之---每日一得01


2015年7月7日-------------------------

1、truncate表會清空建表語句auto_increment的值;某個表的id即是主鍵也是自增,你可以選擇插入任意id值,如果不從1開始插入,從3開始insert,再插入沒有id的值時,自增值是4

2、查看每種引擎的索引大小,來優化數據庫參數
SELECT ENGINE,
ROUND(SUM(data_length) /1024/1024, 1) AS "Data MB",
ROUND(SUM(index_length)/1024/1024, 1) AS "Index MB",
ROUND(SUM(data_length + index_length)/1024/1024, 1) AS "Total MB",
COUNT(*) "Num Tables"
FROM INFORMATION_SCHEMA.TABLES
WHERE table_schema not in ("information_schema", "performance_schema")
GROUP BY ENGINE;

3、使用prepare stmt from准備一個動態sql語句時,主要
(1)被准備的語句定義時必須是會話級的變量不能是local變量,需要加@進行定義,准備後的語句直到會話結束才會丟失,可以使用deallocate prepare stmt消除分配的語句
表名不確定,檢查這個表最大id,從id+1開始插入10行數據
BEGIN
-- 在存儲過程中,一般的sql中values可以是變量,但是表名、字段名不能是變量
declare v_xname varchar(20) default 'testincre1';
delete from test.testincre1 where id=1;
select ifnull(max(id),0)+1 into @incre from test.testincre1;
set @end=@incre+10;
repeat
set @sql=concat('insert into test.',v_xname,' values(@incre,''yangsq'',now());');
select @sql;
prepare stmt from @sql;
execute stmt;
deallocate prepare stmt;
set @incre=@incre+1;
until @incre=@end end repeat;
END
4、sql_slave_skip_counter
Last_SQL_Error: Error 'Unknown table 'sakila.testrepldb'' on query. Default database: 'sakila'. Query: 'DROP TABLE `testrepldb` /* generated by server */'
mysql> start slave sql_thread; 報錯:會反復執行引起錯誤的sql,但是io_thread仍然正常會接受
2015-07-08 10:42:25 12378 [Warning] Slave SQL: If a crash happens this configuration does not guarantee that the relay log info will be consistent, Error_code: 0
2015-07-08 10:42:25 12378 [Note] Slave SQL thread initialized, starting replication in log 'mysql-bin.000012' at position 4449, relay log './yaolansvr_slave01-relay-bin.000014' position: 283
2015-07-08 10:42:25 12378 [ERROR] Slave SQL: Error 'Unknown table 'sakila.testrepldb'' on query. Default database: 'sakila'. Query: 'DROP TABLE `testrepldb` /* generated by server */', Error_code: 1051
2015-07-08 10:42:25 12378 [Warning] Slave: Unknown table 'sakila.testrepldb' Error_code: 1051
2015-07-08 10:42:25 12378 [ERROR] Error running query, slave SQL thread aborted. Fix the problem, and restart the slave SQL thread with "SLAVE START". We stopped at log 'mysql-bin.000012' position 4449

select @@sql_slave_skip_counter;
stop slave;--或者stop slave sql_thread
set global sql_slave_skip_counter=1;
start slave;

--log-error:
2015-07-08 10:53:30 12378 [Warning] Slave SQL: If a crash happens this configuration does not guarantee that the relay log info will be consistent, Error_code: 0
2015-07-08 10:53:30 12378 [Note] Slave SQL thread initialized, starting replication in log 'mysql-bin.000012' at position 4449, relay log './yaolansvr_slave01-relay-bin.000014' position: 283
2015-07-08 10:53:30 12378 [Note] 'SQL_SLAVE_SKIP_COUNTER=1' executed at relay_log_file='./yaolansvr_slave01-relay-bin.000014', relay_log_pos='283', master_log_name='mysql-bin.000012', master_log_pos='4449' and new position at relay_log_file='./yaolansvr_slave01-relay-bin.000014', relay_log_pos='410', master_log_name='mysql-bin.000012', master_log_pos='4576'

5、從 sqlserver 查詢mysql 報錯 從數據類型 dbtype_dbtimestamp 轉化為 datetime 時出錯
mysql某表datetime類型數據是0028-01-01 00:00:00,插入sqlserver datetime報錯,sqlserver datime支持的日期類型范圍是1753 年 1 月 1 日到 9999 年 12 月 31 日

6、replicate的相關參數比較
--replicate-do-table:沒有like pattern的功能,多個表需要指定多次
--replicate-wild-do-table:用like pattern的功能Example: --replicate-wild-do-table=foo%.bar% replicates only updates that use a table where the database name starts with foo and the table name starts with bar

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved