Shell Development basics: Prepare 1 million pieces of test data in MySQL

Source: Internet
Author: User
I recently think it is amazing to use the shell language to meet some business needs. -- Shell charm is waiting for us to discover. Case study: insert 1 million records for testing, mainly to see the power of the index. Necessity it turned out that I had some server-side scripts to run. I first thought about PhP, And then I was thinking about shell, because it was strange to use it. But today, I am eager to use shell Code After writing. Look back after completion. Well, it's really nice. I think it is very necessary for me to learn shell. 1. You can use shell to solve many problems. For example, regular file cutting, multiple commands are executed together. 2. Be familiar with Linux commands when writing shell. You will have a better understanding of the Linux system. -- It is helpful for you to learn about Linux anyway. 3. Simplify many operations. Program You can solve the problem by using shell. The above is purely a personal opinion. If you have any objection, you are welcome to make a picture. Create a table

You can write an SQL file separately and use "mysql-uroot-proot-dyouku <test. SQL" to solve the problem.

Modify

You have to execute it again to create the table. This method is used by many people, but it is too troublesome to execute so many commands on the command line every time. Therefore, it is more convenient to put it in a shell file. Put in an independent File

Some people like to put the MySQL command in a non-path file, so I suggest executing MYSQL as a variable.

Use Variables

Control Input I want to do more perfectly, can we manually enter the user name, password and database?

When I enter this password, if someone else looks at it, the password will not be lost. Change it to a hidden mode.

Hide my password input

If I do not know what to do with the database, it is better to display all the databases.

The first thing that comes to mind is the function. -- Function category.

Prepare 1 million data records

To be more fun, I separated the table creation method from the Insert table method and controlled it through the command line. This is the final code.
#/Bin/ Bashmysql = " /Usr/bin/MySQL  "  Input () {# enter the user name  Echo   "  ##### Please enter your username [enter = root] #####  "  Read Username  If [ "  $ Usename  " = "" ]; Then  Username ="  Root  "      Fi  # Password loss  Echo   "  ##### Please enter your password [enter = root] #####  "  Stty - Echo  Read Password  If [ "  $ Password  " ="" ]; Then  Password = "  Root  "      Fi  Stty  Echo  # Selecting a database  Echo   "  ##### Please select your database [enter = Youku] #####  "  DBS = " '$ Mysql-U $ username-p $ password-hlocalhost -- batch -- skip-column-names-e 'show databases' | SED's // %/G''  "      Select DB In  $ DBs;  Do  Break  Done  # Execute} execute () {Input $ mysql -U $ username-p $ password-d $ db < Test. SQL} insert () {input insert_ SQL = "  Insert into t_user (username, password) Values "      For I In $ ( Seq   1   1000000  )  Do  Insert_ SQL = "  $ {Insert_ SQL}  ""  ('Chuan shanjia $ I ', MD5 ('chuan shanjia $ I ')),  "      Done          Echo $ {Insert_ SQL: 0 :- 1  } $ Mysql -Hlocalhost-U $ username-p $ password-d $ db-e "  $ {Insert_ SQL: 0:-1}  "  }  Case   "  $1  "   In      '  Insert  '  ) Clear  Insert ;;  '  Create  '  )  Clear  Execute ;; * )  Echo   "  Usage: dbtool: insert or create!  "  ;;  Esac  Exit 0 

Running Mode

 

Summary: Article , Maybe it will say, "What is it? ." -- Well, everyone should have the right to review basic knowledge! If you want to learn about the basics of shell programming, we suggest you read this article.
Recommendation
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.