简体   繁体   中英

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.

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

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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM