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

來源:互聯網
上載者:User

mysql開發之---每日一得01,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

著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.