MySQL: How to import a sql dump.. inside mysql command line? - mysql

I often keep a shell open to a remote server where I'm spending a lot of time in mysql. Exiting mysql and logging back in seems like a pain I shouldn't have to deal with if I just want to run a sql file.
When I'm running MySql from the command line, how can I run a dump file?
Right now, I'm using the same approach outlined in this post.

mysql> use db_name;
mysql> source backup-file.sql;
You may want to take a look at this answer: https://stackoverflow.com/a/17666285

Related

MySQL Running SQL Script error - [WinError 32] The process cannot access the file because it is being used by another process:

I am getting an error while running a SQL script to load data. Error is pasted below:
Preparing...
[WinError 32] The process cannot access the file because it is being used by another process: 'C:\\Users\\PRATIK~1\\AppData\\Local\\Temp\\tmpf75l0wi5.cnf'
I have tried uninstalling and installing MySQL several times but nothing is helping.
I faced the same issues while trying to run a MySQL script. I tried to find the process in the temp folder and removed it and tried again but the process seems to start again and appears in the temp folder. I could not run the script, however, I found a workaround, instead of running the script try to open it and run it in the query editor.
Just downgrade your MySQL workbench version.
In my case I downgraded the version from
8.0.25 to 8.0.20.
The sounds like you already had tried that script execution before and stopped it without stopping the mysqld process. So this process (which does the actual import) still holds a file lock on the temporary config file.
Try removing that file and check that all MySQL processes that you don't want are stopped. Then try again.
It seems the actual issue is not related to MySQL itself, but to MySQL Workbench.
The error you're seeing is a generic error coming from Windows itself, not from MySQL. It's unclear how you're running MySQL, for example is it in your localhost, in a Docker environment, or in a remote server.
It seems clear that at least two processes are trying to get an exclusive lock on that temporary file. My guess is that MySQL won't write temporary files to the user folder we're seeing (with your username Pratik).
On Windows, MySQL checks in order the values of the TMPDIR, TEMP, and TMP environment variables. For the first one found to be set, MySQL uses it and does not check those remaining. If none of TMPDIR, TEMP, or TMP are set, MySQL uses the Windows system default, which is usually C:\windows\temp.
Something you can do is to change your MySQL configuration so it uses a specific Temporary path you'll set, restart MySQL and retry running the query. If you see the error contains your new temporary path you've isolated the issue, it is indeed a MySQL problem. If you keep seeing this path you've isolated the issue to MySQL WorkBench.
An alternative approach would be to run the same query from another MySQL client, for example the command-line client mysql; and see if you're getting the same error.
Probably the simpler approach would be to try the queries with dBeaver, another MySQL client, and use that to isolate the issue to either the MySQL server itself or MySQL WorkBench.
This is a common issue for the upgraded version of MySQL, Try using Open Script instead of Run Script and that seems to clear up the issue.
I've found that it was already reported in the official bug tracker: https://bugs.mysql.com/bug.php?id=104841.
I've just checked, and it's still present in MySQL Workbench 8.0.30.
Work Around
Do not try to open the SQL file from this tool bar:
Go to Server > Data Import:
select import from self-contained file
select your target schema
then start import (bottom right btn)

synchronize two mysql databases

