3.--登录mysql: my3306.sh mysql -h127.0.0.1 -uroot -P3306 -p
------研究下 cd /root/data/mysql3306/data/mysql cd /root/data/mysql3306/data/performance_schema --性能相关的 cd /root/data/mysql3306/data/test ---测试库 cd /root/log/mysql3306/iblog/ ---innodb自己的数据和日志
------了解my.cnf(多实例用端口来取分) vi /root/data/mysql3306/my.cnf
1、登录mysql: 本地: mysql -u$usrename -p$password 远程: mysql -u$username -p$passwrod -h$ip 多实例:mysql -u$username -p$passwrod -P$port
2、用户操用 (1)创建用户 方法一: insert into mysql.user(user,host,password) values(‘mytest‘,‘localhost‘,password(‘1234‘)); flush privilege; 方法二:create user mystest@‘%‘ identified by ‘1234‘;
(2)用户授权 单纯的授权 grant all privileges on *.* to mytest@localhost; grant insert,update,delete,select on *.* to mytest@localhost; 授权并创建用户 grant all privileges on *.* to mytest@localhost identified by ‘1234‘;--创建用户并刷缓存, (等同于:insert into mysql.user ,flush privilege) grant all privileges on *.* to mytest@localhost; --对象权限 grant super on *.* to mytest@‘%‘; --系统权限 (supert相当于oracle中的dba权限)
3、实操 show databases; --查看所有的数据库 use mysql; --切到mysql数据库 use tables; --在mysql库的tables select user,host,password from mysql.user; ----查mysql的所有用户,这个是由mysql_install_db创建的 grant all privilege on *.* to test_1@‘%‘; --all代表(select update,delete,alter admin,super_acl),第一个*用户,第二个*对象,%所有的主机 mysql -h127.0.0.1 -utest_1 ----用grant创建的用户登录mysql select user(); ---当前是什么用户 create database jianfeng; ---创建数据库(mysql中的数据库类似于oracle中的schema create table user(id int) engine=innodb ---创建表; grant select on jianfeng.user to test_1@‘%‘; ---jianfeng.user表的查询授权给test_1用户 insert into mysql.user(user,host,password) values(‘test_2‘,‘%‘,password(‘1234‘)); --用这种方法创建test_2用户,有个问题权限没有 flush privileges; ---把mysql.user表的用户权限重新刷到内存中 show master status\G; change master to xxx; show processlist; ---查看当前用户的连接,线程形式(类似oracle中的v$session)
4、drop table处理 rename table test_1 to test;(可以快速切回来rename table test to test_1;) 备份mysqldump:mysqldump -h127.0.0.1 -uroot mydb gyj_t1 >/tmp/gyj_t1.sql drop table test;
5、自增主键(最好是自己定义主键,系统默认的是全局的增量) create table test (id int primary key auto_increment,name varchar(100)) engine=innodb; show create table test\G; create index test_name_idx on test(name); show create table test\G; insert into test(name) values(‘test‘); commit; select * from test;
6、alter table处理 --会动原来的数据,需要拷贝数据 alter table test add coll int;
7、执行计划 select * from test where id=1\G; explain select * from test where id=1; create index test_id_coll_idx on test(id,coll); explain select * from test where id=1; create index test_col_name on test(coll,name); explain select * from test where coll>10 and name=‘xx‘; show create table test\G; alter table test drop index test_name_idx; explain select * from test where coll>10 and name>‘xx‘;
(2)用select导出数据 select * from test into outfile ‘/tmp/yy.sql‘;
9、数据迁移 (1)停机方式 mysqldump/loadata (2)不停机方式 物理上:搭备库(可以级联5.5-->5.6,向下兼容的) 把主库read only,备库就能把主库转过来的binlog消化完,再把备库切为主 show variables like ‘%read%‘; set global read_only=on; insert into test(name) values(‘xx‘); --插不进的,不能用root用户 (3)不同平台小表:oracle--->mysql 脚本:synfull.pl
12、参数和统计信息 show variables; ----参数 show variables like ‘%bin%‘; show status; ----统计信息 show global status like‘%insert%‘; insert into test(name) values(‘xxxxx‘);
show variables like ‘%default%‘; set global default_storage_engine=myisam; ---不影响当前会话的操作,影响新建立的连接 set session default_storage_engine=myisam; ---影响当前会话的操作