导出建库表索引等:
[root@lnpg ~]# mysqldump -ugistar -pxxx -h192.168.207.143 --no-data -d lndb > lndb20230223-1.sql
只导出索引:参考:MYSQL导出现有库中的索引脚本_mysql 导出数据库所有表的主键和索引-CSDN博客
-- MYSQL导出现有库中的索引脚本
SELECT CONCAT( 'ALTER TABLE `', TABLE_NAME, '` ', 'ADD ', IF ( NON_UNIQUE = 1, CASE UPPER( INDEX_TYPE ) WHEN 'FULLTEXT' THEN 'FULLTEXT INDEX' WHEN 'SPATIAL' THEN 'SPATIAL INDEX' ELSE CONCAT( 'INDEX `', INDEX_NAME, '` USING ', INDEX_TYPE ) END, IF ( UPPER( INDEX_NAME ) = 'PRIMARY', CONCAT( 'PRIMARY KEY USING ', INDEX_TYPE ), CONCAT( 'UNIQUE INDEX `', INDEX_NAME, '` USING ', INDEX_TYPE ) ) ), '(', GROUP_CONCAT( DISTINCT CONCAT ( '`', COLUMN_NAME, '`' ) ORDER BY SEQ_IN_INDEX ASC SEPARATOR ', ' ), ');'
) AS 'Show_Add_Indexes'
FROM information_schema.STATISTICS
WHERE -- 可根据实际情况调整为自己的数据库名称TABLE_SCHEMA = 'lndb'
GROUP BY INDEX_TYPE,NON_UNIQUE,TABLE_NAME, INDEX_NAME
ORDER BY TABLE_NAME ASC, INDEX_NAME ASC
删除索引和主键:
-- MYSQL删除现有库中的索引脚本
SELECT CONCAT( 'ALTER TABLE `', TABLE_NAME, '` ', 'DROP ', IF ( NON_UNIQUE = 1, CASE UPPER( INDEX_TYPE ) WHEN 'FULLTEXT' THEN 'FULLTEXT INDEX' WHEN 'SPATIAL' THEN 'SPATIAL INDEX' ELSE CONCAT( 'INDEX `', INDEX_NAME, '`' ) END, IF ( UPPER( INDEX_NAME ) = 'PRIMARY', CONCAT( 'PRIMARY KEY ' ), CONCAT( 'INDEX `', INDEX_NAME, '`' ) ) ), ';'
) AS 'Show_Drop_Indexes'
FROM information_schema.STATISTICS
WHERE -- 可根据实际情况调整为自己的数据库名称TABLE_SCHEMA = 'resdb'
GROUP BY INDEX_TYPE,NON_UNIQUE,TABLE_NAME, INDEX_NAME
ORDER BY TABLE_NAME ASC, INDEX_NAME ASC