How to execute a my sql migration script in MYSQL Shell? - mysql

Despite a comprehensive search I couldnt find a definite answer how to run a mysql script, sitting in one specific directory, directly in the mysql shell.
The MYSQL documentation states:
shell> mysql < text_file
So far so good, but the text file (*.sql) sits in another directory than the mysql instance. So that doesnt work that easily - unfortunately...
I hope someone can help me out.
Thank you in advance
GG

In the documentation you are quoting, "shell>" refers to a Unix prompt, not MySQL shell.
In MySQL shell, just do "source /path/to/file" "/path/to" being the absolute path to the file.

So found it out myself ;-)
Just add the source path of the script behind the mysql-Prompt like:
mysql> SOURCE C:\User\Folder\Folder...
Best GG

Related

How to access data from a SQL snapshot

I have been given a SQL database snapshot in a file sqlfile.sql
I want to access these data, but I do not know how to proceed. I am new to SQL.
I have a Macbook pro with MacOSX Sierra (10.12) and I have installed mysql with Homebrew. The version of mysql is 8.0.16
running mysql works, I can access the mysql prompt line.
I would like to be able to access the data, in python if possible, but if it has to be through the mysql command line, it is fine. Once I can access the tables, I know how to query the data, that is not any problem.
I tried with MySQL Workbench as well, but it does not work either.
Can someone point me towards some guide on how to proceed? I have spend hours trying to find some clue, but I did not succeed.
Thanks a lot!!!
Finally I managed to solve the problem. It was not really complicated, but I had problems finding the right combination of commands.
What I did was, in the command line enter into the mysql prompt. Once here:
mysql> create database mydatabase
Once done, go outside the mysql prompt and in the normal command line execute:
mysql -uroot -p mydatabase < db_snapshot_file.sql
This populates the database called mydatabase. Once done, from Python I could access it. But first, since I had some problem with the authentication, I had to use this answer to solve it.
To access from Python, I used this information which gave me some indications on how to start.

How do you create a folder using SQL

I have a SQL script which selects data from DB and stores it to files. I am unable to create a directory to store these files.
I have shell script that loads the SQL file. Shell and the SQL are on separate server than MySQL db. I would prefer to create this directory using SQL as I want to avoid ssh.
Any suggestions? Surprisingly I couldn't find anything on Google.
I will assume that you're using mysql, according to your tags. You could do it with a Microsoft SQL Server or Oracle database but unfortunately, at the moment, there is no solution to create a directory from MySQL.
Some will guide you with a workaround based on the creation of a data directory, I wouldn't recommand this, as it could lead to performances issues in the future, or worst.
The best solution would be to use a script (java, vbscript, SSH, batch, ...). Again, you won't be able to start this script within your SQL query easily. I know that's no good news, but it is important not to lead you on the wrong direction.
I would suggest to reverse your thinking, and start your SQL query from a script (again, any language you're used to).
I couldn't find any other way other than opening ssh session to the target box.
Open ssh session
Create directory
close ssh session
Load sql file using shell
The sql adds the generated files to the directory created in step 2.
ssh -t $USER#$HOST <<-SSH-END;
mkdir -p "dir/path";
exit;
SSH-END
Sharing just in case someone else needs to do the same.

Importing huge sql file using phpmyadmin

I am trying to import 2 GB sql file of table. I tried using bigdump but it failed. Can any body help me in this regard? I am using phpmyadmin by increasing the max_upload_filesize. Any help will be much appreciated.
How To
Make sure you change both *"post_max_size"* and *"upload_max_filesize"* in your "php.ini" (which is located where your "php.exe" is).
The following example allows you to upload and import 128MB sql files:
post_max_size=128M
upload_max_filesize=128M
Restart Apache and you're all set.
Alternatives
An alternative way to work around the problem is to use the command line. But it's a workaround, and as ugly as workarounds get:
C:\xampp\mysql\mysql.exe -u root -p db_name < C:\some_path\your_sql_file.sql
As you're not using the server but the command line, upload and POST sizes don't matter. That's why I call this a "workaround", since it doesn't actually solve your problem.
I tried using the mysql console like in this answer and it worked for me! (It will take a while)

Splitting MySQL database into tables separate

Well I'm not much of a good developer or a database expert. But I have a little understanding of these things. I'm trying to dump a database on a VPS using "mysqldump" command which works perfectly. But when I tried to restore locally after downloading the dump, gives me a time out error.
Can anyone advise me how to dump a database by splitting it into tables separately. The database I'm referring to is pretty large (6 - 7 GB). I actually tried searching and it confuses me.. even this link here confuses me as where to start.
Any help is highly appreciated.
Are you restoring with phpmyadmin? If you try to upload the import it is probably too large.
You can set a directory where the backup files are stored, then you can select the file in phpmyadmin without uploading it.
For the timeout with importing you can increase the timeout settings, or use something like "BigDump"
If you're using mysqldump I'll assume you're familiar with the command line.
On your local machine, use
mysql -u [root] -p [database_name] < [database_dump.sql] -v
enter password: ********
The empty database needs to be created your local machine first before you can import the structure and data to it (as simple as doing CREATE DATABASE [database_name];)
The -v flag will do it in 'verbose' mode so you can see the queries as they run. Omitting '-v' will stop it filling your window with the queries but will also give you that 'is it working or not?' nervous feeling after a few minutes.
This will work on Windows as well as it works on Linux / Mac / anything else
Replace my [placeholders] with your own values.
Thank you so much for all your answers! Well, what I was looking for is a dumping method or a similar script to dump the database table by table. Finally I tried the dumping the output file with a .txt extension which returned me with success.
Below is the command I used (I know its pretty long proceess, but I finally got all tables dumped);
mysqldump -u users -p database_name table_name > table_name.txt
I used the current directory to output the file assuming I'm already in the directory where I need to dump. If you need to dump the output file to a specific dir, then use /path/to/the/dump/table_name.txt instead of just mentioning the table name. Ans make sure you don't enter password after -p. I don't know why, but I left it blank and it prompts for the password. Then when I type password it dumps to a text file.
I hope this helps.!
Once again thank you so much for the users who came in the first place to help me. :)

