user inside the plugable DB(PDB21) username -'JEWBDEV' and
PWD-abc123, i am not able to export backup .dmp file please suggest
steps.
Create datapump directory and grant permissions to user
SQL> CREATE or REPLACE DIRECTORY dpump_dir as '/home/user1/dumpfiles';
SQL> GRANT READ, WRITE ON DIRECTORY dpump_dir TO JEWBDEV;
Set the environment (outside sqlplus):
setenv DATA_PUMP_DIR DPUMP_DIR
Export table (outside sqlplus):
expdp JEWBDEV/abc123#pdb21 tables=TABLE1 dumpfile=table1.dmp logfile=jewbdev_exp.log
Note, in above examples some of the values like TABLE1, /home/user1/dumpfiles are written for example, it needs to be changed according to your requirement.
Export everything under a schema/user (outside sqlplus):
To export everything under a user you can skip giving the tablenames, following is the format:
expdp system/manager#<pluggable_database> file=<user>.dmp owner=<user>
Example:
expdp system/manager#pdb21 file=JEWBDEV.dmp owner=JEWBDEV
Also, check this SO question to get better understanding on expdp.
Working example
Following steps are verified at my end:
SQL> show pdbs
CON_ID CON_NAME OPEN MODE RESTRICTED
---------- ------------------------------ ---------- ----------
2 PDB$SEED READ ONLY NO
3 CDB1_PDB1 READ WRITE NO
SQL> alter session set container=cdb1_pdb1;
Session altered.
SQL> CREATE or REPLACE DIRECTORY dpump_dir as '$T_WORK';
Directory created.
Following should be run outside sqlplus:
expdp system/manager#cdb1_pdb1 file=scott.dmp owner=SCOTT
In your case it will be:
expdp system/manager#cdb1_pdb21 file=JEWBDEV.dmp owner=JEWBDEV
So, make sure your pdb name is CDB1_PDB21 when you query show pdbs. If the PDB name is PDB21 then the connect string should change like the following:
expdp system/manager#pdb21 file=JEWBDEV.dmp owner=JEWBDEV
Also, note that we are exporting the user/schema JEWBDEV with SYSTEM user.
Related
What's wrong with this code, I want to insert an image to table, but when I was executed this code the result of image field is NULL.
I try with MySQL Workbench executing:
CREATE TABLE image(keyh int, img blob);
INSERT INTO image VALUES(1, load_file('d:\Picture\cppLogo.png'));
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.
If the file does not exist or cannot be read because one of the
preceding conditions is not satisfied, the function returns NULL.
http://dev.mysql.com/doc/refman/5.7/en/string-functions.html#function_load-file
What can you do?
Check which user mysql is running with, and make sure the file is readable by that user. Make sure the security settings allow the file to be read and it is not of greater size than max_allowed_packet.
See SHOW VARIABLES LIKE 'max_allowed_packet'.
For me, it looks like the file is on your localhost and you try to upload it. This is not possible using LOAD_FILE(). The file must be already on the server.
The issue can also be caused by your windows directory seperator character \ (like RiggsFolly said), which is used for escaping instead, switch to unix style / then:
LOAD_FILE('D:/Picture/cppLogo.png')
Or your Image is of greater filesize than a BLOB field can hold, like Balazs Vago said.
i was found the correct syntax is following this:
C:/wamp/binsql5.5.20/data/56VRLRFE.jpg'
not this
C:\wamp\binsql5.5.20\data\56VRLRFE.jpg'
thanks guys for all your Answer :D
Open your MySql Command Line Client and login with root user and type
mysql> SHOW VARIABLES LIKE "secure_file_priv";
this will show you the secure path used by MySql to access the files. something like
+------------------+-----------------------+
| Variable_name | Value |
+------------------+-----------------------+
| secure_file_priv | /var/lib/mysql-files/ |
+------------------+-----------------------+
you can either paste files inside this folder or change the "secure_file_priv" variable value to "empty string" so that it can read file from anywhere.
On Windows the fundamental problem is that MySql, by default, runs as a Windows service under the Network account which means that there are only a few file locations the server can access. Thus for load_file to work, the file must be placed in a folder on the server which can be read by the service. There seems to be no documentation on this. In my investigation the only folder that works with load_file is C:\ProgramData\MySQL\MySQL Server 8.0\Uploads
Run a query to test the load...
select load_file('C:\\ProgramData\\MySQL\\MySQL Server 8.0\\Uploads\\1.txt') ;
Note on windows you have to use either double \ or / to separate the path elements. This will return NULL on failure, otherwise the contents of the file.
Assume now a table named db.image with columns source and image. Source is character and image is blob. The command to load a.jpg into the table would be
insert into db.image (source,image) values ('a.jpg',load_file('c:/programdata/mysql/mysql server 8.0/uploads/a.jpg'));
Store Directly Without folder name for example-
create table myimg(id int, image mediumblob);
insert into myimg values(101, load_file("E://xyz.png"));
I have a MySQL database from which I am trying to export data using
SELECT...INTO OUTFILE...FROM
into the My Documents folder on Windows. I would like the code to work in any computer and I am stuck on how to write the file path.
I tried
SELECT...INTO OUTFILE 'C:/Users/%username%/Documents/filename.txt'...
and I failed. I looked for a solution in the Internet without finding one.
Can someone help me please! Thanks in advance.
It depends on what context it is running in. It is not going to appear on a user box (unless the user box is the server). On a server, without a path, it gets plopped in the dir represented by the value seen thru
show variables where variable_name ='datadir';
Then, under that dir, in the dir that represents the database dir. So for me right now that would be
C:\ProgramData\MySQL\MySQL Server 5.6\data\so_gibberish
as so_gibberish is my db name.
If, on my server, I wanted it to go to my dev dir off the root, it would be either
select * from t1 into outfile 'c:\\dev\\t99t.txt';
Note the escape for Windows above.
or
select * from t1 into outfile '/dev/t99t.txt';
Linux.
If the context it is running in is command line, then you have a chance to pick up such things as environment variables.
If the context (a slightly different select stmt, not an into outfile) is PHP/Java (whatever) on a client box pointing to a separate server box, then perhaps prompt them for the dirpath (test you can write there), and proceed. As such that client would get result sets and do fopens and fwrites.
I cannot run source table_name.sql
when I try, I get the following error:
ERROR:
Failed to open file 'cars.sql', error: 2
I have been following zeetcode:
http://zetcode.com/databases/mysqltutorial/introduction/#mysql
http://zetcode.com/databases/mysqltutorial/firststeps/
The first provides a list of commands to create a database called mydb and a set of tables to be used in the tutorial including one named Cars.
The second link shows you how to access the databases (SHOW DATABASES;) which I could do but when I go to the previously created database mydb, I can see the previously created tables including Cars (even though the tutorial says I should not see anything.
when I follow the next command: source cars.sql, I receive the error above
yet, this query works:
mysql> SELECT * FROM Cars;
any ideas as to why the source function would not work?
This is the first time I am working with mysql.
where is the file cars.sql located? you may need to specify the full path to the file in order for it to be located by mysql.
I want to store images and .docx/.doc, .pptx/.ppt, .pdf files using the front end of my software. I don't understand how to implement this and how to insert the BLOB and CLOB files into the table. Please help.
I am using Kubuntu 11.04, MySQL5, Qt 4.7.3.
Two ways:
1 - Use a LOAD_FILE function -
INSERT INTO table1 VALUES(1, LOAD_FILE('data.png'));
2 - Insert file as hex string, e.g. -
INSERT INTO table1 VALUES
(1, x'89504E470D0A1A0A0000000D494844520000001000000010080200000090916836000000017352474200AECE1CE90000000467414D410000B18F0BFC6105000000097048597300000EC300000EC301C76FA8640000001E49444154384F6350DAE843126220493550F1A80662426C349406472801006AC91F1040F796BD0000000049454E44AE426082');
INSERT INTO MY_TABLE(id, blob_col) VALUES(1, LOAD_FILE('/full/path/to/file/myfile.png')
LOAD_FILE has many conditions attached to it. From the MySQL documentation:
LOAD_FILE(file_name)
Reads the file and returns the file contents as a string. 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.
If the file does not exist or cannot be read because one of the
preceding conditions is not satisfied, the function returns NULL.
Also, there there are bugs with LOAD_FILE in Linux. See http://bugs.mysql.com/bug.php?id=38403 for the bug, and MySQL LOAD_FILE returning NULL for workarounds. On Ubuntu 12.04, MySQL 5.5.32, this works for me:
Copy file to /tmp
Change ownership to mysql user chown mysql:mysql /tmp/yourfile
Log into mysql as mysql root user so you are sure you have FILE privilege
Run your insert statement
Or you could merely use the MySQL Workbench, select the rows, last rows, insert a row without the blob, then just right click and select "Load Value From File".
INSERT INTO table1 VALUES(1, LOAD_FILE(data.png));
won't work but
INSERT INTO table1 VALUES(1, LOAD_FILE('data.png'));
should (assuming data.png exists in the local directory)
for those People who are getting "Column 'image' cannot be null" error while saving Blob through query :-
Open your MySql Command Line Client and login with root user and type
mysql> SHOW VARIABLES LIKE "secure_file_priv";
this will show you the secure path used by MySql to access the files. something like
+------------------+-----------------------+
| Variable_name | Value |
+------------------+-----------------------+
| secure_file_priv | /var/lib/mysql-files/ |
+------------------+-----------------------+
you can either paste files inside this folder or change the "secure_file_priv" variable value to "empty string" so that it can read file from anywhere.
If you are using mysql workbench, just right click on the field (cell) and select 'load value from file' option and then browse to the file and click open and then click on apply. It will automatically generate query like this
UPDATE `dbname`.`tablename` SET `columnname` = ? WHERE (`row` = '1');
How do I do such a thing?
In mysql I do:
SELECT LOAD_FILE('/path/to/file');
What about postgres? Without using the \copy command of psql?
That depends what you want to do exactly.
You have COPY for reading structured data into (temporary) tables.
Note that this is the SQL command, which is similar, but not the same as the \copy command of psql!
And there is pg_read_file() for reading in any text file.
Edit - a basic example:
CREATE FUNCTION f_showfile(myfile text)
RETURNS text AS
$x$
BEGIN
RETURN pg_read_file(myfile, 0, 1000000); -- 1 MB max.
-- or you could read into a text var and do stuff with it.
END;
$x$
LANGUAGE plpgsql VOLATILE;
Only superusers can use this function. Be careful not to open security holes. You could create a function with SECURITY DEFINER, REVOKE FROMpublic and GRANT TO selected roles. If security is an issue read this paragraph at the provided link:
Writing SECURITY DEFINER Functions Safely
pg_read_file() you can only read from the logfile dir and the database dir. On Linux you could create a symlink to a data dir (at a safe location) like this:
cd /path//my/database
ln -s /var/lib/postgresql/text_dir/ .
Then call like this:
SELECT f_showfile('text_dir/readme.txt');
Output:
f_showfile
-------------------------------------------------------------------------
This is my text from a file.