如何修改MySQL数据库的表名、库名等信息


之前因为自己的一些失误导致某些配置文件可被直接下载(后来通过排查nginx的日志文件后发现还没有导致信息泄露,不过也给我提了个醒,以后在编辑文件的时候得注意,备份文件的方法和方式需要考虑得更全面,在新功能上线之前自己一定要好好检查之后再发布出来)。

1.MySQL修改密码

$ mysql -uroot -p

mysql> select Host,User,Password from mysql.user;
mysql> update mysql.user set Password=password(‘new_password’) WHERE User=’acer’;
mysql> FLUSH PRIVILEGES;

2.修改MySQL用户名:

mysql> use mysql;
mysql> update user set user=”新用户名” where user=”旧用户名”;
mysql> flush privileges;
mysql> exit

3.修改MySQL数据库的表名:

运行命令:
ALTER TABLE 原表名 RENAME TO 新表名;
如:
ALTER TABLE old_post RENAME TO new_post;

4.修改MySQL指定数据库名的方法:

假设源库名是 “srcDB”,目标库名是 “targetDB”
首先创建目标库:
create database targetDB;
获取所有源库的表名:
use information_schema;
select table_name from TABLES where TABLE_SCHEMA=’srcDB’;
然后按照以下命令一个个修改:
rename table srcDB.[tablename] to targetDB.[tablename];
一个个执行下来之后表就转到新的库里面了。

5.MySQL修改数据库名称的方法总结

http://stackoverflow.com/questions/67093/how-do-i-quickly-rename-a-mysql-database-change-schema-name
上面这个stackoverflow中介绍了多种实际可行的方法,超给力!

—–

Windows7下MySQL5.6忘记root密码
以下步骤如果添加了MySQL的环境变量,则可以直接运行mysql有关命令,否则必须到mysql安装目录的bin目录下操作。
步骤如下:
1.停止mysql服务。
2.[以管理员身份执行命令]使用 mysqld –skip-grant-tables 命令启动mysql数据库。
3.新打开一个CMD窗口,进行如下操作:
C:WindowsSystem32>mysql -u root
Welcome to the MySQL monitor.  Commands end with ; or g.
Your MySQL connection id is 4
Server version: 5.6.15 MySQL Community Server (GPL)
Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type ‘help;’ or ‘h’ for help. Type ‘c’ to clear the current input statement.
mysql> update mysql.user set password=password(‘new_password’) where user=’root’;
Query OK, 0 rows affected (0.00 sec)
Rows matched: 3  Changed: 0  Warnings: 0
mysql> flush privileges;
Query OK, 0 rows affected (0.05 sec)
因为执行过命令“mysqld –skip-grant-tables”,所以现在任何人都可以直接在CMD窗口中输入“mysql”命令直接登录,无需进行身份验证,如何解决?
其实都不用管的,直接在任务管理器中停掉mysqld.exe这个进程即可,如果需要每次启动MySQL的时候都不用输入密码,则可参考下面的文章进行配置(编辑my.ini文件内容即可)
参考文章:http://dev.mysql.com/doc/refman/5.0/en/resetting-permissions.html
UPDATE mysql.user SET Password=PASSWORD(‘MyNewPass’) WHERE User=’root’;
FLUSH PRIVILEGES;
—————–
下面是从 http://www.51php.com/mysql/7967.html 那里找到的批量修改MySQL数据库表前缀的PHP代码(这对于有大量表前缀需要修改的数据库来说是个很大的帮助!):
<?php
$dbserver='localhost';//连接的服务器一般为localhost
$dbname='your_DBname';//数据库名
$dbuser='UserName';//数据库用户名
$dbpassword='UserPass';//数据库密码
$old_prefix='ecs_';//数据库的前缀
$new_prefix='new__';//数据库的前缀修改为
if ( !is_string($dbname) || !is_string($old_prefix)|| !is_string($new_prefix) ){
	return false;
}
if (!mysql_connect($dbserver, $dbuser, $dbpassword)) {
	print 'Could not connect to mysql';
	exit;
}
//取得数据库内所有的表名
$result = mysql_list_tables($dbname);	// This extension is deprecated as of PHP 5.5.0, and will be removed in the future.
if (!$result) {
	print "DB Error, could not list tablesn";
	print 'MySQL Error: ' . mysql_error();
	exit;
}
//把表名存进$data数组
while ($row = mysql_fetch_row($result)) {
	$data[] = $row[0];
}
//过滤要修改前缀的表名
foreach($data as $k => $v) {
	$preg = preg_match("/^($old_prefix{1})([a-zA-Z0-9_-]+)/i", $v, $v1);
	if($preg){
		$tab_name[$k] = $v1[2];
		//$tab_name[$k] = str_replace($old_prefix, '', $v);
	}
}
if($preg) {
	// echo '<pre>';
	// print_r($tab_name);
	// exit();
	// 批量重命名
	foreach($tab_name as $k => $v){
		$sql = 'RENAME TABLE `'.$old_prefix.$v.'` TO `'.$new_prefix.$v.'`';
		mysql_query($sql);
	}
	print "数据表前缀:".$old_prefix."<br>已经修改为:".$new_prefix."<br>";
} else { print "您的数据库表的前缀 ".$old_prefix." 输入错误。请检查相关的数据库表的前缀";
	if ( mysql_free_result($result) ) {
		return true;
	}
}
?>

不过需要注意的一点就是:mysql_list_tables()这个扩展在PHP5.5以后的版本中已经被弃用了,所以这个脚本不支持PHP5.5及以后的版本,这时你可以通过之前给出的stackoverflow的那个链接找到适合你自己的方法。

,

《 “如何修改MySQL数据库的表名、库名等信息” 》 有 5 条评论

  1. MySQL中varchar最大长度是多少?
    http://dinglin.iteye.com/blog/914276
    http://stackoverflow.com/questions/13506832/what-is-the-mysql-varchar-max-size
    http://stackoverflow.com/questions/332798/equivalent-of-varcharmax-in-mysql

    varchar 字段是将实际内容单独存储在聚簇索引之外,内容开头用1到2个字节表示实际长度(长度超过255时需要2个字节),因此最大长度不能超过65535。

    字符类型若为gbk,每个字符最多占2个字节,最大长度不能超过32766;
    字符类型若为utf8,每个字符最多占3个字节,最大长度不能超过21845。
    若定义的时候超过上述限制,则varchar字段会被强行转为text类型,并产生warning。

  2. 新手MySQL工程师必备命令速查手册
    https://mp.weixin.qq.com/s/87BoE2-0mW_3qALyNSpiTw
    `
    1、MySQL常用语句
    · 表(或者数据库)的CRUD
    · 表数据的CRUD,其中表数据查询使用最多,也更复杂。查询可以按照单表还是多表可以分为:单表SELECT查询和多表的联结查询(INNER JOIN, LEFT JOIN, RIGHT JOIN和FULL JOIN)以及组合查询UNION和UNION ALL
    · SQL语句中各个关键字的执行顺序

    2、MySQL高级功能
    · 存储过程
    · 事务处理
    · 触发器
    `

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注