Is there a way of synchronizing two databases as a batch process?
I have a database on my server but i want to have the same database
running on localhost , i know i can download the entirely database and import it
to localhost but
is there a way to import it once and then just run the insert,update,delete querys
that have been done on the server?
is there a log where queries that have been runned are stored? (queries not resultsets)
So far the only way i found it possible was to keep data structure and import all the data every day
Thanks !
I believe there is software to actually do this (version control and push/replicate databases). Off the top of my head, something available for sql server and mysql is called Redgate (http://www.red-gate.com/). Alternatively docker might offer a similar service but for mysql (https://docker.com/).
I think you can run a cron to do so ... Meaning write a script for exporting database from server and import the same using mysqldump command so in that way you would be able to synchronized the same..
to see the log of my sql queries.. please follow this
Edit mysql configuration file:
vi /etc/my.cnf
This will only log slow queries. You need the general log if you want to see all queries.
general_log = 1
general_log_file = "/opt/lampp/logs/query.log"
Note that you'll need to restart the server for this to take effect. Also, you should only use this type of logging during testing as it does cause slowdown.
for more in detail how-to-enable-general-query-log-in-mysql-under-linux

What is a mysql disconnect command line (why is it useful)?

I am running a mysql database and I connect to it just fine. My question is: whenever I connect to the database (to add new input via php) do I also have to include a disconnect command line?
I ask because my bandwidth usage is growing faster than I expected so I am happy thinking that I am getting traffic, but perhaps it is growing because I connect and do not "disconnect"?
From the mysql docs
mysql is a simple SQL shell with input line editing capabilities. It
supports interactive and noninteractive use.
The fact is that the SQL shell should not be causing major load on your box. The standard practice is to just close the shell and kill the program.
Typing Control+C causes mysql to attempt to kill the current
statement. If this cannot be done, or Control+C is typed again before
the statement is killed, mysql exits
When you exit mysql command line tool the process will end and mysql will continue doing its thing. But the answer to your question is no SQL shell should not be slowing things.
From PHP its a good idea to close the connection when you are done using it. To check out what processes are running open up mysql cmd tool and try the following to see what is connected to your mysql instance.
SHOW PROCESSLIST
if showprocesslist isnt what you were looking for give this a shot:
mysql > show status like '%onn%';
Hopefully this will give you enough information to handle the traffic load.
devzone.zend.com :
"Open connections (and similar resources) are automatically destroyed at the end of script execution. However, you should still close or free all connections, result sets and statement handles as soon as they are no longer required. This will help return resources to PHP and MySQL faster."
My advice:
It is a good practise to close a connection after doing the queries you wanted.

mysql client "use database name" taking too long to execute

My database contains large no of tables (more than 300 tables ) . when I execute " use database name " command on mysql command line client , its taking very long time to execute. Is there any way we can make it execute faster. ?
You can pass the -A argument to the mysql command-line tool to make it not load database metadata when using a database.
That being said, what you're describing is usually a sign that either you have too many tables and/or columns, or your database server is overloaded. Often, it's both. Either one should be fixed.
I know its very old post but thought of writing about it as i also had the same problem in past and found this all
you would be having this problem when using cli while connecting mysql remotely this problem generally doesn't occur on localhost. As while using "use" command mysql check metadata of table and for loading it, it confirms host and credentials while connecting to mysql cli remotely and may be that slows down select DB, you could skip dns resolving but i don't think that will solve the problem completely
Hence "-A" tag/attribute have to pass with mysql command on connect remotely which will not load metadata while selecting DB using "USE" command.
for an example :-
mysql -A -h HOST -u USER -p

MySQL mysql_tzinfo_to_sql program

I am using MySQL 5.0.51b on microsoft windows xp. I am trying to load data from zoneinfo files(generated by library downloaded from here) to database tables as described here.
Now i am not able to find where would i get this "mysql_tzinfo_to_sql" program for windows. I tried executing it on mysql command line client but no success.
On linux you can directly execute this command on the shell.
Any help is appreciated.
You don't need to run mysql_tzinfo_to_sql on Windows.
For Windows just do this:
Download the files. Links here
Move them to your MySQL directory.
Example: C:\ProgramData\MySQL\MySQL Server 5.5\data\mysql
Restart your server.
Now, if you want, you can change your timezone like this: SET time_zone = 'America/Costa_Rica';
Check it with SELECT NOW();
More information here: MySQL
And take a look at this: Answer
The command "mysql_tzinfo_to_sql" doesn't work on Windows.
You have to download the timezone packages wich contains SQL statements and populate the timezone tables using the "source" command, like this:
mysql> use mysql ;
mysql> source /path/to/file/timezone_posix.sql ;
Check the following links for reference:
Blog: https://discourse.looker.com/t/cannot-connect-time-zone-tables-dont-appear-to-be-loaded-in-mysql/208/6
Scripts sql: http://downloads.mysql.com/general/timezone_2016a_posix_sql.zip , http://downloads.mysql.com/general/timezone_2016a_leaps_sql.zip
None of the 'populate file' methods worked for me with mysql 8.
A lot of answer contains this link: http://dev.mysql.com/downloads/timezones.html
There's downloadable zip files that contain sql files. Putting it to any directory didn't help.
One thing helped me: I issued a "use mysql;" and executed the content of the downloadable sql file as a script.
based on Francisco Corrales Morales answer.
For MySQL 5.7+ on Windows 10 machine, my procedure is
download latest POSIX Standard time zone script under 5.7+ section from https://dev.mysql.com/downloads/timezones.html
Extract the file then there would be a single SQL file named timezone_posix.sql
run the sql script, in my case, use command line below
bin\mysql.exe --host=localhost --port=3306 --user=USERNAME -p mysql < c:\...\Downloads\timezone_2020d_posix_sql\timezone_posix.sql
Note make sure you run the time zone script under mysql database/schema.
For Windows, MySQL supplies an already loaded database for you to download and stick in your data directory: http://dev.mysql.com/downloads/timezones.html
Copied from the user comments on the MySQl docs:
Posted by Jyotsna Channagiri on
November 20 2008 6:28pm
Hi,
I thought this information will helps
somebody who are looking for changing
mysql timezone.
The steps are:
Download the timezone table structure and data from
http://dev.mysql.com/downloads/timezones.html
Copy and paste the data in your Mysql/data/mysql folder
Restart your mysql server.
mysql> SET GLOBAL time_zone = 'America/Toronto';
mysql> SET SESSION time_zone = 'America/Toronto';
Check SELECT # #global.time_zone , # #session.time_zone ; It should give
you the time zone you set just before.
Comment:
Yes, but the tables provided by MySQL are outdated (generated by 2006p version of D olson's timezone library). I need the latest timezones data, hence i downloaded the latest library and generated the binaries. Now i need a way to load these tables in mysql.But i don't know how to do it on windows.
Ah, I see. Then you're going to need to do one of two things.
1) get the tool that does this and compile it (or whatever) on Windows. If you're lucky, it's a perl script.
2) fill the database on linux, then copy it to Windows. [This guy][http://it-idiot.einsamsoldat.net/2008/01/moving-mysql-database-from-windows-to-linux-redhat/comment-page-1/2] says it can be done, at least for MyIsam.
mysql_tzinfo_to_sql /usr/share/zoneinfo | mysql -uadmin -ppassword mysql
I am using XAMPP and PHP 7.4.27 on Windows 10 and had some difficulties getting other solutions to work.
Here are the steps I took to get it working was what worked for me.
Download the latest MySQL Community Downloads POSIX Standard or the Non POSIX with leap seconds(if you need the leap seconds included) time zone script under the section that states:
Each file contains SQL statements to fill the tables
Extract the file, which should be a single SQL file named timezone_posix.sql.
Open the extracted SQL file in the code editor of your choice and copy its content.
Open your DB administration tool of choice, select the "mysql" table and under the "SQL" tab paste the contents of the extracted file.
Note: #4 Instructs on the basis of PHPMyAdmin, other administration tools might have a different process.
Click "Go" and follow any prompts after that.
All needed time_zone tables should be populated with timezone data