Related
I recently tried to put my web application and mysql database on the same virtual server. Now I receive a port error. In order to fix the port issue, I want to back up the database and reinstall WAMP. Since I can't get to the phymyadmin, I have to use the mysql command prompt to do a dump.
Would anyone happen to know the EXACT command to put in, if my username is root, I have no password, and the database name is dev? I've tried it multiple ways and I get an error saying that my syntax is wrong.
Thanks in advance.
You can use the following command provided you know the name of the database:
mysqldump -u root -p[root_password] [database_name] > dumpfilename.sql
Alternatively I would install the MySQL Workbench which can connect to your local database instance and provides a GUI to run the export: MySQL Workbench.
Unfortunately if you've uninstalled WAMP then MySQL might not be running anymore, and the above commands will not work because the utilities cannot see the database. If this is the case, try installing WAMP to a new directory and manually copying over the MySQL data files:
How to restore MySQL database from WAMP?
mysqldump -u root -p dev > devBackUp.sql
DOS batch file to export the database with date and time in the filename
FOR /f "tokens=1-8 delims=:./ " %%G IN ("%date%_%time%") DO (
SET dt=%%G%%H%%I_%%J_%%K
)
mysqldump -u root -p[root_password] [database_name]> [database_name]_%dt%.sql
pause
This is a tricky one, I have the following output:
mysqldump: Got error: 1045: Access denied for user 'root'#'localhost' (using password: YES) when trying to connect
When attempting to export my database with mysqldump on Windows XP. The username is root, the password is correct and contains only alphanumeric characters. I have tried different cases, with/without quotes, specifying using -u and -p, specifying using --user= and --password= and other methods of specifying user/passwords etc, specifying the host (it's all local) and even specifying the database using --databases instead of just blank. The error is always the same when using a password and always the same except the "NO" message when without. I have tried many fixes found through searches with no success. One fix suggested inspecting mysql.conf, but the Windows build doesn't seem to have one. The credentials (and indeed commandline parameters) work perfectly with mysql.exe - this problem only seems to be affecting mysqldump.exe.
This worked for me
mysqldump -u root -p mydbscheme > mydbscheme_dump.sql
after issuing the command it asks for a password:
Enter password:
entering the password will make the dump file.
If you're able to connect to the database using mysql, but you get an error for mysqldump, then the problem may be that you lack privileges to lock the table.
Try the --single-transaction option in that case.
mysqldump -h database.example.com -u mydbuser -p mydatabase --single-transaction > /home/mylinuxuser/mydatabase.sql
Try to remove the space when using the -p-option. This works for my OSX and Linux mysqldump:
mysqldump -u user -ppassword ...
The access being denied is probably to the Windows file system not to the MySQL database; try redirecting the output file to a location where your account is allowed to create files.
You need to put backslashes in your password that contain shell metacharacters, such as !#'"`&;
Don't enter the password with command. Just enter,
mysqldump -u <username> -p <db_name> > <backup_file>.sql
Then you will get a prompt to enter password.
Access dined problem solved when I run command prompt in Administrator mode.
Go to Start-> All Programs -> Accessories right click on Command Prompt clickc on Run as.. Select The Following User select administrator username from select option enter password if any click OK button.
Example 1: For entire database backup in mysql using command prompt.
In Windows 7 and 8
C:\Program Files <x86>>\MySQL\MySQL Server 5.5\bin>mysqldump test -u root -p >testDB.sql
Enter Password: *********
In Windows xp
C:\Program Files\MySQL\MySQL Server 5.5\bin>mysqldump test -u root -p >testDB.sql
Enter Password: *********
It asks password for credentials enter password and click on Enter button.
Example 2: For specific table backup / dump in mysql using command prompt.
In Windows 7 and 8
C:\Program Files <x86>>\MySQL\MySQL Server 5.5\bin>mysqldump test -u root -p images>testDB_Images.sql
Enter Password: *********
In Windows xp
C:\Program Files\MySQL\MySQL Server 5.5\bin>mysqldump test -u root -p images>testDB_Images.sql
Enter Password: *********
Dumpt file will be created under folder
In windows xp
C:\Program Files\MySQL\MySQL Server 5.5\bin
In windows 7 and 8
C:\Program Files (x86)\MySQL\MySQL Server 5.5\bin
Note: Check MySQL installation folder in Windows 7, 8 while run in command prompt. If MySQLWorkbench is 32 bit version it is installed in Program Files (x86) folder other wise Program Files folder.
Put The GRANT privileges:
GRANT ALL PRIVILEGES ON mydb.* TO 'username'#'%' IDENTIFIED BY 'password';
mysqldump -h hostname -u username -P port -B database --no-create-info -p > output.sql
I think you should specify the args
Doing without the -u and -p worked for me (when I was logged in as root):
mysqldump --opt mydbname > mydbname.sql
I was having the same issue, for 30min! I found that I was using _p instead of -p, the terminal font confused me!
Putting -p as the first option worked for me on Windows Server 2012R2 (in cmd.exe as Admin).
mysqldump.exe –p --user=root --databases DBname --result-file=C:\DBname.sql
I just ran into this after a fresh install of MySQL 5.6.16.
Oddly, it works without the password specified or flagged:
mysqldump -u root myschema mytable > dump.sql
mysqldump -u (user) -p(passwd) -h (host_or_IP) database_to_backup > backup_file.sql
example:
mysqldump -u god -pheaven -h 10.0.10.10 accounting > accounting_20141209.sql
this would create sql backup file for the accounting database on server 10.0.10.10. Sometimes your error is seen when localhost is not in config. Designating ip of server may help.
I had to remove the single ticks after the password flag:
--password=mypassword
and NOT
--password='mypassword'
Mysql replies with Access Denied with correct credentials when the mysql account has REQUIRE SSL on
The ssl_ca file (at a minimum) had to be provided in the connection paramiters.
Additional ssl parameters might be required and are documented here: http://dev.mysql.com/doc/refman/5.7/en/secure-connection-options.html
Also posted here https://stackoverflow.com/a/39626932/1695680
For MAMP PRO users (or anyone who's mysql is in a weird location) be prepared to specify the mysql full path from the boonies and also specify full path to your user local folder where you want to dump the file or you'll get the "permission denied error"..
Following worked for me after 3 hours of research:
/Applications/MAMP/Library/bin/mysqldump -u root -proot YOUR_DB > /Users/YOUR_USER/yourdump2.sql
In my case, I could access correctly with mysql.exe but not with mysqldump.exe.
The problem was the port for my connection was not the default one (3306) and I had to put the mysqldump port work with (-P3307)
mysqldump -u root -p -P3307 my_database > /path/backup_database
This is the solution that worked for me
mysqldump -h hostname.com -u username -p'password' database > dump.sql
In Past same problem occurred to me after I copied the mysqldump statement from a MS Word file.
But When typing the statement directly, everything worked fine.
In hex editor the "-" of the not working statement was represented by the unicode char e2 80 93 (http://www.fileformat.info/info/unicode/char/2013/index.htm)
In sort, type password directly and check the copy paste code as the uni-code (or other encoding) strings might cause an issue..
I had the same error for last 2 days. Tried bunch of things. Nothing worked.
But this did work:
Create another user. Grant it everything.
mysqldump -u new_user db_name > db_name.sql //no error
I discovered a running apache process acessing the MYSQL causing this error. So I suggest to ensure that all processes which might interact with the DB are shutdown beforehand.
I had the problem that there were views that had a bad "DEFINER", which is the user that defined the view. The DEFINER used in the view had been removed some time ago as being "root from some random workstation".
Check whether there might be a problem by running:
USE information_schema;
SELECT DEFINER, SECURITY_TYPE FROM views;
I modified the DEFINER (actually, set the DEFINER to root#localhost and the SQL SECURITY value to INVOKER so the view is executed with the permissions of the invoking user instead of the defining user, which actually makes more sense) using ALTER VIEW.
This is tricky as you have to construct the appropriate ALTER VIEW statement from information_schema.views, so check:
Modify DEFINER on many
views
MySQL error 1449: The user specified as a definer does not exist
For me it worked when I omitted the password.
So mysqldump -u user dbname > dump.sql
Tried most of the above with no joy.
Looking at my password, it had characters that might confuse a parser. I wrapped the password in quotes and the error was resolved.
-p"a:##$%^&+6>&FAEH"
Using 8.0
If you want to create a mysql data dump, you can use mysqldump command. Following command will create a sql file called xxx.sql at the same location from where this command is run. xxx.sql will have all the necessary sqls to replicate exactly same db schema in any other mysql database.
Command is : mysqldump -u root -ppassword --databases database Name you want to import > xxx.sql
Here root is the mysql root user and password is THIS root user's password.
EXAMPLE: If root user password is hello, database name to export is regdb and xxx.sql is the file where you want to export this regdb, command would be like:
mysqldump -u root -phello --databases regdb > xxx.sql
Note: xxx.sql is the file name where this db will get dumped.
This solution might be one of the last to try/least likely to be the culprit, but this was my problem...
My problem was that the directory I was trying to dump to needed admin privileges to write to and that's what was causing the mysqldump command to return "Access Denied".
I set the dump file path to my desktop dir and then it worked.
This was on Windows.
I had the same error. Only occurred after moving from my normal work PC to a PC at a different location.
I had to add my public IP ho address to Remote MySQL in my CPanel at my host site
I got the same error when I ran the command in a directory that I didn't have write access to.
Test your access by creating an empty file in the directory, and see if you get an error.
Here was my error
mysqldump -u root librenms -p > librenms.sql
-bash: librenms.sql: Permission denied
I changed to my home directory and then it worked.
cd ~
mysqldump -u root librenms -p > librenms.sql
Enter password:
Do the equivalent on windows, and it may just fix your problem!
ENSURE YOU TRY REMOVING AND TYPING THE DASH OVER to make sure that you are actually fighting with the right problem.
Be very careful that you actually have a "-". I apparently had some other character that looks very similar. I had a – instead of a -. I had copied the command from somewhere online don't remember where but the point is I spend a lot of time trying to figure it out when I just needed to replace that character.
I have a large (~150mb) sql file that I am trying to import. It is too large to do it through PHPMyAdmin even splitting it into many pieces and it is too large to import through a php script as it times out after 30 seconds of processing the script. So I'm looking for how to directly import the file from MySQL command line.
Searching online shows that I want to either use database_name < file_name.sql or source file_name.sql but I can't get either of these to work.
Using < gives the generic MySQL syntax error while using source give a slightly more promising failed to open file 'file_name.sql', error: 2 so I am inclined to think that the source command is on the right track.
I am in windows and am using xampp as a localhost server (note I'm only trying to import this file on the localhost so that I can execute the sql). I've tried placing the file in xampp\mysql\bin and xampp\mysql\data\database_name.
Any suggestions of how to import this .sql file into MySQL either from the MySQL command line or by any other means would be greatly appreciated.
On Windows this should work (note the forward slash and that the whole path is not quoted and that spaces are allowed)
USE yourdb;
SOURCE D:/My Folder with spaces/Folder/filetoimport.sql;
With xampp I think you need to use the full path at the command line, something like this, perhaps:
C:\xampp\mysql\bin\mysql -u {username} -p {databasename} < file_name.sql
Don't use "source", it's designed to run a small number of sql queries and display the output, not to import large databases.
I use Wamp Developer (not XAMPP) but it should be the same.
What you want to do is use the MySQL Client to do the work for you.
Make sure MySQL is running.
Create your database via phpMyAdmin or the MySQL shell.
Then, run cmd.exe, and change to the directory your sql file is
located in.
Execute: mysql -u root -p database_name_here < dump_file_name_here.sql
Substitute in your database name and dump file name.
Enter your MySQL root account password when prompted (if no password set, remove the "-p" switch).
This assumes that mysql.exe can be located via the environmental path, and that sql file is located in the directory you are running this from. Otherwise, use full paths.
Option 1. you can do this using single cmd where D is my xampp or wampp install folder so i use this where mysql.exe install and second option database name and last is sql file so replace it as your then run this
You can try this:
mysql -u root -p test < /test.sql
Another option
D:\xampp\mysql\bin\mysql.exe -u root -p databse_name < D:\yoursqlfile.sql
Option 1 for wampp
D:\wamp64\bin\mysql\mysql5.7.14\bin\mysql.exe -u root -p databse_name< D:\yoursqlfile.sql
change your folder and mysql version
Option 2 Suppose your current path is which is showing command prompt
C:\Users\shafiq;
then change directory using cd..
then goto your mysql directory where your xampp installed. Then cd.. for change directory. then go to bin folder.
C:\xampp\mysql\bin;
C:\xampp\mysql\bin\mysql -u {username} -p {database password}.then please enter when you see enter password in command prompt.
choose database using
mysql->use test (where database name test)
then put in source sql in bin folder.
then last command will be
mysql-> source test.sql (where test.sql is file name which need to import)
then press enter
This is full command
C:\Users\shafiq;
C:\xampp\mysql\bin
C:\xampp\mysql\bin\mysql -u {username} -p {database password}
mysql-> use test
mysql->source test.sql
C:\xampp\mysql\bin\mysql -u root -p testdatabase < C:\Users\Juan\Desktop\databasebackup.sql
That worked for me to import 400MB file into my database.
For importing a large SQL file using the command line in MySQL.
First go to file path at the command line.
Then,
Option 1:
mysql -u {user_name} -p{password} {database_name} < your_file.sql
It's give a warning mesaage : Using a password on the command line interface can be insecure.
Done.Your file will be imported.
Option 2:
mysql -u {user_name} -p {database_name} < your_file.sql
in this you are not provide sql password then they asked for password just enter password and your file will be imported.
use mysql source command to avoid redirection failures, especially on windows.
mysql [-u <username>] [-p<password>] <databasename> -e "source /path/to/dump.sql"
where e for "Execute command"
On Windows, please remember to use double quote for sql command.
However, either backslash \ or slash / will work on Windows.
Hello I had the same problem but I tried many different states and I came to it:
SOURCE doesn't work with ; at the end in my case:
SOURCE D:\Barname-Narmafzar\computer programming's languages\SQL\MySQL\dataAug-12-2019\dataAug-12-2019.sql;
and the error was:
ERROR: Unknown command '\B'.
'>
it also didn't work with a quotation for the address.
But it works without ; at the end:
SOURCE D:\Barname-Narmafzar\computer programming's languages\SQL\MySQL\dataAug-12-2019\dataAug-12-2019.sql
But remember to use USE database_name; before that.
I think it's so because the SOURCE or USE or HELP are for the Mysql itself and they are not such query codes although when you write HELP it says:
"Note that all text commands must be first on line and end with ; ".
but here doesn't work.
I should say that I have done it in CMD and I didn't try it in Mysql Workbench.
That was it
This is the result
On my Xampp set-up I was able to use the following to import a database into MySQL:
C:\xampp\mysql\bin\mysql -u {username goes here} -p {leave password blank} {database name} < /path/to/file.sql [enter]
My personal experience on my local machine was as follows:
Username: Root
Database Name: testdatabase
SQL File Location: databasebackup.sql is located on my desktop
C:\xampp\mysql\bin\mysql -u root -p testdatabase < C:\Users\Juan\Desktop\databasebackup.sql
That worked for me to import my 1GB+ file into my database.
Username as root without password
mysql -h localhost -u root databasename < dump.sql
I have faced the problem on my local host as i don't have any password for root user. You can use it without -p password as above. If it ask for password, just hit enter.
On windows:
Use explorer to navigate to the folder with the .sql file.
Type cmd in the top address bar. Cmd will open.
Type:
"C:\path\to\mysql.exe" -u "your_username" -p "your password" < "name_of_your_sql_file.sql"
Wait a bit and the sql file will have been executed on your database.
Confirmed to work with MariaDB in feb 2018.
How can I import a database with mysql from terminal?
I cannot find the exact syntax.
Assuming you're on a Linux or Windows console:
Prompt for password:
mysql -u <username> -p <databasename> < <filename.sql>
Enter password directly (not secure):
mysql -u <username> -p<PlainPassword> <databasename> < <filename.sql>
Example:
mysql -u root -p wp_users < wp_users.sql
mysql -u root -pPassword123 wp_users < wp_users.sql
See also:
4.5.1.5. Executing SQL Statements from a Text File
Note: If you are on windows then you will have to cd (change directory) to your MySQL/bin directory inside the CMD before executing the command.
Preferable way for windows:
Open the console and start the interactive MySQL mode
use <name_of_your_database>;
source <path_of_your_.sql>
mysql -u <USERNAME> -p <DB NAME> < <dump file path>
-u - for Username
-p - to prompt the Password
Eg. mysql -u root -p mydb < /home/db_backup.sql
You can also provide password preceded by -p but for the security reasons it is not suggestible. The password will appear on the command itself rather masked.
Directly from var/www/html
mysql -u username -p database_name < /path/to/file.sql
From within mysql:
mysql> use db_name;
mysql> source backup-file.sql
Open Terminal Then
mysql -u root -p
eg:- mysql -u shabeer -p
After That Create a Database
mysql> create database "Name";
eg:- create database INVESTOR;
Then Select That New Database "INVESTOR"
mysql> USE INVESTOR;
Select the path of sql file from machine
mysql> source /home/shabeer/Desktop/new_file.sql;
Then press enter and wait for some times if it's all executed then
mysql> exit
From Terminal:
mysql -uroot -p --default-character-set=utf8 database_name </database_path/database.sql
in the terminal type
mysql -uroot -p1234; use databasename; source /path/filename.sql
Below command is working on ubuntu 16.04, I am not sure it is working or not other Linux platforms.
Export SQL file:
$ mysqldump -u [user_name] -p [database_name] > [database_name.sql]
Example : mysqldump -u root -p max_development > max_development.sql
Import SQL file:
$ mysqldump -u [user_name] -p [database_name] < [file_name.sql]
Example: mysqldump -u root -p max_production < max_development.sql
Note SQL file should exist same directory
I usually use this command to load my SQL data when divided in files with names : 000-tableA.sql, 001-tableB.sql, 002-tableC.sql.
for anyvar in *.sql; do <path to your bin>/mysql -u<username> -p<password> <database name> < $anyvar; done
Works well on OSX shell.
Explanation:
First create a database or use an existing database. In my case, I am using an existing database
Load the database by giving <name of database> = ClassicModels in my case and using the operator < give the path to the database = sakila-data.sql
By running show tables, I get the list of tables as you can see.
Note : In my case I got an error 1062, because I am trying to load the same thing again.
mysql -u username -ppassword dbname < /path/file-name.sql
example
mysql -u root -proot product < /home/myPC/Downloads/tbl_product.sql
Use this from terminal
After struggling for sometime I found the information in https://tommcfarlin.com/importing-a-large-database/
Connect to Mysql (let's use root for both username and password):
mysql -uroot -proot
Connect to the database (let's say it is called emptyDatabase (your should get a confirmation message):
connect emptyDatabase
3 Import the source code, lets say the file is called mySource.sql and it is in a folder called mySoureDb under the profile of a user called myUser:
source /Users/myUser/mySourceDB/mySource.sql
Open the MySQL Command Line Client and type in your password
Change to the database you want to use for importing the .sql file data into. Do this by typing:
USE your_database_name
Now locate the .sql file you want to execute.
If the file is located in the main local C: drive directory and the .sql script file name is currentSqlTable.sql, you would type the following:
\. C:\currentSqlTable.sql
and press Enter to execute the SQL script file.
If you are using sakila-db from mysql website,
It's very easy on the Linux platform just follow the below-mentioned steps, After downloading the zip file of sakila-db, extract it. Now you will have two files, one is sakila-schema.sql and the other one is sakila-data.sql.
Open terminal
Enter command mysql -u root -p < sakila-schema.sql
Enter command mysql -u root -p < sakila-data.sql
Now enter command mysql -u root -p and enter your password, now you have entered into mysql system with default database.
To use sakila database, use this command use sakila;
To see tables in sakila-db, use show tables command
Please take care that extracted files are present in home directory.
First connect to mysql via command line
mysql -u root -p
Enter MySQL PW
Select target DB name
use <db_name>
Select your db file for import
SET autocommit=0; source /root/<db_file>;
commit;
This should do it. (thanks for clearing)
This will work even 10GB DB can be imported successfully this way. :)
In Ubuntu, from MySQL monitor, you have already used this syntax:
mysql> use <dbname>
-> The USE statement tells MySQL to use dbname as the default database for subsequent statements
mysql> source <file-path>
for example:
mysql> use phonebook;
mysql> source /tmp/phonebook.sql;
Important: make sure the sql file is in a directory that mysql can access to like /tmp
If you want to import a database from a SQL dump which might have "use" statements in it, I recommend to use the "-o" option as a safeguard to not accidentially import to a wrong database.
• --one-database, -o
Ignore statements except those those that occur while the default
database is the one named on the command line. This filtering is
limited, and based only on USE statements. This is useful for
skipping updates to other databases in the binary log.
Full command:
mysql -u <username> -p -o <databasename> < <filename.sql>
For Ubuntu/Linux users,
Extract the SQL file and paste it somewhere
e.g you pasted on desktop
open the terminal
go to your database and create a database name
Create database db_name;
Exit Mysql from your terminal
cd DESKTOP
mysql -u root -p db_name < /cd/to/mysql.sql
Enter the password:....
Before running the commands on the terminal you have to make sure that you have MySQL installed on your terminal.
You can use the following command to install it:
sudo apt-get update
sudo apt-get install mysql-server
Refrence here.
After that you can use the following commands to import a database:
mysql -u <username> -p <databasename> < <filename.sql>
The simplest way to import a database in your MYSQL from the terminal is done by the below-mentioned process -
mysql -u root -p root database_name < path to your .sql file
What I'm doing above is:
Entering to mysql with my username and password (here it is root & root)
After entering the password I'm giving the name of database where I want to import my .sql file. Please make sure the database already exists in your MYSQL
The database name is followed by < and then path to your .sql file. For example, if my file is stored in Desktop, the path will be /home/Desktop/db.sql
That's it. Once you've done all this, press enter and wait for your .sql file to get uploaded to the respective database
There has to be no space between -p and password
mysql -u [dbusername] -p[dbpassword] [databasename] < /home/serverusername/public_html/restore_db/database_file.sql
I always use it, it works perfectly. Thanks to ask this question. Have a great day. Njoy :)
I'm trying to migrate a mysql database from a server with phpMyAdmin to one that doesn't. I have a .sql file exported from the phpMyAdmin server, and am not quite sure where to go from there. While searching for an answer, I keep on finding websites that say to use this command:
mysql -u USER -p DBNAME < dump.sql
but nowhere describes WHERE that file should be located. Is it just supposed to be in the var/lib/mysql directory?
Thanks for the help.
It can be anywhere. Use full path if not in the current directory
mysql -u USER -p DBNAME <
/home/dump.sql
(if the file is in /home/ folder)
EDIT: Thanks for the correction, TehShrike.
You may follow -p with the password for user USER -- in which case there is no space between the option p and the password. Otherwise, mysql will prompt you for password. So the syntax is like the following:
mysql -u <user> -p<password> <database-name> < <path-to-mysqldump-file>
If the database does not already exist, you will have to login to your mysql server and create it first (Before the above line):
mysql -u <user> -p <password>
create database <database-name>