天天看點

mysql配置

# The MySQL server

[mysqld]

port            = 3306

socket          = /tmp/mysql.sock

#skip-locking                   #是否過濾掉lock

key_buffer_size = 256M           #(關鍵選項) 記憶體配置,索引快緩沖區,不是越大越好

max_allowed_packet =1M        #導入檔案(備份,還原)

table_open_cache = 256        #打開表的數量

sort_buffer_size = 1M            #排序緩沖區大小(1個線程占用),根據數字量大小而定

read_buffer_size = 1M            #讀時候的緩沖區

read_rnd_buffer_size =4M         #随即讀

myisam_sort_buffer_size= 64M      #針對引擎

thread_cache_size = 8              #緩沖重用的限制,跟CPU核數有關

query_cache_size= 16M             #暫存記憶體,存放查詢結果

# Try number ofCPU's*2 for thread_concurrency

thread_concurrency = 8                #跟CPU核數有關,最大并發線程數,實體CPUx2

interactive_timeout =8              #這是時間,如果空閑的連接配接超過8s 就會斷開

wait_timeout=8                   # 與interactive_timeout有關,已完成的連接配接數與指定時間有關

long_query_time=1                               #慢查詢,超過1秒記錄

log_slow_queries=/data/mysql/slow.log          #慢查詢日志

log-bin=mysql-bin                    #二進制日志,主從複制

server-id      = 1                     #主從複制

mysql裡的參數:

show  variables;   #檢視mysql裡的參數

%    #萬能比對,和shell的*類似

SHOW STATUS LIKE ‘key_read%’;   #檢視比值,看看配置得是否合适

Mysql重置密碼

設定root密碼:mysqlamin  -uroot password  ‘自定義的密碼’ 

忘記密碼在/etc/my.cnf裡[mysqld的底下加入skip-grant(不用密碼),儲存後退出重新開機mysql,在通過mysql  -uroot進入

use mysql;

update user set   password=password(‘新密碼’) where user=’root’;

select * from  user  where  user=’root’\G;     #檢視root的資訊

重置完密碼後注釋或删除skip-grant,再重新開機mysql服務

普通使用者改密碼:

update user set password=password('新密碼') where user='使用者名';

mysql遠端連接配接:

mysql -uroot  -h遠端ip -P3306  -p密碼    #用戶端連接配接遠端伺服器

ERROR 1130 (HY000):Host ‘遠端ip’ is not allowed to connect to this MySQLserver

telnet  遠端ip  3306

提示連通了,但Host ‘遠端ip’ is not allowed to connect to this MySQL  serverConnection closed by foreign host.

但可以遠端連接配接127.0.0.1:mysql  -uroot  -h127.0.0.1 -P3306  -p密碼

授權:grant  all on  *.* to ‘root’@’用戶端ip’identified by ‘用戶端連接配接的密碼’;     #登陸到遠端伺服器,授權用戶端Ip登陸

取消授權:revoke all on *.* from 'root'@'192.168.1.12' identified by '123';

      或:revoke all on *.* from 'root'@'192.168.1.12';

或mysql>delete from mysql.user where host='192.168.0.168' and user='root';

flush privileges

select user,host from mysql.user;  show grants for ‘user'@'host';這兩句話看權限就行了

Select  *from user where host=’用戶端ip’\G;    #顯示整齊

all   #所有權限

*.*   #第一個*是庫;第二個*是表

\G   #将表的内容整齊的顯示

登入到遠端mysql,用selectuser();指令來檢視目前登陸的使用者

本地有多個mysql:

mysql  -uroot  -S /tmp/mysql.sock  –p密碼       #使用sock來登陸

操作:(庫------表-----行-----字段)

use 資料庫;

create  table tb1 (`id` int(4), `name` char(40)) ENGINE=MyIASM DEFAULT CHARSET=gbk;  #建立表和表的選項,引擎為MyISAM, 預設字元集為gbk

show databases;     #檢視有哪些資料庫

create  database  aaa; #建立庫

在庫插入到表裡的資料: insertinto tb1 values(1,'aaa');    #由于(`id` int(4), `name` char(40))兩個字段

繼續插入到表裡的資料: insertinto tb1 values(2,'linux');

mysql> insert into tb1 (`id`) values(2);         #往表裡單獨插入一個資料,id為2,name值為NULL

mysql> insert into tb1 (`name`) values(3);

mysql> update tb1 set name = 'cao' where id = '4';     #id是4的就更改name值

select * from tb1;              # 檢視表裡的内容

select database();   #檢視在哪個資料庫下

select user();      #檢視在哪個使用者

select version();     #檢視是哪個版本

show tables;       #檢視有哪些表

desc  tb1;    #檢視表裡面有哪些字段

show create table tb1\G;               #檢視創表的選項/語句,整齊顯示

show create table pre_forum_post\G;          #文章表,post裡包含了很多創表的語句

delete from tb1 where id='2';          #删除表裡的資料id為2的行

truncate table abc.tb1;       #清空指定庫裡的表内容,庫.表

drop table tb1;          #删除表

drop database abc;          #删除庫

myslq> grant all on 庫.*  to  ‘user1’@’192.168.11.%’  identified by ‘授權的密碼’;         #.*是所有,to後面跟使用者名,%是通配,是所有

flush privileges;     #重新整理權限

show processlist;    #檢視目前資料庫有哪些隊列

show variables;    #檢視所有的變量

set  globalmax_connetion=200         #預設是150,改為200(隻是臨時更改,需要永久生效:修改my.cnf裡的配置

show variables line ‘max_connections’;   #和shell的grep的過濾意思差不多

show variables line ‘max_connec%’;        #檢視max_connec開頭的變量

show status;     #檢視所有狀态,用在調優比較多

show status like ‘%running’;    #檢視以比對running結尾

show status like ‘%buffer%’;    #檢視比對中間為buffer

mysql錯誤日志:/etc/init.d/mysqld裡的datadir定義的路徑,有以本地hostname.err

修複表的指令:repairtable wsw123.pre_forum_post;   #wsw123是庫,pre_forum_post是表名

備份:

mysqldump  -p密碼 庫名 > /test/discuz.sql                  #備份整個論壇的庫

mysqldump -uroot –p12345 wsw123 pre_forum_post > /test/post.sql    #備份論壇的庫裡的表

恢複:

mysql  -uroot –p密碼  -D 庫名 < /test/discus.sql

mysql  -uroot –p密碼  -D 庫名 </test/post.sql          #恢複時候不用加表名,直接庫名就好

特殊備份:

備份字元集,mysqldump-uroot --default-character-set=gbk  -p12345wsw123  pre_forum_post >/test/post.sql  #防止亂碼

還原字元集,mysql  -uroot --default-character-set=gbk  -p密碼  -D 庫名 </test/post.sql

字元集相容性較好是utf8

檢視日志:

因為mysql的日志是二進制,是以要用mysqlbinlog指令來檢視

     本文轉自wsw26 51CTO部落格,原文連結:http://blog.51cto.com/wsw26/1750702,如需轉載請自行聯系原作者