Ubuntu MySQL python

来源:互联网 发布:淘宝详情页优惠券设置 编辑:程序博客网 时间:2024/06/16 15:14

安装MySQL

sudo apt-get updatesudo apt-get install mysql-server mysql-clientpip install MySQL-python

若出现错误:EnvironmentError: mysql_config not found,安装libmysqlclient-dev

sudo apt-get install libmysqlclient-devpip install MySQL-python
pip install PyMySQL

基本开启关闭和登录退出语法

sudo start mysqlsudo stop mysqlsudo /etc/init.d/mysql restartsudo /etc/init.d/mysql startsudo /etc/init.d/mysql stopmysql -u root -p密码show databases;use basename;show tables;exit

[MySQL学习资源]

python连接mysql

import pymysqlconn = pymysql.connect(host='127.0.0.1', port=3306,user='root', passwd='123456', db='tianchinew', charset='utf8')# 创建游标cursor = conn.cursor()createtablesql='create table user(id int primary key auto_increment, user_id int ,item_id int, item_category int,date date ,hour int,view int,favor int,addcar int, buy int)'# 执行SQL,并返回收影响行数effect_row = cursor.execute(createtablesql) effect_row = cursor.execute("LOAD DATA LOCAL INFILE '***.csv'  INTO TABLE user  FIELDS TERMINATED BY "," OPTIONALLY ENCLOSED BY '"' ESCAPED BY '"' LINES TERMINATED BY '\r\n' IGNORE 1 LINES; ")# 执行SQL,并返回受影响行数# effect_row = cursor.execute("update tb7 set pass = '123' where nid = %s", (11,))# 执行SQL,并返回受影响行数,执行多次# effect_row = cursor.executemany("insert into tb7(user,pass,licnese)values(%s,%s,%s)", [("u1","u1pass","11111"),("u2","u2pass","22222")])# 提交,不然无法保存新建或者修改的数据conn.commit()# 关闭游标cursor.close()conn.close()

载入文件到mysql

<code class="hljs r">LOAD DATA [LOW_PRIORITY | CONCURRENT] [LOCAL] INFILE 'file_name.txt'   [REPLACE | IGNORE]  INTO TABLE tbl_name   [FIELDS    [TERMINATED BY 'string']   [[OPTIONALLY] ENCLOSED BY 'char']   [ESCAPED BY 'char' ] ]   [LINES   [STARTING BY 'string']  [TERMINATED BY 'string']  ]   [IGNORE number LINES]  [(col_name_or_user_var,...)]   [SET col_name = expr,...]]</code>

例如:

创建表mysql> create table if not exists user (user_id int ,item_id int ,item_category int ,date date,hour int ,view int,favor int,addcar int,buy int)default charset=utf8;外部数据导入表中,忽略表头第一行,忽略第一列用@dummymysql>load data local infile '/home/flyvideo/PycharmProjects/ljm/tianchi_newOffline/data/user_df.csv' into table user fields terminated by ',' lines terminated by '\n' ignore 1 lines (@dummy,user_id,item_id,item_category,date,hour,view,favor,addcar,buy);

换行 Unix \n windows \r\n mac \r

遇到问题

  • 客户端没有导入导出权限

-mysql服务器端的文件读写权限是默认开启的,但是客户端默认关闭。修改配置文件:

sudo vim /etc/mysql/my.cnf

在mysqld和mysql下分别添加:local-infile=1

  • ERROR 1290 (HY000): The MySQL server is running with the –secure-file-priv option so it
    MySQL默认设置的导入导出权限

该目录没有文件导入导出权限
1.如果secure_file_priv 的值为空的话,这个变量将不起作用;

2.如果secure_file_priv的值为路径的话,则mysql服务器将限制导入导出操作功能在这个设置的路径下使用。这个路径必须存在。

3.如果secure_file_priv的值设置为空的话,mysql服务器不能使用导出导入操作功能.但是,在mysql 5.7.6下是可以操作的。

查看该值的设定:mysql>SHOW VARIABLES LIKE "secure_file_priv";修改配置文件:vi /etc/mysql/mysql.conf在[mysqld]处添加:secure_file_priv=""重启一下mysql
  • file ’ ’ not found
    找不到文件:Ubuntu系统文件路径没有读写的权限
    要解决这个问题会扯到 AppArmor。这是一个保护机制,限制每个程序对特定目录和文件的访问权限。也即是说,当前 mysql 程序访问这个文件的权限被 AppArmor 限制住了

1)打开 /etc/apparmor.d/usr.sbin.mysqld 文件
2)此时能看到很多关于mysql能够读写为目录和文件的记录,文件尾部加入:

#你需要增加读取权限的目录/tmp/ r,/tmp/* rw,

保存并退出。
3)重新导入 AppArmor 配置,利用 /etc/init.d/apparmor reload 命令
4)重新启动 mysql,利用 service mysql restart

  • 还是找不到
    添加local:
load data local infile ''.....
原创粉丝点击