mysql 常用操作
1、登陆mysql查看表信息时,为了防止中文出现乱码
修改下面脚本:
#vi /home/coremail/bin/mysql_cm
#!/bin/sh
/home/coremail/mysql/bin/mysql -ucoremail -p3376211849 -h127.0.0.1 -P3308 cmxt --default-character-set=gbk
mysql> show variables like '%char%';
+--------------------------+--------------------------------------------+
| Variable_name | Value |
+--------------------------+--------------------------------------------+
| character_set_client | gbk |
| character_set_connection | gbk |
| character_set_database | latin1 |
| character_set_results | gbk |
| character_set_server | latin1 |
| character_set_system | utf8 |
| character_sets_dir | /home/coremail/mysql/share/mysql/charsets/ |
+--------------------------+--------------------------------------------+
给root用户设置密码
#mysqladmin -u root password '123456'
修改root用户密码
#mysqladmin -uroot -p123456 password ' secret '
忘记root用户名密码,解决方法:
#service mysql stop
# /usr/bin/mysqld_safe --skip-grant-tables &
#mysql
mysql> use mysql
mysql> UPDATE user SET password=password('123456') WHERE user='root';
查看数据编码方式:
status;
导入数据
source a.sql;
2、mysql 数据的迁移
数据的导出:
mysqldump -n --default-character-set=utf8 -ucoremail -p密码 cm30 > cm30.sql
mysqldump -n --default-character-set=utf8 -ucoremail -p密码 cm_fng_db > cm_fng_db.sql
mysqldump -n --default-character-set=utf8 -uroot -pmailc0re groupsend > groupsend20110426.sql
由于日志数据表比较大,所以只导出表结构
mysqldump -ucoremail -pcnd2033404 --default-character-set=gbk --opt coremail ax_action ax_cf_category ax_content_filter ax_policy ax_sender_filter cm_mbox cm_timedsend cm_user_bin_data cm_user_info cm_user_pref cm_user_pref_ext honeypot sms_mid_map tc_event tc_task td_alias_seed td_allowed_domain td_cos td_cos_dd td_cos_detail td_domain td_domain_alias td_op_log td_org_cos td_org_unit td_organization td_provider td_provider_cos td_site_admin td_smtp_alias td_system_admin td_user td_user_service > /localdata/coremail.sql
mysqldump -ucoremail -pcnd2033404 --default-character-set=gbk --opt -d coremail ax_log_convert ax_log_domain_stat ax_log_filter_stat ax_log_mail ax_log_mail_smtp_dn ax_log_mailto ax_log_mailto_ext ax_log_reject_ip_stat ax_log_spam_stat ax_log_virus tl_imap tl_pop tl_server_info tl_sys_access_stat tl_sys_mailinfo_stat tl_sys_recv_stat tl_sys_sent_stat tl_user_access_stat tl_user_lastmailinfo tl_user_mailinfo_stat tl_user_recv_stat tl_user_sent_stat tl_wap tl_web > /localdata/coremaillog.sql
数据的导入:
◦drop 掉无用的数据库
◦create database cmxt2 以及 create database cm_fng_db
◦使用一下命令导入 mysql 数据
mysql --default-character-set=utf8 cmxt2 < cm30.sql
mysql --default-character-set=utf8 cm_fng_db < cm_fng_db.sql
脚本的执行方法:
/home/coremail/mysql/bin/mysql -ucoremail -p2641647999 -h127.0.0.1 -P3308 --default-character-set=gbk coremail < cm307_upgrade_XT201_cmxt.sql
/home/coremail/mysql/bin/mysql -ucoremail -p2641647999 -h127.0.0.1 -P3308 --default-character-set=gbk coremail < cm307_upgrade_XT201_cmxt_log.sql
mysql4.1创建用户:
grant all privileges on *.* to coremail@localhost identified by 'mail@Coremail123456';
mysql5.1创建用户:
mysql> create user test identified by "test";
Query OK, 0 rows affected (0.01 sec)
yy表示你要建立的用户名,后面的123表示密码
上面建立的用户可以在任何地方登陆。
如果要限制在固定地址登陆,比如localhost 登陆:
CREATE USER yy@localhost IDENTIFIED BY '123';
用户授权问题:
grant all privileges on *.* to coremail@172.19.0.128 identified by '3477578970';
执行mysql 脚本
在命令行下(未连接数据库),输入
mysql -h localhost -u root -p123456 < F:\hello world\niuzi.sql (注意路径不用加引号的!!) 回车即可.
连接的情况下:
sql> source 文件路径
查看mysql 当前运行的进程
mysql> show processlist;
mysql发生表损坏问题
在执行mysql备份的时候
mysqldump -pwujiangsql -uroot -h10.1.1.4 --single-transaction --databases coremail >/tmp/mysql.bak`date +%F`.sql
导出单个数据表结构和数据
mysqldump -h localhost -uroot -p123456 database table > dump.sql
导出整个数据库结构(不包含数据)
mysqldump -h localhost -uroot -p123456 -d database > dump.sql
导出单个数据表结构(不包含数据)
mysqldump -h localhost -uroot -p123456 -d database table > dump.sql
导出数据,不导出表结构:
/home/coremail/mysql/bin/mysqldump -uroot -p1494002174 -h127.0.0.1 -P3308 -t cmxt td_user>/tmp/td_user.sql
提示 ‘表名’ mysql marked as crashed and should be repaired
修复表:
check table tl_pop; #检查表是否有损坏
mysql>repair table ax_log_mail_smtp_dn
停下mysql数据库,到数据库表存放的目录
myisamchk -r 表名
修改字段的默认值:
alter table cm_user_pref alter edit_mode set default '1';
数据库mysql 优化连接数
show variables like '%max_connections%';
+-----------------+-------+
| Variable_name | Value |
+-----------------+-------+
| max_connections | 400 |
+-----------------+-------+
1 row in set (0.00 sec)
配置文件加max_connections=400,一般压力比较高的客户都要加大些,要不然非常容易出现这种问题
查询smtp 某段时间发信数量
select distinct user_id,count(noauth) from tl_smtp where access_time between "20110511" and "20110512" group by user_id;
MySQL 5.5 doesn't support "type" table option, instead it uses ENGINE option.
Below shell script to change schema/mysql.sql to newer format.
Code:
sed 's/type=InnoDB/ENGINE=InnoDB/g' schema/mysql.sql > /tmp/mysql.sql
rm schema/mysql.sql
cp /tmp/mysql.sql schema/mysql.sql
发生数据文件损坏的问题:自动修复数据文件。
Got error 28 from storage engine
my.cnf
user = coremail
default-storage-engine=MyISAM
#innodb_force_recovery=1 #增加配置
阅读(4348) | 评论(0) | 转发(0) |