ºìÁªLinuxÃÅ»§
Linux°ïÖú

Mysql´æȡȨÏÞϵͳ

·¢²¼Ê±¼ä:2005-06-24 00:23:09À´Ô´:ºìÁª×÷Õß:linux_
MysqlÓÐÒ»¸ö¸ß¼¶µÄµ«²»±ê×¼µÄ´æȡȨÏÞϵͳ.´Ë²¿·Ö¼´ÃèÊöÆäÈçºÎ¹¤×÷.

ȨÏÞϵͳ×öʲô

MysqlµÄȨÏÞϵͳµÄÖ÷Òª¹¦ÄÜÊÇÑéÖ¤´Ó¼Ù¶¨Ö÷»úÀ´µÄÓû§Á¬½Ó.ÒÔ¼°¼ì²éÓÃ
»§¶ÔÊý¾Ý¿âµÄselect,insert,updateºÍdelete²Ù×÷ȨÏÞ.

Æ丽¼Ó¹¦ÄÜ»¹°üÀ¨ÄäÃûÓû§ÑéÖ¤¼°¸³ÓèȨÏÞÒÔʹÓÃMysqlÌØÊâµÄ¹¦ÄÜ,Èç´Ó
ÎļþÖÐ×°ÈëÊý¾ÝºÍ¹ÜÀí²Ù×÷.

MysqlµÄÓû§ÃûºÍ¿ÚÁî

MysqlµÄÓû§ÃûºÍ¿ÚÁîÔÚUnixºÍWindowsÖ®¼äÓÐһЩ²î±ð:

?Óû§Ãû,MysqlÓÃÓÚ´ïµ½ÆäÑé֤ĿµÄ.ÓëUnixºÍWindowsµÄÓû§ÃûºÁÎÞ¸Éϵ
(login names).´ó¶àÊýµÄMysql¿Í»§¶ËȱʡµÄʹÓÃUinxµ±Ç°Óû§Ãû×÷Ϊ
MysqlÓû§ÃûÈ¥µÇ¼,µ«ÄÇÖ»ÊÇΪÁË·½±ã¶øÒÑ.¿Í»§¶Ë³ÌÐòÔÊÐíʹÓÃ-u»ò--
user²ÎÊýÖ¸¶¨Ò»¸ö²»Í¬µÄÃû×Ö.ÕâÒâζ×ÅÄã²»¿ÉÄÜʹMysqlÊý¾Ý¿âµÃµ½°²È«
±£»¤³ý·Çÿһ¸öMysqlÓû§Ãû¶¼¾ßÓпÚÁî.ÈκÎÈ˶¼¿ÉÒÔÓÃÈκÎÃû×ÖÀ´Æóͼ
Á¬½ÓÊý¾Ý¿â,Èç¹ûËûÃÇÖ¸¶¨µÄijһ¸öÃû×ÖÇ¡ºÃûÓпÚÁî,ËûÃǾͻáÈ¡µÃ³É¹¦
.

?MysqlÓû§Ãû¿ÉÒÔÌá¸ßµ½Ê®Áùλ×Ö·û³¤,¶øµäÐ͵ÄUnixÓû§Ãû¶¼ÏÞÖÆÔÚ°Ëλ
.

?MysqlµÄ¿ÚÁî¸úUnix¿ÚÁîºÁÎÞ¸Éϵ.²»±Ø°ÑÄãµÇ¼½øUnix»úÆ÷µÄ¿ÚÁîºÍÄãÓÃ
À´ÔڸûúÆ÷ÉÏ´æÈ¡Êý¾Ý¿âµÄ¿ÚÁîÁªÏµÆðÀ´.

?MysqlʹÓÃÁËÓëUnixµÇ¼½ø³Ì²»Í¬µÄ¼ÓÃÜËã·¨.Çë²Î¼û7.3.11½Ú¸÷ÖÖº¯ÊýÖÐ
¶ÔPASSWORD()ºÍENCRYPT()º¯ÊýµÄÃèÊö.

6.3Á¬½ÓÉÏMysql·þÎñÆ÷

Mysql¿Í»§¶Ë³ÌÐòͨ³£»áÈÃÄãÖ¸¶¨Ò»Ð©Á¬½Ó²ÎÊý,µ«ÄãÏë´æÈ¡MysqlÊý¾Ý¿âµÄʱ
ºò:ÄãÏëÁ¬½ÓµÄÖ÷»ú,ÄãµÄÓû§ÃûºÍ¿ÚÁî.ÀýÈç,Mysql¿Í»§¶Ë¿ÉÒÔÏóÕâÑù¿ªÊ¼(
¿ÉÑ¡²ÎÊý±»¼ÓÉÏÁË""[""ºÍ""]""):

shell> mysql [-h Ö÷»úÃû] [-u Óû§Ãû] [-p¿ÚÁî]

Ò²¿ÉÒÔÓÃ--host=Ö÷»úÃû,--user=Óû§ÃûºÍ--password=¿ÚÁîÀ´´úÌæ-h,-uºÍ-p
Ñ¡Ïî.×¢Òâ,ÔÚ-p»ò--passwordºÍ¿ÚÁîÖ®¼äûÓпոñ.

Èç¹ûÔÚÃüÁîÐÐûÓÐÖ¸¶¨Á¬½Ó²ÎÊý,Mysql½«Ê¹ÓÃȱʡֵ:

?ȱʡµÄÖ÷»úÃûÊÇlocalhost.
?ȱʡµÄÓû§ÃûÊÇÄãÔÚUnixÖеĵǼÃû.
?Èç¹ûûÓÐ-p²ÎÊýÔò½«²»Ìṩ¿ÚÁî.

Òò¶ø,¶ÔÓÚUnixÓû§JoeÈçϵÄÃüÁîÊǵȼ۵Ä

shell> mysql -h localhost -u joe
shell> mysql -h localhost
shell> mysql -u joe
shell> mysql

ÆäËüµÄ¿Í»§¶ËµÄ¶¯×÷Óë´ËÀàËÆ.

ÔÚUnixϵͳÖÐ,Äã²»±ØÔÚÿһ´Îµ÷Óÿͻ§»ú³ÌÐò½øÐÐÁ¬½ÓµÄʱºò¶¼ÔÚÃüÁîÐÐÇÃÈë
ËùÓÐÑ¡Ïî,¿ÉÒÔΪËüÃÇÖ¸¶¨²»Í¬µÄȱʡֵ.Õâ¿ÉÒÔÓÃÁ½ÖÖ·½·¨À´°ìµ½:

?Äã¿ÉÒÔÔÚÄãµÄhomeĿ¼ÖеÄ"".my.cnf""ÅäÖÃÎļþÖеÄ[client]Ò»½ÚÖ¸¶¨Á¬½Ó²Î
Êý.¸ÃÎļþµÄÏà¹Ø½Ú¿´ÆðÀ´¿ÉÄÜÏóÕâÑù:

[client]host=host_name
user=user_name
password=your_pass

Çë²Î¼û4.15.4Ñ¡ÏîÎļþÒ»½Ú
?ÄãÒ²¿ÉÒÔʹÓû·¾³±äÁ¿À´Ö¸¶¨Á¬½Ó²ÎÊý.Ö÷»ú¿ÉÒÔ±»Ö¸¶¨Ê¹ÓÃMYSQL_HOST,Mysql
Óû§Ãû¿ÉÒÔ±»Ö¸¶¨Ê¹ÓÃUSER,LOGNAME»òÊÇLOGIN(¾¡¹ÜÕâЩ±äÁ¿¿ÉÄÜÒѾ­±»ÉèÖÃ
³ÉÄãµÄUnixµÇ¼Ãû,µ«ËüÃÇ¿ÉÄܱ»ÈÎÒâ¸Ä±ä).¿ÚÁî¿ÉÒÔ±»Ö¸¶¨ÎªÊ¹ÓÃMYSQL_PWD(
µ«ÕâÊDz»°²È«µÄ;²Î¼ûϽÚ)

