Getting MYSQL DB info via bash - mysql

I'm using some bash code which I got off another post on here
#!/bin/sh
users=$(mysql --user=user --password=password --database=$db -h _IP ADDRESS) -s -- execute="select$ users from db limit 1;"|cut -f1)
echo "$users"
The database is DB and the table is users basically I want to be able to get a user count from the table but when I run the script I get
ERROR 1049 (42000): Unknown database 'execute=select users from db limit 1;'
any idea what i'm doing wrong or a better way of doing it? if I do
select * form users;
on the mysql server itself it returns 12 rows in set (0.00 sec) 12 being the number of users, so I just want my script to query the user table on database DB and return the number of rows ie 12.

users_count=$(
mysql --user user_name --password=password -h x.x.x.x <<EOF | tail -n 1
select count(1) from mysql.user;
EOF
)

it seems that the order of your params is wrong...
MYSQL is telling you that 'execute=select users from db limit 1;' is in the position of the database parameter.
Try something like this:
mysql --user user_name --password=password -e 'select users from db_schema.table limit 1;'

Simply try this:
mysql -u username --password=password -h hostname database_name -e 'select count(1) from table limit 1;'

Related

Truncate all tables (except desired ones) is not working

I'm trying to write a command in terminal which will truncate all tables in database except don't truncate those tables which are specified. This are my commands
// create tmp database
mysql -h localhost -u root -proot -e "create database testDb;"
// create tmp user and grant all PRIVILEGES for testDb
mysql -h localhost -u root -proot -e "GRANT ALL PRIVILEGES ON testDb.* TO tmpUser#localhost IDENTIFIED BY 'tmpPass'; FLUSH PRIVILEGES;"
// truncate all tables except table1 and table2
mysql -u tmpUser -ptmpPass -e "SET FOREIGN_KEY_CHECKS=0; SELECT CONCAT('TRUNCATE TABLE ', TABLE_NAME, '; ') FROM information_schema.tables WHERE table_schema = 'testDb' AND table_name NOT IN ('table1', 'table2');"
And the out put in terminal is
+-----------------------------------------------------------------------+
| CONCAT('TRUNCATE TABLE ', TABLE_NAME, '; ') |
+-----------------------------------------------------------------------+
| TRUNCATE TABLE tableX; |
| TRUNCATE TABLE tableY; |
| TRUNCATE TABLE tableZ; |
+-----------------------------------------------------------------------+
which is OK, because based on log all tables got TRUNCATED except table1 and table2, but the problem is that when I check in database if tables are truncated, data is still present. So if I check tableX, tableY or tableZ it still has records in it.
So the question is: Is there something wrong with my TRUNCATE TABLES command? Can I somehow check if there is some mysql process which is not finished maybe. How to debug this?
I'm working on Vagrant machine (just telling if this could be a case in any way).
If you need any additional information's please let me know and i will provide. Thank you!
I solved this problem with some sort shell script... It goes like this
EXCLUDETABLES=('table1' 'table2')
ALLTABLES=$(mysql -u root -ppass -Nse "SHOW TABLES testDb")
for TABLE in $ALLTABLES
do
DOTRUNCATE="yes"
for EX in "${EXCLUDETABLES[#]}"
do
if [[ "$EX" == "$TABLE" ]]; then
DOTRUNCATE="no"
break
fi
done
if [[ "$DOTRUNCATE" == "yes" ]]; then
mysql -u root -ppass -e "TRUNCATE TABLE testDb.$TABLE;"
fi
done

MySQL: Insert into a table from another HOST

So I would like to copy some records from one table to another. But the trick is that the another table is in a different HOST. I will try to explain by giving you a mysql query pseudo code.
Another_host = "192.168.X.X";
INSERT INTO database_original.table_1( id, name, surname)
SELECT id, name, surname
FROM Another_host.database_another.table_2
WHERE Another_host.database_another.table_2.id > 1000;
I would probably have to declare the user for the "Another_host" somewhere.
This is what I am trying to do..is this even possible like I imagine it?
Thx
There is one workaround solution which will do the same what you want.
Step 1:
Take dump of select query
mysql -e "select * from myTable" -h <<firsthost>> -u myuser -pxxxxxxxx mydatabase > mydumpfile.sql
Step 2: Restore the dump
mysql -h <<secondhost>> -u myuser -pxxxxxxxx < mydumpfile.sql

