Grep in MySQL CLI interpretter - mysql

Is there any way to grep the output of the MySQL interpretter (CentOS 5.x)? For instance, I know that I need a table with "user" in the table name. Ideally, I would love something like this:
DESCRIBE TABLES; | grep "user"
I know that I can exit MySQL and then do it in bash:
mysql -u me -p "USE someTable; DESCRIBE TABLES;" | grep "user"
But I would prefer to stay in MySQL. Thanks.

Give this a try!
mysql> pager grep user
PAGER set to 'grep user'
mysql> USE someTable;
mysql> DESCRIBE TABLES;
I've just tried it with Mysql v5.0.83, and it does what you would think ;)

You can simply filter tables with a like
show tables like '%user%';

Related

Search for specific string/pattern in MySQL Database from Terminal/SSH/Commandline

I have a Magento database in which I want to search for a particular string/pattern.
But the database's size is too large so I cannot export the database to .sql file and then search into that file(editor even Geany crashes opening such large files).
So how can I do a search the database for a perfect match of [string/pattern] and display fulltext information as result, through only using command-line and MySQL Database credentials ?
I tried below command, but it requires username to be given as -u[USERNAME], also it doesn't display full query or result in terminal window.
mysqldump -p[PASSWORD] [DATABASE] --extended=FALSE | grep [pattern] | less -S
Anyone have any solutions for this ?
You can first log into MySQL CLI as especified in http://dev.mysql.com/doc/refman/5.7/en/connecting.html
mysql --host=localhost --user=myname --password=mypass mydb
So, you can use a query command to find your pattern. If you know the table you want to search such as the column it make the thinks easy. The SELECT statement is like this:
SELECT column FROM table WHERE column LIKE '%pattern%';
http://dev.mysql.com/doc/en/select.html
If you don't know the table's name, you can list all and try to find by the meaning.
SHOW TABLES;
Edited with better code
You didn't say if this was a one off or not but this will check all tables in a schema for a value.
First in your home directory set up a file named .my.cnf with the following contents and change its permissions to 700 (Replace [USERNAME] and [PASSWORD] with your username and password.
[client]
user=[USERNAME]
password="[PASSWORD]"
Then execute the following (Replacing [DATABASE] and [CHECKSTRING] with your database and the check string)
mysql [DATABASE] --silent -N -e "show tables;"|while read table; do mysql [DATABASE] --silent -N -e "select * from ${table};"|while read line;do if [[ "${line}" == *"[CHECKSTRING]"* ]]; then echo "${table}***${line}";fi;done;done
If checking for 51584 the result would be something like
test_table***551584,'column 2 value','column 3 value'
test_table5***'column 1 value',251584,'column 3 value'
If you want to know which column had the value then select from INFORMATION_SCHEMA.COLUMNS and add another nest.
mysql [DATABASE] --silent -N -e "show tables;"|while read table; do mysql [DATABASE] --silent -N -e "select column_name from information_schema.columns where table_schema='[DATABASE]' and table_name = '${table}';"|while read column; do mysql [DATABASE] --silent -N -e "select ${column} from ${table};"|while read line;do if [[ "${line}" == *"[CHECKSTRING]"* ]]; then echo "${table}***${column}***${line}";fi;done;done;done
If checking for 51584 the result would be something like
test_table***column1***551584
test_table5***column2***251584
First of all you need to login into database with correct username and password by below command.
sudo mysql -u root -p
then check the database in which you want to operate operation.
eg.
SHOW DATABASES;
USE Test;
now your database is ready for operation through terminal. Here I assume my database name is "Test".
Now for String/pattern matching use command as below or follow the link http://www.mysqltutorial.org/mysql-regular-expression-regexp.aspx.
SELECT
column_list
FROM
table_name
WHERE
string_column REGEXP pattern;

Copy MySQL user to create a new one

I have a ton of users on many different MySQL servers with this type
myuser#localhost
myotheruser#localhost
now, I want to create new users, that should have the same password as the user above, and have access to the same databases, but from a different host like this:
myuser#127.0.0.1
myotheruser#127.0.0.1
does anyone have a quick and easy way to do this?
I have tried a different approach to this problem, but running these two commands:
mysqldump --skip-extended-insert mysql user | grep 'localhost' | egrep '^INSERT INTO' | sed 's/localhost/127.0.0.1/g' > add-local-ip-user.sql
mysqldump --skip-extended-insert mysql db | grep 'localhost' | egrep '^INSERT INTO' | sed 's/localhost/127.0.0.1/g' > add-local-ip-db.sql
Then, I just output the content:
cat add-local-ip*
then I open mysql:
mysql mysql
and finally just paste the output from above, and run flush privileges does anyone have any objection to doing it this way? e.g. is this a stupid solution?

mysql command line to vertical output

I'd like to get the result of a mysql query with a vertical output.
My problem when using --vertical (or G) is the starred lines.
$mysql -N -e 'select filed1, field2 from db.tlb\G'
*************************** 1. row ***************************
value1_field1
value1_field2
*************************** 2. row ***************************
value2_field1
value2_field2
...
Is there an option I didn't find to get rid of the lines ***[...] x. row [...]*** ?
At the moment, I'm using a egrep -v '^\*.*\*$', but I'm sure a better solution exists.
I'm not sure this is a good solution, but if explicitly using egrep is annoying, you could define a shell function to launch mysql with the desired pager. Assuming you're using a bash (or compatible):
# define a shell function to launch mysql with the required _pager_
sh$ mysql() { `which mysql` $* --pager="egrep -v '^\*.*\*$'" ; }
[ ... ]
# launch mysql "as usual" (in reality, this is the shell function that is invoked)
sh$ mysql -u user -p -h mysql-host mydb
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 67
Server version: 5.1.49-3 (Debian)
-- Use mysql normally -- but the "egrep pager" will remove the "star lines"
mysql> select * from T\G
col1: w
col2: x
col3: 0.4
1 row in set (0.00 sec)
As I said before, this is not a perfect solution since egrep will blindly remove from output any "star line" -- not only the one from an ego (\G) command.
try this (but I can not imagine why you need such an output)
mysql -N -B -e 'select * from db' mysql | tr "\t" "\n"
First Option
would be to change the MySQL pager like so:
test.sh
#!/usr/bin/env bash
# Some Basic Configuration
db="homestead"
dbuser="homestead"
dbhost="127.0.0.1"
# Executes the MySQL command using the basic configuration
# This also sets the MySql Pager to less -Sin to enable
# it also removes all lines starting with "*"
mysql -h $dbhost -u $dbuser -p --pager="grep -Ev '(^\*.*$)|(^$)' | less -Sin" $db
Usage
Firstly Edit the Config variables:
$ nano ./test.sh
Secondly Run the script
$ bash ./test.sh
Second Option
Change the pager after you're already in the MySQL CLI
$ mysql -u root -p -h 127.0.0.1 somedatabase
enter password:
mysql> pager grep -Ev '(^\*.*$)|(^$)' | less -Sin

How do I select a MySQL database through CLI?

I've managed to get into MySQL using the command line terminal, but when I tried to enter some SQL, it said 'no database selected'
how do I select a database? my database name is: photogallery
What code do I use to select it?
Use USE. This will enable you to select the database.
USE photogallery;
12.8.4: USE Syntax
You can also specify the database you want when connecting:
$ mysql -u user -p photogallery
While invoking the mysql CLI, you can specify the database name through the -D option. From mysql --help:
-D, --database=name Database to use.
I use this command:
mysql -h <db_host> -u <user> -D <db_name> -p
Switch to a database.
mysql> use [db name];
MySQL Commands
Hope this helps.
use [YOUR_DB_NAME];
Alternatively, you can give the "full location" to the database in your queries a la:
SELECT photo_id FROM [my database name].photogallery;
If using one more often than others, use USE. Even if you do, you can still use the database.table syntax.
Use the following steps to select the database:
mysql -u username -p
it will prompt for password, Please enter password.
Now list all the databases
show databases;
select the database which you want to select using the command:
use databaseName;
select data from any table:
select * from tableName limit 10;
You can select your database using the command use photogallery;
Thanks !
USE database_name;
eg. if your database's name is gregs_list, then it will be like this >>
USE gregs_list;

Drop multiple databases using mysql command

I have many databases with different names.
I want to drop multiple databases, Is there any command since all names of db are different.
Eg: mysql db, Test db, live db.
As of I know, there is no specific command/query to delete multiple databases without having a specific pattern in their names. Even I was asked to do the favor several times. So I researched and found no specific solution. Then I tried the below hack. It worked without giving much trouble. May be it could help for you too.
Take all the databases using the below command.
SHOW DATABASES ;
Paste all of them in an excel/some other text file (I prefer NPP). Keep the only names which you want to delete from the list. Dont forget to remove your working db's from the list.
Add DROP DATABASE in front of those names.
That's it simple. Copy & Paste all of those in your workbench. You can execute all of them in one shot.
If you create a shell script this should remove all the databases. You will need to edit it to suit your needs.
DBUSER='user'
DBPASS='password'
SQLFILE='/path/to/file/databases.sql'
echo '* Dropping ALL databases'
DBS="$(mysql -u$DBUSER -p$DBPASS -Bse 'show databases' | grep -v Database | grep -v database | grep -v mysql | grep -v information_schema)"
for db in $DBS; do
echo "Deleting $db"
mysql -u$DBUSER -p$DBPASS -Bse "drop database $db; select sleep(0.1);"
done
First run this query to produce a list of drop commands:
select CONCAT('drop database `', schema_name,'`;') as database_name from information_schema.schemata where schema_name like '%DATABASES_TO_REMOVE%' order by schema_name;
Then copy the output rows of this query and paste into a query window
In my case I then needed to remove the single-quotes (') surrounding the resulting command queries which I did using a simple find + replace (often Ctrl + H, replace ' with < empty >)
And execute (highlighting all of the drop statements in my case)!
Unfortunetly, there is nothing like that, unless you create your own function.
simple bash script can be done this work
#!/bin/bash
cat /home/mshafee/file | while read line
do
mysql -u username -p****** -h 0.0.0.0 -e "drop database $line;"
done
here provide username, password and IP address.