简体   繁体   English

如何创建没有密码的 MySQL 用户 - 远程登录需要 - 用于 bash 插入脚本?

[英]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:在我的远程空间,我使用 bash 脚本进行插入,类似于:

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.问题是每次插入都需要将近 4 秒的时间,除了每次插入时的身份验证之外,我无法确定问题的原因。 So, if I could create a user in MySQL with minimal authentication involved...Something like:所以,如果我可以在 MySQL 中创建一个用户,并且涉及最少的身份验证......类似于:

# 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:只需删除IDENTIFIED BY部分:

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:您最好限制此用户允许的IP范围:

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:您可以通过使用密码创建用户,然后将 a.my.cnf 文件放置在运行包含以下内容的 bash 脚本的帐户的主目录中来执行此操作:

[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.我认为您的问题在于您正在为每个插入启动 mysql 客户端。 You should be doing your inserts from a php, java, etc program - not from a shell script.您应该从 php、java 等程序进行插入,而不是从 shell 脚本。

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.我通常每分钟从 php 或 java 程序到 MySQL 数据库执行 1000 次插入,在小型(CPU/内存)机器上具有数百万条记录。

First off, using a client cnf file on the remote machine running the script wont speed this up.首先,在运行脚本的远程机器上使用客户端 cnf 文件不会加快速度。 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. MySQL 客户端仍在发送登录信息并为每个插入登录,它只是读取 uid/pw 的文件,而不是使用 cmd 行 arguments。 AFAIK The network and authentication overhead are identical. AFAIK 网络和身份验证开销是相同的。 Even the network packet contents will be the same.甚至网络数据包的内容也是一样的。

You should still use a cnf file..您仍然应该使用cnf文件..

The way to.improve performance is to do multi-line linserts:提高性能的方法是做多行 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).我建议您创建一个没有密码但仅具有一些权限的用户(插入特定表或特定数据库)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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