ÉèÖÃLinuxϵͳÒÔÖ§³ÖÊý¾Ý¿â±à³Ì
ÉèÖÃlinuxϵͳÒÔÖ§³ÖÊý¾Ý¿â±à³Ì
ÓÉÓÚLinuxϵͳµÄ¿ªÔ´ÐÔºÍÎȹÌÐÔ£¬Ô½À´Ô½¶àµÄ¿ª·¢ÕßÑ¡ÔñÔÚLinuxÇéÐÎϾÙÐÐÊý¾Ý¿â±à³Ì¡£ÎªÁË˳Ëì¾ÙÐÐÊý¾Ý¿â±à³ÌÊÂÇ飬ÎÒÃÇÐèÒªÔÚLinuxϵͳÖоÙÐÐһЩÉèÖá£
Ê×ÏÈ£¬ÎÒÃÇÐèҪװÖÃÊý¾Ý¿âЧÀÍÆ÷Èí¼þ¡£³£¼ûµÄÊý¾Ý¿âÈí¼þ°üÀ¨MySQL¡¢PostgreSQLºÍOracleµÈ¡£ÔÚ±¾ÎÄÖУ¬ÎÒÃÇÒÔMySQLΪÀý¾ÙÐÐÏêϸ½â˵¡£
×°ÖÃMySQLÊý¾Ý¿â
ÔÚLinuxϵͳÖУ¬ÎÒÃÇ¿ÉÒÔʹÓðü¹ÜÀí¹¤¾ßÀ´×°ÖÃMySQL¡£ÒÔDebian/UbuntuϵͳΪÀý£¬¿ÉÒÔʹÓÃÒÔÏÂÏÂÁî¾ÙÐÐ×°Öãº
sudo apt-get update sudo apt-get install mysql-server
µÇ¼ºó¸´ÖÆ
×°ÖÃÀú³ÌÖÐϵͳ»áÌáÐÑÓû§ÉèÖÃMySQLµÄrootÓû§ÃÜÂ룬Îñ±Ø¼Ç×ÅÕâ¸öÃÜÂë¡£
ÉèÖÃMySQL
ĬÈÏÇéÐÎÏ£¬MySQLÖ»ÔÊÐíµ±ÌïÖ÷ʱ»ú¼ûÊý¾Ý¿â¡£ÈôÊÇÎÒÃÇÏ£Íûͨ¹ýÍøÂç»á¼ûMySQLÊý¾Ý¿â£¬ÐèÒª¾ÙÐÐһЩÉèÖá£
Ê×ÏÈ£¬ÎÒÃÇÐèÒª±à¼MySQLµÄÉèÖÃÎļþ¡£ÒÔUbuntuϵͳΪÀý£¬ÉèÖÃÎļþλÓÚ /etc/mysql/mysql.conf.d/mysqld.cnf¡£Ê¹ÓÃÎı¾±à¼Æ÷·¿ª´ËÎļþ£º
sudo nano /etc/mysql/mysql.conf.d/mysqld.cnf
µÇ¼ºó¸´ÖÆ
ÕÒµ½ bind-address ÕâÒ»ÐУ¬²¢½«Æä×¢Ê͵ô£¨ÔÚÐÐÊ×Ìí¼Ó # ·ûºÅ£©¡£ÉúÑIJ¢¹Ø±ÕÎļþ¡£
½ÓÏÂÀ´£¬ÎÒÃÇÐèÒª½¨ÉèÒ»¸öÔÊÐíÔ¶³Ì»á¼ûµÄMySQLÓû§£¬²¢ÊÚȨ¸ø¸ÃÓû§»á¼ûÊý¾Ý¿âµÄȨÏÞ¡£·¿ªMySQLÏÂÁîÐУº
mysql -u root -p
µÇ¼ºó¸´ÖÆ
ÊäÈë֮ǰÉèÖõÄrootÃÜÂëµÇ¼MySQL¡£ÔÚMySQLÏÂÁîÐÐÖУ¬Ö´ÐÐÒÔÏÂÓï¾äÀ´½¨ÉèÔÊÐíÔ¶³Ì»á¼ûµÄÓû§£º
CREATE USER 'your_username'@'%' IDENTIFIED BY 'your_password';
µÇ¼ºó¸´ÖÆ
Ç뽫 ‘your_username’ Ì滻ΪÄãÏëÒªµÄÓû§Ãû£¬½« ‘your_password’ Ì滻ΪÄãÏëÒªµÄÃÜÂë¡£
È»ºó£¬Ö´ÐÐÒÔÏÂÓï¾äÀ´ÊÚȨ¸ÃÓû§»á¼ûÊý¾Ý¿âµÄȨÏÞ£º
GRANT ALL PRIVILEGES ON *.* TO 'your_username'@'%'; FLUSH PRIVILEGES;
µÇ¼ºó¸´ÖÆ
ͬÑù£¬½« ‘your_username’ Ì滻ΪÄ㽨ÉèµÄÓû§Ãû¡£
×°Öñà³ÌÓïÑÔµÄÊý¾Ý¿âÇý¶¯
ÔÚ¾ÙÐÐÊý¾Ý¿â±à³Ì֮ǰ£¬ÎÒÃÇÐèҪװÖÃÏìÓ¦±à³ÌÓïÑÔµÄÊý¾Ý¿âÇý¶¯³ÌÐò¡£ÒÔPythonΪÀý£¬ÎÒÃÇ¿ÉÒÔʹÓÃpipÏÂÁîÀ´×°ÖÃMySQLÇý¶¯³ÌÐò£º
sudo pip install mysql-connector-python
µÇ¼ºó¸´ÖÆ
¹ØÓÚÆäËû±à³ÌÓïÑÔ£¬×°ÖÃÏìÓ¦µÄÊý¾Ý¿âÇý¶¯³ÌÐòÒ²ÊÇÀàËƵġ£
±à³ÌʾÀý
½ÓÏÂÀ´£¬ÎÒÃÇÀ´ÑÝʾÔõÑùʹÓÃPythonÅþÁ¬MySQLÊý¾Ý¿â£¬²¢¾ÙÐÐһЩ³£¼ûµÄÊý¾Ý¿â²Ù×÷¡£
Ê×ÏÈ£¬ÎÒÃÇÐèÒªµ¼ÈëMySQLÊý¾Ý¿âÇý¶¯³ÌÐò£º
import mysql.connector
µÇ¼ºó¸´ÖÆ
È»ºó£¬ÎÒÃÇ¿ÉÒÔʹÓÃÒÔÏ´úÂëÀ´½¨ÉèÓëMySQLÊý¾Ý¿âµÄÅþÁ¬£º
mydb = mysql.connector.connect( host="your_host", user="your_username", password="your_password", database="your_database" )
µÇ¼ºó¸´ÖÆ
½« “your_host” Ì滻ΪÄãµÄMySQLЧÀÍÆ÷IPµØµã»òÖ÷»úÃû£¬½« “your_username” Ì滻ΪÄ㽨ÉèµÄÓû§Ãû£¬½« “your_password” Ì滻ΪÄ㽨ÉèµÄÃÜÂ룬½« “your_database” Ì滻ΪÄãÒªÅþÁ¬µÄÊý¾Ý¿âÃû¡£
½ÓÏÂÀ´£¬ÎÒÃÇ¿ÉÒÔÖ´ÐÐSQLÓï¾äÀ´Ö´ÐÐÖÖÖÖÊý¾Ý¿â²Ù×÷¡£ÒÔÏÂÊÇÒ»¸ö¼òÆÓµÄʾÀý£¬ÏòÊý¾Ý¿âÖеıí¸ñ²åÈëÒ»Ìõ¼Í¼£º
mycursor = mydb.cursor() sql = "INSERT INTO your_table (column1, column2) VALUES (%s, %s)" val = ("value1", "value2") mycursor.execute(sql, val) mydb.commit()
µÇ¼ºó¸´ÖÆ
½« “your_table” Ì滻ΪÄãÒª²åÈëÊý¾ÝµÄ±íÃû£¬½« “column1″¡¢”column2” Ì滻ΪÄãÒª²åÈëÊý¾ÝµÄÁÐÃû£¬½« “value1″¡¢”value2” Ì滻ΪÄãÒª²åÈëµÄÏêϸֵ¡£
ÒÔÉϽöÊÇÒ»¸ö¼òÆÓµÄʾÀý£¬ÏÖʵµÄÊý¾Ý¿â±à³ÌÊÂÇéÖÐÎÒÃÇ¿ÉÄÜÐèÒªÖ´ÐиüÖØ´óµÄ²Ù×÷£¬°üÀ¨ÅÌÎÊÊý¾Ý¡¢¸üÐÂÊý¾ÝµÈ¡£
ÔÚÕâƪÎÄÕÂÖУ¬ÎÒÃÇÏêϸÏÈÈÝÁËÔõÑùÉèÖÃlinuxϵͳÒÔÖ§³ÖÊý¾Ý¿â±à³Ì£¬ÒÔ¼°ÔõÑùʹÓÃPythonÅþÁ¬MySQLÊý¾Ý¿â²¢Ö´ÐмòÆÓµÄÊý¾Ý¿â²Ù×÷¡£Ï£ÍûÕâЩÄÚÈݶÔÄãÔÚLinuxÇéÐÎϾÙÐÐÊý¾Ý¿â±à³ÌÓÐËù×ÊÖú¡£×£Äú±à³ÌÓä¿ì£¡
ÒÔÉϾÍÊÇÉèÖÃLinuxϵͳÒÔÖ§³ÖÊý¾Ý¿â±à³ÌµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