Èç¹ûͨ¹ý¶àÖÖ²»Í¬µÄ·½·¨Ö¸¶¨Á¬½Ó²ÎÊý,ÄÇôÔÚÃüÁîÐÐÖÐÖ¸¶¨µÄÖµ±ÈÔÚÅäÖÃÎļþÖÐ
ºÍ»·¾³±äÁ¿ÖÐÖ¸¶¨µÄÖµ¾ßÓиü¸ßµÄÓÅÏÈȨ.¶øÔÚÅäÖÃÎļþÖÐÖ¸¶¨µÄÖµÓÖ±ÈÔÚ»·¾³±ä
Á¿ÖÐÖ¸¶¨µÄÖµ¾ßÓиü¸ßµÄÓÅÏÈȨ.

ʹÄãµÄ¿ÚÁȫ

ÔÚÆäËüÓû§¿ÉÒÔ·¢ÏÖµÄÇé¿öϽ«ÄãµÄ¿ÚÁîÓÃÒ»ÖÖ·½·¨±©Â¶µÄÖ¸¶¨³öÀ´ÊÇÏ൱ʧ²ßµÄ
.ÏÂÃæÁгöÁ˵±ÄãʹÓÿͻ§¶Ë³ÌÐòʱ¿ÉÒÔÓÃÀ´Ö¸¶¨ÃÜÂëµÄ·½·¨,±È±È½ÏÁËÿһÖÖ·½·¨
µÄÓÅÁÓ:

?ÔÚÃüÁîÐÐʹÓÃÒ»¸ö -pÃÜÂë »ò --password=ÃÜÂë ²ÎÊý.ÕâÈ·ºÜ·½±ã,µ«¼«²»°²È«,
ÄãÃÜÂëÄܱ»ÏµÍ³×´Ì¬¼ì²â³ÌÐò¿´¼û(Èçps),ËùÒÔ¿ÉÒÔ±»ÆäËûÓû§µ÷ÓóöÀ´ÏÔʾÔÚ
ÃüÁîÐÐÉÏ.(MySQL¿Í»§¶ËÌرðÔÚÃüÁîÐвÎÊý³õʼ»¯Ê±ÓÃ0¸²¸ÇËüÃÇ,µ«ÔÚ¸ÃÖµÏÔµÃ
¿É¼ûʱÈÔÓжÌÔݵļä¸ô).
?ÓÃÒ»¸ö-p»ò--password²ÎÊý(²»Ö¸¶¨ÄãµÄÃÜÂë).ÕâÑù,¿Í»§¶Ë³ÌÐò»áÒªÇóÃÜÂë´ÓÖÕ
¶ËÊäÈë:

shell> mysql -u ̞ -p]
Enter password: ********

¿Í»§¶Ë°ÑÄãÊäÈëµÄÃÜÂëÒÔ*""×Ö·ûÏÔʾÔÚÖÕ¶ËÉÏ,ÒÔʹÅÔ¹ÛÕßÎÞ·¨¿´¼û.Èç´ËÊäÈë
ÃÜÂë±ÈÄãÔÚÃüÁîÐÐÉÏÖ¸¶¨ÃÜÂë¸ü°²È«,ÒòΪËü¶ÔÆäËûÓû§²¢²»¿É¼û.µ«ÊÇ,´ËÊäÈë
ÃÜÂëµÄ·½·¨Ö»ÔÚÄãʹÓý»»¥Ê½³ÌÐòʱ²ÅÓÐЧ.Èç¹ûÄãÏë·Ç½»»¥µÄ´ÓÒ»¸ö½Å±¾Öе÷
Óÿͻ§¶Ë,ÔòûÓлú»á´ÓÖÕ¶ËÊäÈëÃÜÂëÁË.
?°ÑÄãµÄÃÜÂë±£´æÔÚÒ»¸öÅäÖÃÎļþÖÐ.¾ÙÀýÀ´Ëµ,Äã¿ÉÒÔ°ÑÄãµÄÃÜÂëÁÐÔÚÄãhomeĿ¼
ϵÄ.my.cnfÎļþµÄ[client]½Ú:

[client]
password=your_pass

Èç¹ûÄã°ÑÃÜÂë´æ·ÅÔÚ.my.cnfÖÐ,ÄÇôͬ×éÓû§ºÍÆäËûÓû§¶Ô¸ÃÎļþ±ØÐë²»Äܾß
ÓжÁÈ¡ºÍдÈëµÄȨÏÞ.È·¶¨ÎļþµÄ´æȡģʽ±ØÐëΪ400»ò600.²Î¼û4.15.4½ÚÑ¡ÏîÎÄ
¼þ.
?ÄãÒ²¿ÉÒÔ°ÑÄãµÄÃÜÂë´æ·ÅÔÚMYSQL_PWD»·¾³±äÁ¿ÖÐ,µ«¸Ã·½·¨±»ÈÏΪ¼«²»°²È«¶øͨ
³£²»±»Ê¹ÓÃ.ijЩ°æ±¾µÄPS°üÀ¨Ò»¸öÑ¡Ïî¿ÉÒÔÁгöÕýÔÚÔËÐеĽø³ÌµÄ»·¾³;ÄãµÄÃÜ
Â뽫±»Çå³þµÄÏÔʾ³öÀ´,Èç¹ûÄãÉèÖÃÁËMYSQL_PWD»·¾³±äÁ¿µÄ»°.

×ܶøÑÔÖ®,×ȫµÄ·½·¨ÊÇÔÚ¿Í»§¶ËÌáʾÊäÈëÃÜÂë»ò½«ÃÜÂë±£´æÔÚÊܵ½ÕýÈ·±£»¤µÄ
.my.cnfÎļþÖÐ.

MySQLÌṩµÄȨÏÞ

ȨÏÞÐÅÏ¢´æ·ÅÔÚmysqlÊý¾Ý¿â(¼´Êý¾Ý¿âµÄÃû×Ö½Ðmysql)µÄuser, db, host, tables
_priv and columns_priv±íÖÐ.µ±MySQL·þÎñÆ÷ÔÚÆô¶¯Ê±»òÔÚ6.9½Ú""¸Ä±äµÄȨÏÞºÎʱ
ÉúЧ""Ö¸³öµÄÇé¿ö϶ÁÈ¡ÕâЩ±íÖеÄÏîÄ¿.

´ËÊÖ²áËùÌá¼°µÄMySQLÌṩµÄȨÏÞµÄÃû×ÖÁÐÔÚÆäÏÂ,¸÷ÁÐÁгöÁËȨÏÞµÄÃû×Ö,¶ÔÓ¦µÄ²Ù
×÷ºÍÊÊÓõķ¶Î§.


ȨÏÞ ÁÐÃû ·¶Î§
select select_priv ±í
insert Insert_priv ±í
update Update_priv ±í
delete Delete_priv ±í
index Index_priv ±í
alter Alter_priv ±í
create ¡¡ Create_priv Êý¾Ý¿â,±í»òË÷Òý
drop Drop_priv Êý¾Ý¿â»ò±í
grant Grant_priv Êý¾Ý¿â»ò±í
reload Reload_priv ·þÎñÆ÷¹ÜÀí
shutdown Shutdown_priv ·þÎñÆ÷¹ÜÀí
process Process_priv ·þÎñÆ÷¹ÜÀí
file File_priv ·þÎñÆ÷ÉÏÎļþ´æÈ¡

select, insert, update ºÍ delete ȨÏÞÔÊÐíÄãÔÚÒ»¸öÊý¾Ý¿âÖдæÔڵıíµÄÐÐÉÏÖ´
ÐвÙ×÷.

