mysql常用解决方案
1.删除了mysql数据库user表记录,会导致用户不能正常登录。
此时将mysqld 以skip-grant-tables启动,忽略权限。
进入到mysql库中,执行以下脚本。
就可以用root/bill登录了
/*
Navicat MySQL Data Transfer
Source Server : localhost
Source Server Version : 50703
Source Host : localhost:3306
Source Database : mysql
Target Server Type : MYSQL
Target Server Version : 50703
File Encoding : 65001
Date: 2015-06-13 20:07:50
*/
SET FOREIGN_KEY_CHECKS=0;
-- ----------------------------
-- Table structure for `user`
-- ----------------------------
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
`Host` char(60) COLLATE utf8_bin NOT NULL DEFAULT '',
`User` char(16) COLLATE utf8_bin NOT NULL DEFAULT '',
`Password` char(41) CHARACTER SET latin1 COLLATE latin1_bin NOT NULL DEFAULT '',
`Select_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
`Insert_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
`Update_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
`Delete_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
`Create_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
`Drop_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
`Reload_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
`Shutdown_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
`Process_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
`File_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
`Grant_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
`References_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
`Index_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
`Alter_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
`Show_db_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
`Super_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
`Create_tmp_table_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
`Lock_tables_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
`Execute_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
`Repl_slave_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
`Repl_client_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
`Create_view_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
`Show_view_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
`Create_routine_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
`Alter_routine_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
`Create_user_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
`Event_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
`Trigger_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
`Create_tablespace_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
`ssl_type` enum('','ANY','X509','SPECIFIED') CHARACTER SET utf8 NOT NULL DEFAULT '',
`ssl_cipher` blob NOT NULL,
`x509_issuer` blob NOT NULL,
`x509_subject` blob NOT NULL,
`max_questions` int(11) unsigned NOT NULL DEFAULT '0',
`max_updates` int(11) unsigned NOT NULL DEFAULT '0',
`max_connections` int(11) unsigned NOT NULL DEFAULT '0',
`max_user_connections` int(11) unsigned NOT NULL DEFAULT '0',
`plugin` char(64) COLLATE utf8_bin NOT NULL DEFAULT 'mysql_native_password',
`authentication_string` text COLLATE utf8_bin,
`password_expired` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
PRIMARY KEY (`Host`,`User`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_bin COMMENT='Users and global privileges';
-- ----------------------------
-- Records of user
-- ----------------------------
INSERT INTO `user` VALUES ('%', 'root', '*29A1BB43D3B9EB42028B4566E4836353285B9395', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', '', '', '', '', '0', '0', '0', '0', 'mysql_native_password', '', 'N');
INSERT INTO `user` VALUES ('127.0.0.1', 'root', '*29A1BB43D3B9EB42028B4566E4836353285B9395', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', '', '', '', '', '0', '0', '0', '0', 'mysql_native_password', '', 'N');
INSERT INTO `user` VALUES ('::1', 'root', '*29A1BB43D3B9EB42028B4566E4836353285B9395', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', '', '', '', '', '0', '0', '0', '0', 'mysql_native_password', '', 'N');
INSERT INTO `user` VALUES ('localhost', '', '', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', '', '', '', '', '0', '0', '0', '0', 'mysql_native_password', null, 'N');
2.授权
#(执行下面的语句 *.*:所有库下的所有表 %:任何IP地址或主机都可以连接)
GRANT ALL PRIVILEGES ON *.* TO ‘root’@’%’ IDENTIFIED BY ‘123’ WITH GRANT OPTION;
FLUSH PRIVILEGES;
mysql导入导出包括函数或者存储过程
1.mysql导出整个数据库
mysqldump -h hostname -u username -p databasename > backupfile.sql
如果root用户没用密码可以不写-p,当然导出的sql文件你可以制定一个路径,未指定则存放在mysql的bin目录下 www.2cto.com
2.mysql导出数据库一个表
mysqldump -hhostname -uusername -ppassword database tablename> 导出的文件名
mysqldump -hlocalhost -uroot hqgr t_ug_user> user.sql
3.mysql导出一个数据库结构
mysqldump -hhostname -uusername -ppassword -d –add-drop-table databasename>d:hqgrstructure.sql
-d 没有数据 –add-drop-table 在每个create语句之前增加一个drop table
4.如果需要导出mysql里面的函数或者存储过程
mysqldump -h hostname -u username -ntd -R databasename > backupflie.sql (包括存过过程和函数一次性完整导出)
其中的 -ntd 是表示导出表结构和数据;-R是表示导出函数、存储过程
可以参照mysqldump –help
mysql常用导入数据的命令:
mysql数据库导入导出:
mysqldump -u 用户名 -p 数据库名 > 数据库名.sql
如:
mysqldump -u root -p testdb > testdb.sql (不包括存储过程和函数)
mysqldump -u root -p -R testdb > testdb.sql (**包括存储过程和函数**)
MySQL source命令向数据库中导入数据:
mysql>use testdb;
mysql>set names utf8;
mysql>source /tmp/bdc.sql;
奇怪错误处理:
下面是导出存储过程的代码
1 # mysqldump -u 数据库用户名 -p -n -t -d -R 数据库名 > 文件名
其中,-d 表示–no-create-db, -n表示–no-data, -t表示–no-create-info, -R表示导出function和procedure。所以上述代码表示仅仅导出函数和存储过程,不导出表结构和数据。但是,这样导出的内容里,包含了 trigger。再往mysql中导入时就会出问题,错误如下:
ERROR 1235 (42000) at line **: This version of MySQL doesn’t yet support ‘multiple triggers with the same action time and event for one table’
所以在导出时需要把trigger关闭。代码为
1 # mysqldump -u 数据库用户名 -p -n -t -d -R –triggers=false 数据库名 > 文件名
这样导入时,会出现新的问题:
ErrorCode:1418
This function has none of DETERMINISTIC, NOSQL, or READS SQL DATA inits declaration and binary logging is enabled (you *might* want to use the less safe log_bin_trust_function_creators variable)
解决方法是,在/etc/my.cnf中找到[mysqld],在它下面添加这样一行:
1 log-bin-trust-function-creators=1
创建数据库:CREATE DATABASE `total_admin` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;
1.查看mysql大小
use 数据库名 SELECT sum(DATA_LENGTH)+sum(INDEX_LENGTH) FROM information_schema.TABLES where TABLE_SCHEMA=’数据库名’;
得到的结果是以字节为单位,除1024为K,除1048576为M。
2.查看表的最后mysql修改时间select TABLE_NAME,UPDATE_TIME from INFORMATION_SCHEMA.tables where TABLE_SCHEMA=’数据库名’;
安装好mysql的提示:
To start mysqld at boot time you have to copy
support-files/mysql.server to the right place for your system
PLEASE REMEMBER TO SET A PASSWORD FOR THE MySQL root USER !
To do so, start the server, then issue the following commands:
/usr/bin/mysqladmin -u root password 'new-password'
/usr/bin/mysqladmin -u root -h v10 password 'new-password'
Alternatively you can run:
/usr/bin/mysql_secure_installation
which will also give you the option of removing the test
databases and anonymous user created by default. This is
strongly recommended for production servers.
See the manual for more instructions.
You can start the MySQL daemon with:
cd /usr ; /usr/bin/mysqld_safe &
You can test the MySQL daemon with mysql-test-run.pl
cd /usr/mysql-test ; perl mysql-test-run.pl
Please report any problems with the /usr/bin/mysqlbug script!