1、优雅关闭数据库的方法
(1)mysqladmin方法
mysqladmin -uroot -poldboy123 shutdown
(2)自带的脚本
/etc/init.d/mysqld stop
2、mysql登录
mysql -uroot -poldboy123 #密码历史记录需要删除
cat /root/.mysql_history
mysql -uroot -p -S /data/3306/mysql.sock #多实例登录
3、设置密码方法
mysqladmin -uroot password 'oldboy123'
#修改密码
mysqladmin -uroot -poldboy123 password '123';
#登录数据库后更改
update mysql.user set password=password("456") where user='root' and host='localhost';
flush privileges
4、SQL分类
5、创建字符集数据库
mysql> create database oldboy_utf8 DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;
Query OK, 1 row affected (0.00 sec)
mysql> show create database oldboy_utf8\G
*************************** 1. row ***************************
Database: oldboy_utf8
Create Database: CREATE DATABASE `oldboy_utf8` /*!40100 DEFAULT CHARACTER SET utf8 */
1 row in set (0.00 sec)
6、授权
select user,host from mysql.user;
create user oldboy@localhost identified by '123';
flush privileges;
help grant
GRANT ALL ON oldboy_uft8.* TO 'test'@'localhost';
show grants for 'test'@'localhost'; #查看用户权限
mysql> REVOKE INSERT ON oldboy_uft8.* FROM 'test'@'localhost';
Query OK, 0 rows affected (0.00 sec)
mysql> show grants for 'test'@'localhost';
+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Grants for test@localhost |
+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| GRANT USAGE ON *.* TO 'test'@'localhost' IDENTIFIED BY PASSWORD '*94BDCEBE19083CE2A1F959FD02F964C7AF4CFC29' |
| GRANT SELECT, UPDATE, DELETE, CREATE, DROP, REFERENCES, INDEX, ALTER, CREATE TEMPORARY TABLES, LOCK TABLES, EXECUTE, CREATE VIEW, SHOW VIEW, CREATE ROUTINE, ALTER ROUTINE, EVENT, TRIGGER ON `oldboy_uft8`.* TO 'test'@'localhost' |
+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
2 rows in set (0.00 sec)
mysql> use mysql;
Database changed
mysql> show tables;
+---------------------------+
| Tables_in_mysql |
+---------------------------+
| columns_priv |
| db |
| event |
| func |
| general_log |
| help_category |
| help_keyword |
| help_relation |
| help_topic |
| host |
| ndb_binlog_index |
| plugin |
| proc |
| procs_priv |
| proxies_priv |
| servers |
| slow_log |
| tables_priv |
| time_zone |
| time_zone_leap_second |
| time_zone_name |
| time_zone_transition |
| time_zone_transition_type |
| user |
+---------------------------+
24 rows in set (0.00 sec)
7、索引及主键
alter table student change id id int primary key auto_increment;
alter table student add index index_dept(dept(8));
create index inde_name_dept on student(name,dept);
alter table student drop index index_dept;
create unique index index_name on student(name);
8、备份
mysqldump -uoldboy -poldboy123 -B oldboy >/opt/oldboy_bak.sql
grep -E -v "#|\/|^$|--" /opt/oldboy_bak.sql
USE `oldboy`;
DROP TABLE IF EXISTS `student`;
CREATE TABLE `student` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` char(20) NOT NULL,
`age` tinyint(2) NOT NULL DEFAULT '0',
`dept` varchar(16) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `inde_name_dept` (`name`,`dept`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
LOCK TABLES `student` WRITE;
UNLOCK TABLES;
DROP TABLE IF EXISTS `test`;
CREATE TABLE `test` (
`id` int(4) NOT NULL AUTO_INCREMENT,
`name` char(20) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=latin1;
LOCK TABLES `test` WRITE;
INSERT INTO `test` VALUES (1,'oldboy'),(2,'oldgirl'),(3,'test');
UNLOCK TABLES;
9、使用explain查看执行计划
mysql> explain select * from test where name ='test'\G;
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: test
type: ALL
possible_keys: NULL
key: NULL
key_len: NULL
ref: NULL
rows: 3
Extra: Using where
1 row in set (0.00 sec)
create index index_name on test(name);
mysql> explain select * from test where name ='test'\G;
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: test
type: ref
possible_keys: index_name
key: index_name
key_len: 20
ref: const
rows: 1
Extra: Using where; Using index
1 row in set (0.00 sec)
10、中文乱码问题
#先执行字符集设置,再插入就不会中文乱码,临时的
set names latin1;
#在sql文件中指定set names latin1,然后登录mysql,执行
source test.sql
#在sql文件中指定set names latin1,然后通过mysql命令导入数据。
#通过指定mysql命令的字符集参数实现--default-characater-set=latin1
#在客户端指定,服务端再指定,目的就是字符集统一一致。
11、字符集
mysql -uroot -p123 -e 'show character set';
show variables like 'character_set%';
[root@jackroo mysql-5.5.32]# vi /etc/sysconfig/i18n
#LANG="en_US.UTF-8"
LANG="zh_CN.GB2312"
LANG="zh_CN.UTF8"
#LANG="en"
SYSFONT="latarcyrheb-sun16"
. /etc/sysconfig/i18n
[root@jackroo mysql-5.5.32]# echo $LANG
zh_CN.UTF8
mysql> show variables like 'character_set%';
+--------------------------+-------------------------------------------+
| Variable_name | Value |
+--------------------------+-------------------------------------------+
| character_set_client | utf8 |
| character_set_connection | utf8 |
| character_set_database | latin1 |
| character_set_filesystem | binary |
| character_set_results | utf8 |
| character_set_server | latin1 |
| character_set_system | utf8 |
| character_sets_dir | /application/mysql-5.5.32/share/charsets/ |
+--------------------------+-------------------------------------------+
8 rows in set (0.00 sec)
#***
vi /etc/my.cnf
[client]
#password = your_password
default-character-set=utf8
port = 3306
socket = /application/mysql-5.5.32/tmp/mysql.sock
mysql> show variables like 'character_set%';
+--------------------------+-------------------------------------------+
| Variable_name | Value |
+--------------------------+-------------------------------------------+
| character_set_client | utf8 |
| character_set_connection | utf8 |
| character_set_database | latin1 |
| character_set_filesystem | binary |
| character_set_results | utf8 |
| character_set_server | latin1 |
| character_set_system | utf8 |
| character_sets_dir | /application/mysql-5.5.32/share/charsets/ |
+--------------------------+-------------------------------------------+
8 rows in set (0.00 sec)
[mysqld]
character-set-server=utf8 #新增
port = 3306
socket = /application/mysql-5.5.32/tmp/mysql.sock
#服务端修改需要重启
[root@jackroo mysql-5.5.32]# /etc/init.d/mysqld restart
Shutting down MySQL.. SUCCESS!
Starting MySQL.. SUCCESS!
mysql> show variables like 'character_set%';
+--------------------------+-------------------------------------------+
| Variable_name | Value |
+--------------------------+-------------------------------------------+
| character_set_client | utf8 |
| character_set_connection | utf8 |
| character_set_database | utf8 |
| character_set_filesystem | binary |
| character_set_results | utf8 |
| character_set_server | utf8 |
| character_set_system | utf8 |
| character_sets_dir | /application/mysql-5.5.32/share/charsets/ |
+--------------------------+-------------------------------------------+
12、如何更改生产数据库?
修改步骤:
对于已有的数据库想修改字符集不能直接通过"alter database character set * "或 “alter table tablenmae character set *”,这两个命令都没有更新已有记录的字符集,而只是对新建的表或者记录生效。
已经有的数据的调整,必须先将数据导出,经过修改字符集后重新导入后才可完成。
1)导出表结构
mysqldump -uroot -p --default-character-set=latin1 -d dbname altertable.dql
#–default-character-set=gbk表示以GBK字符集进行连接,-d只导表结构
2)编辑altertable.sql将latin1改成GBK
3)确保数据库不再更新,导出所有的数据
mysqldubmp -uroot -p --quick --no-create-info --extended-insert --default-character-set=latin1 dbname>altdata.sql
4)打开altdata.sql将set names latin1 修改成set names gbk;
5)建库
create database dbname default charset gbk;
6)创建表,执行alttable.sql 7)导入数据
mysql -uroot -p dbname
mysql -uroot -p dbname