½öµ±ÄãÕæÒª´Ó±íÖзµ»ØÐÐʱSELECT±í´ïʽ²ÅÐèÒªselectȨÏÞ.Äã¿ÉÒÔÖ´ÐÐÒ»¸öSELECT
±í´ïʽ,ÉõÖÁµ±ÄãÔÚ·þÎñÆ÷ÉÏûÓдæÈ¡ÈκÎÊý¾Ý¿âµÄȨÏÞʱҲÐÐ.±ÈÈç,Äã¿ÉÒÔÓÃmysql
¿Í»§¶Ë×÷Ò»¸ö¼òµ¥µÄ¼ÆÊý:

mysql> SELECT 1+1;
mysql> SELECT PI()*2;

indexȨÏÞÔÊÐíÄãcreate»òdrop(ɾ³ý)Ë÷Òý.

alterȨÏÞÔÊÐíÄãʹÓÃALTER TABLE.

createºÍdropȨÏÞÔÊÐíÄãcreateÐÂÊý¾Ý¿âºÍ±í,»òdrop(ɾ³ý)´æÔÚµÄÊý¾Ý¿âºÍ±í.

×¢Òâ,Èç¹ûÄãÔÊÐíÒ»¸öÓû§ÓµÓжÔmysqlÊý¾Ý¿âµÄdropȨÏÞ,ÄÇô¸ÃÓû§¿ÉÒÔdropµôMySQL
ÓÃÀ´´æ·Å´æȡȨÏ޵ĵط½.

grantȨÏÞÔÊÐíÄãÊÚÓèÆäËûÓû§ÄãËùÓµÓеÄÄÇЩȨÏÞ.

fileȨÏÞÔÊÐíÄãÓÃLOAD DATA INFILE ºÍ SELECT ... INTO OUTFILEÓï¾ä´æÈ¡·þÎñÆ÷ÉÏ
µÄÎļþ.ÈκÎÓû§Èç¹û¾ßÓÐÁËÕâ¸öȨÏÞËû¾Í¿ÉÒÔ´æÈ¡ÈκÎMySQL·þÎñÆ÷¿ÉÒÔ´æÈ¡µÄÎļþ.
ʣϵÄȨÏÞÓÃÓÚ¹ÜÀí²Ù×÷,ÔÚÖ´ÐÐmysqladmin³ÌÐòʱ»áÓõ½.ϱíÁгöÁËmysqladminÃü
ÁîÔÊÐíÄãÖ´ÐеÄÿһ¸ö¹ÜÀíȨÏÞ:



ȨÏÞ È¨ÏÞÓµÓÐÕß¿ÉÒÔÖ´ÐеIJÙ×÷
reload reload, refresh, flush-privileges, flush-hosts, flush-logs, flush-tables
shutdown shutdown
process processlist, kill

reloadÃüÁî¸æËß·þÎñÆ÷ÖØжÁÈ¡ÈÏÖ¤±í.refreshÃüÁîË¢ÐÂËùÓеÄÊý¾Ý¿â,´ò¿ªºÍ¹Ø±ÕÈÕ
Ö¾Îļþ.flush-privilegesÊÇreloadµÄÒ»¸öͬÒå´Ê.ÆäËüflush-*Ö´ÐкÍrefresh´óÖÂÏà
ͬµÄ¹¦ÄÜ,µ«ÊÇÔÚ¸üÓÐÏ޵ķ¶Î§ÄÚ,Ò²ÐíÔÚijЩ³¡ºÏЧ¹û¸üºÃ.ÀýÈç,Èç¹ûÄã½öÏëË¢ÐÂÈÕ
Ö¾Îļþ,flush-logs¾ÍÊDZÈrefresh¸üºÃµÄÑ¡Ôñ.

shutdownÃüÁî¹Ø±Õ·þÎñÆ÷.

