encrypting password in shell script - mysql

I'm running a shell script on a Ubuntu server, the shell script inserts data to my database (I use MariaDB) for the connection I have a database user and the database user has a password. The problem is the password is plain text, is there a way I can encrypt this password, store it in an other file, or another way so no one can read the password when reading the script?
thanks in advance,
mysql -u db_user -pplain_password <<EOF
USE db_name
INSERT INTO table ()
VALUES ();
EOF

Check out mysql_config_editor. This will allow you to create a .mylogin.cnf file which will store the password in an encrypted form. .mylogin.cnf will be used by your script, or other client program, to connect to the database.

Related

Restoring mysql backup without users

I have a confusion actually. While restoring the mysql backups, we generally use this command.
mysql -u username -p password databasename < backup.sql.
I just tried "mysql databasename < backup.sql" and that seemed to work too. So my confusion is, why do we add username / pass and what are the benefits / disadvantages of using / not using it?
If your MySQL doesn't have blank user and blank password then you couldn't able to import the database.
By default- Mysql installation comes with blank username and password in that case you can restore database without username and password only.
But if you have secure installation of mysql, means removed bkank user than you need to pass privileged username and password to restore the database.
While using mysql command for importing database, you can use either full command or skip some of the parameters.
You can also use :
mysql -u username -p password < backup.sql
This will create the database (if your dump have create database command) and import the tables into that.

How to create a MySQL user without password - needed for remote login - to be used in bash insert script?

My requirement is to create a user for remote login but without a password. At my remote space I use a bash script to do inserts, which is something like:
for i in {1..5000}; do
mysql -h 11.40.3.169 -uUser -pPass -DDatabaseName <<<
"insert into DatabaseTableName values('$i','dummy_$i');" &
echo -n "$i "
sleep 1
date
done
The problem is that each insert is taking almost 4 seconds, and I can not pinpoint the problem to anything but authentication at every insert. So, if I could create a user in MySQL with minimal authentication involved...Something like:
# I'm trying to remove this password
GRANT ALL PRIVILEGES TO 'user'#'%' IDENTIFIED BY 'password';
...Anything you can suggest.
Just remove the IDENTIFIED BY part:
GRANT ALL PRIVILEGES ON *.* TO 'user'#'%'
Note that remote login from anywhere without a password is a very insecure thing. You better limit the allowed IP range for this user:
GRANT ALL PRIVILEGES ON *.* TO 'user'#'allowed_remote_machine'
You can do this by creating a user with a password and then placing a .my.cnf file in the home directory of the account which runs the bash script containing the following:
[mysql]
user=user
password=pass
[mysqladmin]
user=user
password=pass
This might be better than creating a user with no password.
I think your problem lies in the fact that you are starting the mysql client for each insert. You should be doing your inserts from a php, java, etc program - not from a shell script.
The startup time of the client (and connection to the host) is killing you. I routinely do 1000s of inserts per minute from a php or java program to a MySQL database with millions of records on a small (CPU/memory) machine.
It's not so good idea to have a user without password and all privileges. I suggest you to create a user without password but just with some privileges (insert to specific table or specific database).
First off, using a client cnf file on the remote machine running the script wont speed this up. MySQL client is still sending logon information and logging in for each insert, it's just reading.a file for uid/pw instead of using cmd line arguments. AFAIK The network and authentication overhead are identical. Even the network packet contents will be the same.
You should still use a cnf file..
The way to.improve performance is to do multi-line linserts:
MySQL --defaults-file=/some/uid/pw/etc/.client.cnf -e \
"Insert into
tbl_name
('fld1','fld2')
values
('r1-fld1','r1-fld2'),
('r2-fld2','r2-fld2'),
...and so on (up to max_allowed_packet_size)
('r500-fld2','r500-fld2');"
Or READ DATA INFILE on server side after shipping over the data file

Backup MySQL via php, in a Symfony application

I'm trying to do a php script to backup my database.
Here's what I've tried so far :
$command = "mysqldump -u [username] -p [password] [databasename] | gzip > db.sql.gz";
$this->output = system($command);
How do I get the password and
username from databases.yml ?
How can I do a script that sends me
the backup file, instead of saving it
on the server (à la phpmyadmin) ?
You can create a symfony task. If you pass in an environment (i.e. dev, prod) or connection you get access to the Doctrine connection manager and create a connection. You can use that connection to make a database dump or get the connection details from the connection manager.
You can use the doctrine insert sql task as template for your task. I've done similar in the past.

Problem in importing database in MySQL

I have a .sql file with some database backups inside. Now I want to restore them back to MySQL. How can I this using command line of MySqL please? I found this:
mysql -u username -p -h localhost database_name < dumpfile.sql
but I don't know what username should be, what database_name should be and how I could browse to a .sql file in another folder.
You need to replace username with your database username and it will prompt you for a password. If the dump file has the "create database [name];" and "use [name];" instructions then you dont need to specify the database_name attribute.
To pull the .sql from another folder you just need to specify the path (/home/user/Downloads/file.sql, for example).
You could also try downloading mysql administrator from the mysql website.
Check this link too
http://www.techiecorner.com/31/how-to-restore-mysql-database-from-sql-dump-file/
Redirecting a .sql file into the MySQL CLI works because that's the format that mysqldump produces. And people usually call mysqldump to dump a whole database, so they get one file afterwards.
The username and password are dependant on what's been setup on the database instance you want to reload the data in to. On a clean, empty install, the MySQL root user will work (and probably won't have a password). On an established install, you should find an appropriate user. The user you use will need substantial permissions as it needs to create and write to tables.
The .sql file may have CREATE database and USE database statements near the top. If this is present, then make sure that database does not exist before you pipe the file in. If not, you will need to find out what name is expected by whatever program will be using the database.
As for piping another file in in a different directory, this is simple shell notation. The < filename notation fully supports paths so you can do < some/other/path/filename.sql or < ~/sql/filename.sql, for example. (Note that I've assumed you're using a Unix shell.)
You can use cmd
type cmd run as adminstration (C:\windows\system32>)
give path of mysql of bin folder (C:\windows\system32>
cd `C:\xampp\mysql\bin)
C:\xampp\mysql\bin>mysql -u username -p -h localhost database_name
type-> use database_name
type-> source F:/example.sql

mysql restoring data on localmachine where the password is empty

I use a ruby program to download data from staging server and to populate that data to my local mysql server where the userid is root and password is empty.
cmd = "mysql -u #{user} -p'#{password}' my_db < out.sql"
Since password is empty, I get prompted for password. I just have to hit enter. I would like to avoid that. What's the fix?
Omit the -p option.