配置数据库,使能够被远程访问:
- 进入数据库:mysql -uroot -p
- 创建可供远程连接的用户:GRANT ALL PRIVILEGES ON . TO 'root'@'%' IDENTIFIED BY '123' WITH GRANT OPTION;
- 使新用户生效:flush privileges;
- 修改配置文件:vim /etc/mysql/mysql.conf.d/mysqld.cnf
将该文件中的:bind-address = 127.0.0.1这行注释掉 - 重启mysql:sudo service mysql restart
python连接mysql数据库代码:
import pymysql
conn = pymysql.connection(host='10.1.***.***', port=3306, user='root', passwd='123', db='face_image', charset='utf8')
cursor = conn.cursor()
sql = "insert into face values(" + str(p_id) + ",\""+ gender_text + "\"," + "%d" % emotion_label_arg + "\""+ emotion_text + "\","+ "current_timestamp);"
cursor.execute(sql)
conn.commit()
建数据库:create database name;
选择某个数据库:use name;
直接删除数据库,不提醒:drop database name
显示数据库:show databases;
建表:create table name();
显示表:show tables
网友评论