How to store files in MySQL database using MySQL Workbench? - html

I'm trying to store .html files in a MySQL database (using MySQL Workbench). At the moment I'm trying to do load_file('filepath') so the contents of the .html file can be read and converted to string and that string will be stored in the table in the DB. But the problem is that it seems to always return null so I can't check if it's actually working.
I know there are 4 criteria to pass for load_file to work but I don't know which scripts I have to execute to grant the privileges. Since I'm trying to convert the contents of the file to string, maybe there is another way more efficient to store the whole .html file into the DB and if yes could anyone show me how to do this? My MySQL server has a username of root and working on Mac OS.

The load_file function says that:
To use this function, the file must be located on the server host, you must specify the full path name to the file, and you must have the FILE privilege. The file must be readable by all and its size less than max_allowed_packet bytes. If the secure_file_priv system variable is set to a nonempty directory name, the file to be loaded must be located in that directory.
So there are a number of conditions to check if you actually can load the file. Start by checking if your user has the FILE privilege and that the file is accessible by your MySQL server.

For people who's looking for, this is how I did it.
I have a table as
CREATE TABLE `test` (
`ID` int(11) DEFAULT NULL,
`UPLOAD_FILE` mediumblob
);
and I save a file smiley.jpg as
INSERT INTO test (`ID`, `UPLOAD_FILE`) VALUES ('1', LOAD_FILE('E:/smiley.jpg'));

Related

LOAD_FUNCTION in MySQL shows an invalid identifier

I created a table namely pictures to insert images into the table. After using the insert command and load my picture with load_function it's showing an invalid identifier.
CREATE table pics(id number(10),pics1 blob);
INSERT INTO pics
values(1,LOAD_FUNCTION('C:\Users\Vandana\Desktop\face\my_image.jpg'));
Your issue is caused by your CREATE TABLE statement.
number is not a valid MySQL column data type, you should use INTEGER(10) SIGNED [sic].
CREATE TABLE pics(id INTEGER(10) SIGNED, pics1 BLOB);
However NUMBER is a valid data type for Oracle Database, which is very much so different from MySQL.
LOAD_FUNCTION is not a valid MySQL function [sic].
I believe you are wanting LOAD_FILE, which will retrieve the file's contents for the blob column.
You should also use double backslashes when referencing Windows file paths, to avoid escape sequence characters like \n.
INSERT INTO pics(id, pics1)
VALUES(1, LOAD_FILE('C:\\Users\\Vandana\\Desktop\\face\\my_image.jpg'));
Please keep in mind that LOAD_FILE only has access to the files
located on the system and that the user/group running the database service has read permissions for.
See secure_file_priv for more details.
In MySQL is better to use LOAD_FILE command:
MySQL LOAD_FILE() reads the file and returns the file contents as a string, which in your case will be a blob
CREATE table pics(id number(10),pics1 blob);
INSERT INTO pics
values(1, LOAD_FILE('C:\Users\Vandana\Desktop\face\my_image.jpg'));
Remembering: You need the right read privileges on your file.
You can refer:
More complete answer with load_file()

MySql General Query Log File Name in Truncate