processlistÃüÁîÁгöÓйطþÎñÆ÷ÖÐËùÖ´ÐеÄÏ̵߳ÄÐÅÏ¢.killÃüÁîɱµô·þÎñÆ÷µÄÏß³Ì
.Äãͨ³£Ö»ÄÜÁгöºÍɱµôÄã×Ô¼ºµÄÏß³Ì,µ«Èç¹ûÄãÓÐprocessȨÏ޾ͿÉÒÔÁгö»òɱµôÓÉÆä
ËûÓû§Æô¶¯µÄÏß³Ì.

ͨ³£Ö»ÊÚÓèÏà¹ØÓû§Ò»Ð©±ØÐëȨÏÞÊÇÒ»¸öºÃ°ì·¨,µ±ÄãµÃѧ»áСÐĽ÷É÷µÄÊÚ³öÏÂÁÐȨÏÞ:

?grantȨÏÞÔÊÐíÓû§°ÑËûÃÇ×Ô¼ºµÄȨÏÞÊÚÓèÁíÍâһЩÓû§.Èç¹ûÁ½¸öÓû§Óв»Í¬µÄȨ
ÏÞ,¶øÓÖͬʱ¾ßÓÐgrantȨÏ޵Ļ°,ÄÇËûÃǾ͹²Í¬¾ßÓÐÁËÕâËùÓеÄÕâЩȨÏÞ.
?alterȨÏÞÓµÓÐÕß¿ÉÒÔÓøø±íÖØÃüÃûµÄ·½Ê½ÆÆ»µÈ¨ÏÞϵͳ.
?fileȨÏÞЩÐí»á±»ÀÄÓÃ,ÒÔÔÚÊý¾Ý¿â±íÖжÁÈ¡ÈκεÄÈ«³ÌÎļþ,¸ÃÏîÄ¿¿ÉÒÔʹÓÃSELECT
½øÐдæÈ¡.
?shudownȨÏÞÒ²¿ÉÄܱ»ÀÄÓÃ.¹Ø±Õ·þÎñÆ÷,¶ÔÆäËûËùÓÐÓû§¾Ü¾ø·þÎñ.
?processȨÏÞ¿ÉÒÔÒÔ´¿Îı¾µÄ·½Ê½ÏÔʾ³öµ±Ç°ÕýÖ´ÐеIJéѯ,°üº¬ÉèÖúÍÐÞ¸ÄÃÜÂëµÄ²é
ѯ.
?mysqlÊý¾Ý¿âÉϵÄȨÏÞ¿ÉÒÔÓÃÀ´¸Ä±äÃÜÂëºÍÆäËü´æȡȨÏÞÐÅÏ¢.(ÃÜÂëÊǼÓÃÜ´æ´¢µÄ,Ëù
ÒÔÒ»¸öÓжñÒâµÄÓû§²»Äܼòµ¥µÄ¶ÁÈ¡ËüÃÇ.µ«ÊÇ,Èç¹ûÓÐ×ã¹»µÄȨÏÞ,Ëû¿ÉÒÔ°ÑËü¸Ä³ÉÁí
Ò»¸ö²»Í¬µÄÃÜÂë.)

ͨ¹ýMySQLȨÏÞϵͳÎÞ·¨ÊµÏÖÏÂÃæһЩÊÂÇé:

?Äã²»ÄÜÃ÷È·µÄÖ¸¶¨Ò»¸öÓû§½«±»¾Ü¾ø´æÈ¡.Ò²¾ÍÊÇ˵,Äã²»ÄÜÃ÷È·Ö¸¶¨Æ¥ÅäÒ»¸öÓû§,¾Ü
¾øËüµÄÁ¬½Ó.
?Äã²»ÄÜÖ¸¶¨Ò»¸öÓû§ÄÜÔÚÒ»¸öÊý¾Ý¿âÖд´½¨ºÍɾ³ý±í,¶ø²»ÄÜ´´½¨ºÍɾ³ý¸ÃÊý¾Ý¿â×ÔÉí.
ÎÄÕÂÆÀÂÛ

¹²ÓÐ 1 ÌõÆÀÂÛ

  1. jerry520 ÓÚ 2009-03-27 16:40:14·¢±í:

    ºÇºÇ,²»´í°¡!