The mysql command-line client supports a history (you can use the cursor-keys to access and navigate it), but is there a way to access it (for example) in bash?
In bash you can do:
history | grep "command"
Is there something similar in mysql?
You may use Ctrl+R to search the history, as in bash.
Usually this lists up all MySQL commands that have been used:
less ~/.mysql_history
so, in mysql you can use:
system grep "command" ~/.mysql_history
Open the file in vim editor
vi ~/.mysql_history
then you can search the pattern with / by entering you text.
Related
Relative Linux newbie here but I can't believe how much I'm struggling with this simple thing:
In shell, at a mysql prompt (version 5.6.xx):
mysql> show variables;
great, it blasts a ton of information to the console. I need it to pipe to a file instead. I've tried various (old) suggestions I found on the interwebs...no joy.
How do I get the result of this command to end up written to a text file?
You can simply use the "-e" option like this:
mysql -uyour_user_name -pyour_passwd -e "show variables;" > ./mysql_variables.txt
Give this a go.
mysql < commands.txt > output.txt
where commands.txt is a file containing
show variables;
And the output should end up in the output.txt file
Mercurial's output may be localized. This is nasty when parsing command output and it actually may break such code. Is there some command line option or environment variable which can be used to force Mercurial to use English locale?
If you're writing script that interact with the mercurial executable then you probably want to look at the Command Server, or if you're writing python scripts hgapi.
These will give you interfaces which don't have localised strings in them.
For a linux shell (Bash, bsh, sh), use:
LANG=en_US hg <status/log/etc>
I am trying to import zipped database files into Mysql using command prompt using the following command
7z < backup.sql.7z | mysql -u root test
The root user don't have any password associated with it.
test is my target blank database.
I use 7zip for unzipping purpose.
The zipped database i.e. backup.sql.7z is located in D drive.
But it's giving the following error
So, instead I used the following command
7z < backup.7z | mysql -u root test
Note: This time I am using backup.7z instead of backup.sql.7z
But then I get the following error
Clearly there's something wrong with my SQL syntax.
What will be the correct syntax to use then ?
I needed to import from a compressed file as well, and stumbled upon your question.
After a bit of messing around, I found that this worked for me:
7z x -so backup.7z | mysql -u root test
x is the extraction command
-so makes 7-zip write to stdout
Nothing wrong with your syntax, it's just a limitation with 7zip. It's better to use xz in this case, which doesn't put extraneous junk in stdout, or directly call the 7z.dll with your favorite programming language. 7z.exe is really meant for archive management, rather than unix-style piping, and Igor is very reluctant to change that.
If you try a plain 7z < somefile.7z you'll immediately see that all you get back is a usage list.
I would like to know how to retrieve a file using Vi in MySQL. I logged in using:
mysql -uuser -p -hserver -A database
Then I do:
\e
The editor opens and I type my query of 200 lines, then I :wq and \G (if I save the file it says: /tmp/sql9SbYQZ saved) and I see the result.
Now, if I make a mistake or run a different query and I try to type \e again, the query is lost.
ll /tmp/sql9SbYQZ
ls: /tmp/sql9SbYQZ: No such file or directory
Is there a way to retrieve the lost file?
Here's what I added to my .vimrc in order to save the current query in case i made a mistake.
nmap <F7> :w! /tmp/query.sql\| wq!<CR>
This will create a map to the F7 key (you can change it of course). So every time you open a file either using edit or \e, you change it use the F7 key.
This will save a backup of your current query to /tmp/query.sql and then save and close the temporary file. This way, if you make a mistake, you just re-open the backup file and try again.
Here's also a link you might like: http://vim.wikia.com/wiki/Open_the_last_edited_file
With the vi/m editor used with mysql, crontab, and many others, the work is done in a tmp file, as you see from your messages.
Edit (Big doah!, remove cruft about ls -l /tmp/..., you already did that!)
In the future the solution is to tell vim to w the buffer to a file name of your chosing, i.e.
w! /home/you/scripts/mysql2.sql
Then close the editor with
q
Note you may not need the ! after w.
I hope this helps.
Here is something you can try:
In Linux, do the following
$ cd
$ cp .mysql_history mystuff.txt
$ vi mystuff.txt
You should see the file .mysql_history. The mysql client records all queries and commands executed. Hopefully, your query is in there.
Give it a Try !!!
In Linux, and many other systems, when navigating the terminal you can press Tab to auto complete a directory or file name.
I'm wondering if there is anything like that in the MySQL terminal. For example, if I want to get the description of someTableWithRidiculousLongName I could type describe someTableW then Tab and it would auto-complete the rest.
Does anything like that exist in the MySQL terminal?
Edit or create a file called .my.cnf in your home directory, containing:
[mysql]
auto-rehash
To enable autocomplete within the MySQL prompt type:
mysql> \#
After that you can type:
mysql> describe someTableW[TAB]
To get:
mysql> describe someTableWithRidiculousLongName
start MySQL console with additional option --auto-rehash, i.e.
mysql --auto-rehash -u root -p
I know this is an old question, but I've found very helpful MySql cli client with advanced autocompletion:
mycli. It's much smarter than builtin auto-rehash feature.
On OS X 10.11.6 I set --auto-rehash as described above, but it did not work. (This is OS X so mysql is compiled with the BSD libedit library.)
Then I remembered that I had set vi key-bindings for mysql client by creating ~/.editrc, containing one line: bind -v. This works great for giving me vi-like navigation in mysql client, but it broke column name completion (I was able to verify this by removing .editrc).
So I researched a little bit and found that ~/.editrc should have at least the following lines:
bind -v
bind \\t rl_complete
With this additional line, name completion works correctly in mysql AND vi-like navigation works also. (There are other .editrc settings which greatly improve mysql client navigation, but this isn't the place to start that thread of discussion.)
Some notes about auto-rehash:
When you enable autocompletion editing the mysql config file..
[mysql]
auto-rehash
You can do it for all users or only for one user:
/etc/my.cnf: All Users
~/.my.cnf: Actual user
You can also disable autocompletion adding:
no-auto-rehash
Extracted from: http://www.sysadmit.com/2016/08/linux-mysql-autocompletar.html
You can also auto-complete based on the command history. Start typing, then invoke the keys which are bound to ed-search-prev-history and ed-search-next-history. This applies if mysql comes with libedit support. The default keybindings are Ctrl-P and Ctrl-N, but this can be customized in .editrc. My example for Ctrl-up and Ctrl-down:
# start typing, then press Ctrl-Up
bind "\e[1;5A" ed-search-prev-history
# start typing, then press Ctrl-Up, then Ctrl-Down
bind "\e[1;5B" ed-search-next-history
Previously, mysql was based on readline, and then history-search-backward and history-search-forward are the correct commands. Configuration then was by means of .inputrc. Same example as above:
# these are the key bindings for the readline library
# start typing, then press Ctrl-Up
"\e[1;5A": history-search-backward
# start typing, then press Ctrl-Up, then Ctrl-Down
"\e[1;5B": history-search-forward
So, say you started typing sel and invoke Ctrl-Up, select * from some_long_table_name would come up if that is a command I have used earlier.