步骤:
1、首先安装MySQL,为保证以下步骤能够顺利执行,要求数据库为最初的安装状态;
2、在MySQL服务器上登录MySQL进行初始化设置:
首先用:
sudo vim /etc/mysql/my.cnf
# Instead of skip-networking the default is now to listen only on
# localhost which is more compatible and is not less secure.
# bind-address = 127.0.0.1 <span style="color:#ff0000;">//注释掉此句,因为默认MySQL是只能本地访问的。</span>
#
# * Fine Tuning
然后登录MySQL:
<span style="white-space:pre"> </span>mysql -u root -p
更改mysql 数据库的user表里的host项,将“localhost”改为“%”
update user set host = '%' where user = 'root';
select host, user from user;
这样还不行,最后还要执行:
mysql>FLUSH PRIVILEGES
才能生效
不要忘了重启MySQL,不然是不起作用的!!!记得加sudo权限哦。。
sudo service mysql restart
3、在远程主机上执行下面的Python脚本,执行前要安装
apt-get install python-mysqldb
注:这里我遇到了很多问题,因为222上更改了好多配置,最后也没解决下面的问题:import MySQLdb import sys,string,os try: conn = MySQLdb.connect(host ='192.168.102.221',user='root',db='test',passwd='password',port=3306) //这里对test数据库进行操作 except Exception,e: print e sys.exit() cur = conn.cursor() sql = """insert into config (id,dpid,port,bandwidth) values(4,"0000125",2,1)""" //此条语句按实际情况来写。 try: cur.execute(sql) conn.commit() except Exception,e: print e conn.rollback() conn.close print "success"
(2003, "Can't connect to MySQL server on '192.168.102.222' (111)")