This involves MySQL 5.7 running on Windows Server 2016.
I'm working with a TRUNCATE statement in MySql to reduce the size of a large Log file (named "mySite.log"), which resides in:
ProgramData/MySQL/MySQL Server 5.7/Data/
I have researched and implemented the following:
mysql> SET GLOBAL general_log=OFF;
and this was successful.
However, I am trying to ascertain that the large log file that I see in the directory stated above is in fact the General Query Log File. It carries the name of the database as the prefix of the file name ("MySite.log") just as the other files (.bin's and .err, .pid) in the same directory do.
Is this large log file actually the general_log file? (If using MySQL Workbench, where would the naming of the log file and storage location be set up? I can't seem to locate that.)
Will the following syntax truncate the log file?
mysql> TRUNCATE TABLE mysql.general_log;
Will 'TRUNCATE TABLE' be used, even if the log is stored in a file, rather than database table?
Will 'mysql.general_log' need to be renamed to 'myDatabase.mysite' to match the name of my "MySite.log" file, from above?
Thanks for any leads.
Some interesting manual entries to read about this:
5.4.1 Selecting General Query and Slow Query Log Output Destinations
5.4.3 The General Query Log
You can check how your server is configured with
SHOW GLOBAL VARIABLES LIKE '%log%';
Then look for the value of log-output. This shows if you're logging to FILE, TABLE or both.
When it's FILE, check for the value of general_log_file. This is where the log file is in your file system. You can simply remove it, and create a new file (in case you ever want to enable general_log again). Then execute FLUSH LOGS; afterwards.
When it's TABLE then your TRUNCATE TABLE mysql.general_log; statement is correct.
Regarding your second question, just never mess with the tables in the mysql schema. Just don't (if you don't know what you're doing). Also I don't even get how you got to that thought.

Load and replace file path string with the content from that file in a MySQL database

I have a database of entries consisting of a 'name', 'id' and a 'description', but currently the 'description' field is set to the file path of a .txt file that actually contains the description content. Each .txt file's name is each row's 'id', plus the .txt extension and they all reside in the same directory.
Can I load and replace each 'description' field with the content from the relevant text file (using MySQL)?
You can't write a MySQL query directly that will read the description values from your file system. That would require the MySQL server to be able to read raw text from files in your file system. You Can't Do Thatâ„¢.
You certainly can write a program in your favorite host language (php, java, PERL, you name it) to read the rows from your database, and update your description rows.
You could maybe contrive to issue a LOAD DATA INFILE command to read each text file. But the text files would have to be very carefully formatted to resemble CSV or TSV files.
Purely using mysql this would be a difficult, if not impossible exercise because mysql does not really offer any means to open files.
The only way to open an external text file from mysql is to use LOAD DATA INFILE command, which imports the text file into a mysql table. What you can do is to write a stored procedure in mysql that:
Create a temporary table with a description field large enough to accommodate all descriptions.
Reads all id and description field contents into a cursor from your base table.
Loop through the cursor and use load data infile to load the given text file's data into your temporary table. This is where things can go wrong. The account under which mysql daemon / service runs needs to have access to the directories and fiels where the description files are stored. You also need to be able to parametrise the load data infile command to read the full contents of the text file into a single field, so you need to set the field and line terminated by parameters to such values that cannot be found in any of the description files. But, even for this you need to use a native UDF (user defined function) that can execute command line programs because running load data infile directly from stored procedures is not allowed.
See Using LOAD DATA INFILE with Stored Procedure Workaround-MySQL for full description how to this.
Issue an update statement using the id from the cursor to update the description field in your base table from the temporary table.
Delete the record from your temp table.
Go to 3.
It may be a lot easier to achieve this from an external programming language, that has better file manipulation functions and can update each record in your base table accordingly.

Upload mysql database in chunks

I am trying to upload a 32mb MYSQL database into a pre-existing database, but the php admin on my shared hosting has a 10mb limit... I have tried zipping it up - but when the server unzips the database, the uncompressed file is too large for the server to handle.
Is it possible to split the database up and upload it by pasting it in parts as an SQL query - I assume I would need each chunk to have something at the start of it which says
"Import this data into the pre-existing tables in the database"
What would this be?
At the moment there is a few hundred lines saying things like "CREATE" and "INSERT INTO"
You might try connecting to the database remotely with mysql workbench, or command line tool mysql. If you can do that, you can run:
source c:/path/to/your/file.sql
and you won't be constrained by phpmyadmin's upload size restrictions. Most shared hosting I've seen allows it. If not, you may just need to grant permissions for the user#host in phpmyadmin (or whatever the interface is).
The dump file created by mysqldump is just a set of SQL statements that will rebuild your tables.
To load it in in chunks I'd recommend either dumping it out in sets of tables and loading them one by one or if required the dump file should be roughly in the same (pseudo) format:
Set things up ready for loading
CREATE TABLE t1;
INSERT INTO TABLE t1...;
INSERT INTO TABLE t1...;
CREATE TABLE t2;
INSERT INTO TABLE t2...;
INSERT INTO TABLE t2...;
Finalise stuff after loading
You can manually split the file up by keeping the commands at the start and finish and just choosing blocks for individual tables by looking for their CREATE TABLE statements.

Meaning of 'Default Database' in MySQL

I was going through the specifications for loading data into and from MySQL tables at dev.mysql, when I came across the specifications for the local option in the load infile data command. It says that if local is not used, then if a file name with no leading components is given, the server looks for the file in the database directory of the default database. Can anyone tell me what is meant by default database here, and how to set one? Can it be set from within MySQL itself, or through some server directive?
The default database is the one you called with a USE clause or specified at login time. If you use SELECT * FROM tablename as opposed to SELECT * FROM databasename.tablename you also use the default database.
Edit
Just to make that clear: The default database is not a static thing - it is defined only for a defined point in time on a defined session - e.g. such as the PIT and session where you start the load data infile command.
Generally the default database will be specified in a database param under the [client] header in your config (my.ini/my.cnf/etc), like such:
[client]
database = name_of_default_db