Why the first and last command line returns null??
$ mysql -u root -ppass -Bse "SELECT ENCRYPT('p#ssW0rd', 'ñsñsñsñ');"
NULL
$ mysql -u root -ppass -Bse "SELECT ENCRYPT('p#ssW0rd', 'dafdadsfe');"
dac0rB9hTC86M
$ mysql -u root -ppass -Bse "SELECT ENCRYPT('p#ssW0rd', 'dafdadsfeñññ');"
dac0rB9hTC86M
$ mysql -u root -ppass -Bse "SELECT ENCRYPT('p#ssW0rd', CONCAT('6', SUBSTRING(SHA(RAND()), -16)));"
63YRpxd2B8u1A
$ mysql -u root -ppass -Bse "SELECT ENCRYPT('p#ssW0rd', CONCAT('$6', SUBSTRING(SHA(RAND()), -16)));"
c7hTUIQiUwO02
$ mysql -u root -ppass -Bse "SELECT ENCRYPT('p#ssW0rd', CONCAT('$6$', SUBSTRING(SHA(RAND()), -16)));"
NULL
I've also executed:
$ mysql -u root -ppass -Bse "SELECT CONCAT('$6$', SUBSTRING(SHA(RAND()), -16));"
$7b07151fc5373796
And then here it comes the magic:
MariaDB [(none)]> SELECT ENCRYPT('p#ssW0rd', 'ñsñsñsñ');
+------------------------------------+
| ENCRYPT('p#ssW0rd', 'ñsñsñsñ') |
+------------------------------------+
| NULL |
+------------------------------------+
1 row in set (0.00 sec)
MariaDB [(none)]> SELECT ENCRYPT('p#ssW0rd', CONCAT('$6$', SUBSTRING(SHA(RAND()), -16)));
+------------------------------------------------------------------------------------------------------------+
| ENCRYPT('p#ssW0rd', CONCAT('$6$', SUBSTRING(SHA(RAND()), -16))) |
+------------------------------------------------------------------------------------------------------------+
| $6$0edfe4f3541e5035$kYDyVhQ2sEHDlZE2GsNX0s2Xstg8z7Mj.D3ly.A0DzItg/5WIGoZldKQ0uSiaW9X/ljmmjGbWkMJwsY/WZ4/p. |
+------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
This is very annoying as I would like to insert from command line into a table which has password field not null:
$ mysql -u root -ppass -Bse "INSERT INTO servermail.virtual_users (id, domain_id, password , email) VALUES ('1', '1', ENCRYPT('s1perP#$$', CONCAT('$6$', SUBSTRING(SHA(RAND()), -16))), 'new#user.com');"
ERROR 1048 (23000) at line 1: Column 'password' cannot be null
$ "INSERT INTO servermail.virtual_users (id, domain_id, password , email) VALUES ('1', '1', ENCRYPT('s1perP#$$', CONCAT('$6$', SUBSTRING(SHA(RAND()), -16))), 'new#user.com');" | mysql -u root -ppass
ERROR 1048 (23000) at line 1: Column 'password' cannot be null
First: Don't use the MySQL ENCRYPT() function. By using it, you are sending unencrypted passwords to your database server, which may expose them to compromise (particularly if your database server is on a remote machine). It will no longer be available in future versions of MySQL, so it would be unwise to use in a new application.
Second: If you are going to use the ENCRYPT() function despite my warnings, you need to understand how it uses the salt argument. Randomly chosen values are unlikely to be valid, especially if they contain non-ASCII characters like ñ, and will cause the function to return NULL. If you do not have a salt, leave that argument out to use a randomly generated salt, instead of trying to generate one yourself.
#David pointet in the right direction: your problem has nothing to do with MySQL - you're just not using Shell escaping right!
"crypt('$6', ..." is evaluated by the Shell to "crypt('', ..." which, as well as "crypt('6', ..." is treated as traditional DES salt.
"crypt('$6$', ..." is evaluated to "crypt('$', ..." which gives NULL as "$" is not a valid character for salt.
For the first examples, if the salt does not start with "$", it's regarded as tradional DES salt and only the first two characters are taken. They must be of [a-zA-Z0-9./]. Thus the first special chars give NULL but in the third example they are irrelevant.
See the crypt(3) manpage for more information about the salts.
[edit:]
Better ways to write the commands:
a) Quote $ characters:
mysql -Bse "SELECT ENCRYPT('p#ssW0rd', CONCAT('\$6\$', SUBSTRING(SHA(RAND()), -16)));"
b) Use single-qutoes in shell and double quotes in SQL even if that's not strict to the SQL standard (MySQL accepts it, Postgres wont)
mysql -Bse 'SELECT ENCRYPT("p#ssW0rd", CONCAT("$6$", SUBSTRING(SHA(RAND()), -16)));'
c) write the SQL into a file and use
mysql < my.sql
Related
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
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
I am trying to copy a table "table1" from "db1" on "server1" to "db1" on "server2". Here is what I attempted:
mysqldump -u USER -pPASSWORD --single-transaction db1 table1 \ | mysql --host=SERVER1 -u USER -pPASSWORD db1 table1;
My username and password on both servers are the same. Database name and table name on both servers are same.
But this returns the warnings:
Warning: Using unique option prefix database instead of databases is deprecated and will be removed in a future release. Please use the full name instead.
Warning: mysqldump: ignoring option '--databases' due to invalid value ''
mysqldump: Couldn't find table: "table1"
Try this:
mysqldump -u -p db_name table_name > table_name.sql
Now take this table_name.sql file to server2, create a database (db_name), exit from mysql command line and use the following command:
mysql -p -u db_name < table_name.sql
The following worked:
mysqldump -u USER -pPASSWORD --single-transaction --add-drop-table db1 table1 | mysql --host=SERVER1 -u USER -pPASSWORD db1
I shouldn't have specified the table name in the end and use add-drop table after single-transaction!
I am having a simple shell script which will grep a sequence from the log and displays the result on terminal when i run the script. How can i store this result into a MySQL table directly in the script itself?
For Example:
#!/bin/bash
echo "Enter the two numbers to be added:"
read n1
read n2
answer=$(($n1+$n2))
echo $answer
In the above example, how can i store n1, n2 and answer values in a database table.
Create an insert query, and pipe it to stdin of mysql.
echo "insert into myTable values ($n1, $n2, $answer);" | mysql -u .. -h ..
First you need to have a table to hold the value of n1, n2, answer
suppose you have "mytable" in database "test" with 3 columns for n1 n2 and answer, then just insert like this:
echo "insert into test.mytable values ($n1, $n2, $n1+$n2);" | mysql -h hostname -u username -ppassword
or
mysql -h hostname -u username -ppassword -e "insert into test.mytable values ($n1, $n2, $n1+$n2);"
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;'