I am trying to back up my database and have spent a good hour trying to find the file I'm creating.
Using the command prompt
pg_dump -U postgres mydb
If I try specify a location
pg_dump -U postgres mydb > "C:\Users\MyName\Desktop\backup"
It says Access Denied
Without specifying an output file, your dump should be going to standard out.
As for the example writing to the desktop - does your OS user have permissions to write to that location? If so, you should post exactly what you are running and your exact error message.
Also, rather than using redirection, you can use the -f flag to pass in the where the output file should be created. I highly recommend reading through the documentation for pg_dump.
Related
Using: MySQL 5.7
What I want to achieve:
To save console output of Cloud SQL output as a text.
Error:
Cloud SQL returns this:
ERROR 1045 (28000): Access denied for user 'root'#'%' (using password: YES)
Things I tried:
Logging in with no password → asks password anyways, and any password including that of the server itself does not work.
Creating various users with password → same error result
Creating a Cloud SQL instance with skip-grant-tables so that no permission is required to modify the table → Cloud SQL does not support this flag
I tried manually flagging the database with this option, but Cloud Shell doesn’t even support root login without password.
Possible solution:
If I can: mysql -u root with Cloud SQL with no password, then it should be able to do this just fine. It seems that any user besides root cannot even login to the instance.
Thank you in advance. Any clues / help is appreciated.
I believe the most trivial solution is to use the Google Cloud SDK with the following command.
You will export the results of the query in CSV format to Google Cloud Storage bucket, and copy them from the bucket to your system. Then you’ll have to parse the CSV file which is a standard procedure.
There’s an how-to guide here and you can take a look at a concrete example below:
Have some variables that will be used in multiple commands
INSTANCE_ID=your_cloud_sql_instance_id
BUCKET=gs://your_bucket here
Create bucket if you don’t have one, choosing the location accordingly
gsutil mb -l EU -p $DEVSHELL_PROJECT_ID $BUCKET
You can read the explanation of the following commands in the documentation 2, but bottom line will have a csv file on your file system at the end. Also make sure to edit the name of the DATABASE variable below as well as the correspondent query.
gsutil acl ch -u `gcloud sql instances describe $INSTANCE_ID --format json | jq -c ".serviceAccountEmailAddress" | cut -d \" -f 2`:W $BUCKET
DATABASE=db_visit
FILENAME=$DATABASE'_'`date +%Y%m%d%H%M%Y`_query.csv
gcloud beta sql export csv $INSTANCE_ID $BUCKET/$FILENAME --database=$DATABASE --query="select * from visit"
gsutil cp $BUCKET/$FILENAME .
To automate the login through mysql client and make subsequent queries and get its output I encourage you to research a solution along the lines of pexpect.
What I've been trying to do is to send some data from MySQL to other computers.
I searched the internet for a solution and the best way is probably to put the data into a file, by using the export function.
Soon I encountered an error. Here is part of the log.
Running: "C:\Program Files (x86)\MySQL\MySQL Server 5.6\bin\mysqldump.exe"
--defaults-file="c:\users\takaha\appdata\local\temp\tmpx2hr0e.cnf"
--enable-cleartext-plugin --user=root --host=localhost --protocol=tcp
--port=3306 --default-character-set=utf8 --events --skip-triggers "db_student_comments"
mysqldump: unknown option '--enable-cleartext-plugin'
Operation failed with exitcode 2
14:44:03 Export of C:\Users\takaha\Documents\dumps\Dump20150212.sql has finished with 1 errors
Apparently, this error is similar to this error, but trying the first and second advice on this page didn't solve it, and I've spent more than two days in this problem so far.
I'll put a image of my cmd, since using cmd was recommended at other Stackoverflow pages. This image shows the dumping seems to have successfully been done, but I don't know where the file is. (The "dump" folder was automatically created in C:\Users\username\Documents, but this folder is empty.)
I'd appreciate any advice.
EDIT
Thanks to Danyal, this was solved.
All I had to do was to exactly indicate the directory to create the file after ">".
why don't you use this command?
mysqldump -u'username' -p'password' database_name > back_up.sql
You need to have mysql in the environment variable. If it isn't there, you can go to mysql's bin directory and can execute this command.
Per the docs:
The mysql, mysqladmin, and mysqlslap client programs support an --enable-cleartext-plugin option that enables the plugin on a per-invocation basis.
It does not appear to be a valid option for mysqldump. You shouldn't need it at all unless you're using non-standard MySQL authentication.
I successfully created a mysqldump file myDump.sql of a myDb1 database using guidelines from this thread. Also I created a second database myDb2, navigated to the directory containing myDump.sql and trying to restore it into the new database myDb2 but failing, Two methods I tried:
> mysql -u root -p myDb2 < myDump.sql;
> -- entered password
and:
> mysql -u root -p
mysql> -- entered password
mysql> USE myDb2;
mysql> SOURCE myDump.sql;
Both have the same error message:
ERROR:
ASCII '\0' appeared in the statement, but this is not allowed unless option --binary-mode is enabled and mysql is run in
non-interactive mode. Set --binary-mode to 1 if ASCII '\0' is expected. Query: ' ■-'.
I'd also like to know if I need to use the same database name as the old db for the new one. I tried with a different and same names, but with this same result error.
This is probably caused by coding systems.
My dump file is generated using redirection (">") in powershell and I encountered the same problem. The output redirection generated a file with UTF-16 Little endian.
However, this can be solved by converting the dumpfile into utf-8. This can be done in emacs as:
M-x set-buffer-file-coding-system
Then save the file and import again.
The coding system of a file can be detected using GNU "file" utility, and it also available in windows and can be found here: http://gnuwin32.sourceforge.net/packages/file.htm
For future use, a better dump command like:
mysqldump <dbname> -r <filename>
Check the myDump.sql file, it maybe a coding error in the file. These garbage characters cause this problem. Delete the garbage characters to solve the issue.
Open with Sequel Pro shows this
`í}k¯]ÇÝçðWÜ?øy«««_%c�sè;¶`Ìô hãEE¤"8Áü÷ô>ûqzW¯:êmX0`²¸yyëÔºµë¹êGw?û+þ{ð£»g¯ÿçw¯¿ºû/ß¾¹{ö/ï^}÷§oªô__ûöË7_ß'éÁªà¿¿{÷ÍÇ}ôý÷ßOo/ãoßL_¼ùÓG×?ûâÍ«×Óß¼ùãW¯/òÍGË?`
A mysqldump command like the following:
mysqldump -u<username> -p<password> -h<remote_db_host> -T<target_directory> <db_name> --fields-terminated-by=,
will write out two files for each table (one is the schema, the other is CSV table data). To get CSV output you must specify a target directory (with -T). When -T is passed to mysqldump, it writes the data to the filesystem of the server where mysqld is running - NOT the system where the command is issued.
Is there an easy way to dump CSV files from a remote system ?
Note: I am familiar with using a simple mysqldump and handling the STDOUT output, but I don't know of a way to get CSV table data that way without doing some substantial parsing. In this case I will use the -X option and dump xml.
mysql -h remote_host -e "SELECT * FROM my_schema.my_table" --batch --silent > my_file.csv
I want to add to codeman's answer. It worked but needed about 30 minutes of tweaking for my needs.
My webserver uses centos 6/cpanel and the flags and sequence which codeman used above did not work for me and I had to rearrange and use different flags, etc.
Also, I used this for a local file dump, its not just useful for remote DBs, because I had too many issues with selinux and mysql user permissions for SELECT INTO OUTFILE commands, etc.
What worked on my Centos+Cpanel Server
mysql -B -s -uUSERNAME -pPASSWORD < query.sql > /path/to/myfile.txt
Caveats
No Column Names
I cant get column names to appear at the top. I tried adding the flag:
--column-names
but it made no difference. I am still stuck on this one. I currently add it to the file after processing.
Selecting a Database
For some reason, I couldn't include the database name in the commandline. I tried with
-D databasename
in the commandline but I kept getting permission errors, so I ended using the following the top of my query.sql:
USE database_name;
On many systems, MySQL runs as a distinct user (such as user "mysql") and your mysqldump will fail if the MySQL user does not have write permissions in the dump directory - it doesn't matter what your own write permissions are in that directory. Changing your directory (at least temporarily) to world-writable (777) will often fix your export problem.
I created a data base using mysql. I used MySQLDump to create one database backup file in text format (MySql 5.5 on Windows XP). The database is local on my machine (local host).
I am having trouble using the MySQL command to load the dump file to restore the database. I have done the following:
Research stack overflow for how to do it. I noticed there's a bug using the MySQL command to restore the data from a post. Before I run the command, I DROP the database and CREATE the database using MySQL workbench.
I type the following command in the DOS prompt to restore the database:
mysql -u root -p -h localhost -D matlab_data -o < backup.sql
backup.sql is a the backup file in text format created by MySqlDump.
I am then asked for the password which I enter. I get the DOS prompt right away with no error message. I've waited several hours for the command to run and the database is still empty.
I have tried various command formats over the last few days. If I enter incorrect data in the command line (non existen file, database, etc), I get an error message.
I feel I would not see the DOS prompt until the database is restored. If I don't DROP and CREATE the database, I get an error message. Otherwise, not.
Does anybody have any idea what the issue is? I realize that I could be making a stupid mistake.
Thank you for your help.
shell into the mysql console and run the sql file as this
If you are already running mysql, you can execute an SQL script file using the source command or . command:
mysql> source file_name
mysql> \. file_name
note that file_name must be an absolut path