Where do I find the MysqlDumpSlow command?

Hi I'm logging slow queries because we're having some performance
issues and I have read about mysqldumpslow and thought that would be a good
way to sort through the queries.
At the command prompt, I type mysqldumpslow and I get this:
'mysqldumpslow' is not recognized as
an internal or external command,
operable program or batch file.
I'm using MySQL version 5.0.79 on Windows Vista.
Note:
c:\Program Files\MySQL\MySQL Server 5.0\bin is my path and I have searched the drive for 'mysqldumpslow' and can not find it.
What am I doing wrong?
Note:
MySql 5.0.x does support the mysqldumpslow command follow this link to manual
edit : oops, I read the manual wrong, and gave wrong information :-( sorry :-( let's give it another try...
I've just installed MySQL on windows, to try using mysqldumpslow, and I don't have mysqldumpslow installed either :-( So, you are not alone, and it doesn't seem to be a problem with your install (I've tried 5.1.x, but as you highlighted, it should be the same for 5.0.x)
Looking at the "mysqldumpslow" I have under Linux, it appears it is a Perl script ; and Perl is not often installed on a Windows machine. Maybe that would be a hint to a solution...
Well, after a bit more testing, when installing MySQL, it seems you have to select "Developpers Components > Scripts, examples", which is not installed by default (at least on windows) -- no need to reinstall everything : you can "modify" the installation, to add this option.
Then, you will have a "script" directory next to the "bin" one.
For instance, on your install, it should be something like "c:\Program Files\MySQL\MySQL Server 5.0\scripts".
In this directory, there are some scripts ; one of them is mysqldumpslow.pl ; which is what you are looking for ;-)
Now, you "just" have to get Perl installed and running on your machine (sorry, I've never installed Perl on windows ; but you can find some informations here)
Hope this helps better than what I posted before !
try typing
whereis mysqldumpslow
If that still says its not installed,
EDIT: Above is not relevant, as on windows. Missed that, thanks to commenter for pointing it out.
I always use mysqlsla (mysql slow log analyser) ahead of mysqldumpslow - you could give that a go.
http://hackmysql.com/mysqlsla
Mysqldumpslow is a Perl script so under Windows you will need to make
sure you have Perl installed and will need to explicitly invoke it via
Perl.
The way the script is set up it will work automatically under Unix
because Unix has the ability to run scripts directly by identifying the
correct script processor from the first line of the script. Of course
you still need Perl installed, but that is taken for granted in most
Unix installations.
Or you can also try to set the path in Windows environment variables.