简体   繁体   中英

bash script command to inject a schema.sql into mysql db

i found this code but do not quite understand what the command is doing.

sudo -u test-user mysql -U test_traffic traffic < ./phoenix/data/sql/lib.model.schema.sql

i know the last part is using lib.model.schema.sql to create the tables and fields the first part i dont quite understand: sudo -u test-user mysql -U test_traffic traffic

i know the command sudo and mysql

please explain? thanks

Let's look at it bit by bit. Firstly the format

sudo -u username command

is an instruction to run command (which might be simple or complex) as the user username . So in your example, you are running the mysql command as the user test-user . You should note that this includes all the parameters to the mysql command - that's the entire rest of the line.

The command

mysql -U test_traffic traffic < ./phoenix/data/sql/lib.model.schema.sql

appears corrupt (certainly running it on 5.0.51a fails). It would make sense if the -U was a -u which would indicate that that the command was to be executed for mysql user test_traffic . If it was a -u you would then have an instruction to import the sql file into the traffic database.

So the combined instruction says, import the lib.model.schema.sql file into the database test_traffic using the mysql user test_traffic and executing the entire command as if you were logged-in as the user test-user .

Try Below steps for mysql:

   mysql > -h hostname  -u username -p password

    mysql > use databasename;

    mysql > source path/to/scriptfile

If you want to inject theschema.sql file into your database, with a shell script, simply use :

mysql -h [host] -u [username] -p[password] -D [database] < your_file

If you want to dynamicly tell which file should be loaded, replace your_file by $1 and pass the name of the file as an argument to your script.

Take care also to the -p option. There is no space between the -p and your password.

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