Hướng dẫn mysqlsh run sql file
I want to execute a text file containing SQL queries, in MySQL. I tried to run
Any idea on what I am doing wrong?
PiJei 5644 silver badges17 bronze badges asked Jan 20, 2012 at 10:51
2 If you’re at the MySQL command line
answered Jan 20, 2012 at 11:04
Zoe EdwardsZoe Edwards 12.2k3 gold badges21 silver badges41 bronze badges 12 You have quite a lot of options:
Sebas 20.7k9 gold badges53 silver badges108 bronze badges answered Jan 20, 2012 at 10:55
Eugen RieckEugen Rieck 63k10 gold badges69 silver badges92 bronze badges 3 you can execute mysql statements that have been written in a text file using the following command:
if your database has not been created yet, log into your mysql first using:
then:
then:
that should do it! More info here: http://dev.mysql.com/doc/refman/5.0/en/mysql-batch-commands.html answered Apr 26, 2013 at 4:22
Paul PreibischPaul Preibisch 3,8292 gold badges26 silver badges30 bronze badges 3 My favorite option to do that will be:
I use it this way because when you string it with "" you avoiding wrong path and mistakes with spaces and - and probably more problems with chars that I did not encounter with. With @elcuco comment I suggest using this command with [space] before so it tell bash to ignore saving it in history, this will work out of the box in most bash. in case it still saving your command in history please view the following solutions: Execute command without keeping it in history extra security edit Just in case you want to be extra safe you can use the following command and enter the password in the command line input:
answered Dec 17, 2014 at 7:34
talsibonytalsibony 8,1285 gold badges46 silver badges44 bronze badges 9 All the top answers are good. But just in case someone wants to run the query from a text file on a remote server AND save results to a file (instead of showing on console), you can do this:
Hope this helps someone. answered Apr 1, 2015 at 18:15
BhushanBhushan 17.7k30 gold badges102 silver badges136 bronze badges 2 I came here searching for this answer as well, and here is what I found works the best for me: Note I am using Ubuntu 16.x.x
Hope this helps.
thclpr 5,5478 gold badges49 silver badges83 bronze badges answered Sep 7, 2016 at 1:59
Zac SmithZac Smith 1,8191 gold badge12 silver badges16 bronze badges 2 Give the path of .sql file as:
answered Jan 5, 2017 at 17:17
Shubham VermaShubham Verma 7,9146 gold badges51 silver badges75 bronze badges 1
Do not specify single quotes. If the above command is not working, copy the file to c: drive and try again. as shown below,
Machavity♦ 30.1k26 gold badges87 silver badges98 bronze badges answered Mar 9, 2015 at 6:52
instead of redirection I would do the following
This will execute the file path-to-sql-file answered Apr 24, 2019 at 8:15
DataGuruDataGuru 6877 silver badges16 bronze badges Never is a good practice to pass the password argument directly from the command line, it
is saved in the Use this instead:
answered Feb 3, 2017 at 5:23
azro 49.1k7 gold badges31 silver badges66 bronze badges answered Feb 8, 2017 at 16:26
So many ways to do it.
If you get errors from the command line, make sure you have previously run
This must be run from within the mysqld.exe directory, hence the CD. Hope this is helpful and not just redundant. answered Jul 19, 2019 at 19:37
DonkeyKongDonkeyKong 95513 silver badges18 bronze badges 1 From linux 14.04 to MySql 5.7, using
You can use this method for many MySQL commands to execute directly from Shell. Eg:
Make sure you separate your commands with semicolon (';'). I didn't see this approach in the answers above and thought it is a good contribution.
Dharman♦ 27.9k21 gold badges75 silver badges127 bronze badges answered Oct 12, 2020 at 17:32
Very likely, you just need to change the slash/blackslash: from
to
So the command would be:
answered Sep 3, 2013 at 22:30
Ben LinBen Lin 7098 silver badges14 bronze badges use the following from mysql command prompt-
Use no quotation. Even if the path contains space(' ') use no quotation at all. answered Sep 28, 2013 at 16:46
Rajesh PaulRajesh Paul 6,5306 gold badges38 silver badges55 bronze badges Since Make sure that the Database is created first. Then:
answered Aug 7, 2014 at 14:20
3xCh2_233xCh2_23 1,4391 gold badge20 silver badges39 bronze badges For future reference, I've found this to work vs the aforementioned methods, under Windows in your msql console: mysql>> If your root drive isn't called "c" then just interchange with what your drive is called. First try backslashes, if they dont work, try the forward slash. If they also don't work, ensure you have your full file path, the .sql extension on the file name, and if your version insists on semi-colons, ensure it's there and try again. answered Jun 25, 2015 at 2:31
Chad MxChad Mx 1,17611 silver badges17 bronze badges You can run with drush command on your project directory
answered Apr 9 at 20:41
Xab IonXab Ion 6471 gold badge7 silver badges15 bronze badges I had this error, and tried all the advice i could get to no avail. Finally, the problem was that my folder had a space in the folder name which appearing as a forward-slash in the folder path, once i found and removed it, it worked fine. answered Aug 7, 2015 at 15:31
|