update mysql query from a batch file

Basically my batch file contains:
mysql -u root -pMypassword use myTableDB update myTable set extracted='Y'
but for some syntax error it doesn't update the table. However, when i run through command line:
mysql -u root -pMypassword use myTableDB
mysql update myTable set extracted='Y'
through command line it works. Anyone can point me what syntax error i have on the batch file.
The cleanest way would be the following:
mysql -u root -pMypassword -DmyTableDB -ANe"update myTable set extracted='Y'"
or if you want the SQL command placed in a variable, you could do this
set sqlstmt=update myTable set extracted='Y'
mysql -u root -pMypassword -DmyTableDB -ANe"%sqlstmt%"
Here is an example i just ran
set sqlstmt=show databases
mysql -u root -pMypassword -DmyTableDB -ANe"%sql%"
and I got this
C:\WINDOWS\system32> set sqlstmt=show databases
C:\WINDOWS\system32> mysql ... -ANe"%sql%"
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
+--------------------+
C:\WINDOWS\system32>
mysql client reads SQL commands from STDIN. To do what you want, you would have to do something like the following in your batch file:
echo "update myTable set extracted='Y'" | mysql -u root -pMypassword myTableDB

How to insert a resultset of a query to a table in shell script?

I have written a shell script as shown below
#!/bin/bash
result=$(msql -h localhost -uroot -proot -e database "SELECT * FROM USERS");
resultdetails=$(msql -h localhost -uroot -proot -e database "SELECT * FROM DETAILS");
I need to Insert both the result set from result and resultdetails to a new table "TABLE USERDETAILS".
I have tried lot and searched google for hour but couldn't able to get a solution so any help thanks in advance.
try the insert ... select ... syntax. Below is a example
msql -h localhost -uroot -proot -e database "INSERT into userdetail SELECT u1, u2, u3, d1, d2, d3 FROM USERS u INNER JOIN DETAILS d ON (u.id=d.user_id)"

mysql command line thinks I am using a different table. ERROR 1146 (42S02) at line 1

I have a database called maxppgco_drp1 with a table called users. I want to update the users table.
mysql --user=maxppgco --password=myPass -D 'maxppgco_drp1' -e "SELECT * FROM maxppgco_drp1.`users` WHERE 1";
the error I get is:
ERROR 1146 (42S02) at line 1: Table 'maxppgco_drp1.maxppgco' doesn't exist
How do I properly select the table. The query works in PHPmyAdmin.
EDIT::::Solution::::
mysql --user=maxppgco --password=myPass -e "USE maxppgco maxppgco_drp1; SELECT * WHERE 1";
Need to choose database in sql -e statement with USE statement.. not in the command line with -D
Try,mysql --user=maxppgco --password=myPass -D 'maxppgco_drp1' -e "use maxppgco_drp1;SELECT * FROM maxppgco_drp1 WHERE maxppgco_drp1.'users' = 1";
I think the problem is that your table is not called maxppgco.users but maxppgco. Users is probably a column that is part of your WHERE cause.
In one statement mysql --user=maxppgco --password=myPass -D maxppgco_drp1 -e "SELECT * FROM maxppgco_drp1 WHERE maxppgco_drp1.users = 1";.
Or perhaps breaking up the statement works.
Login with your username / password (mysql --user=maxppgco
--password=myPass) or (mysql -u maxppgco -p).
Type in your password "myPass".
Connect to you database maxppgco_drp1 (connect
maxppgco_drp1).
Execute your SQL statement (SELECT * FROM
maxppgco_drp1 WHERE maxppgco_drp1.'users' = 1).