-2

Can we connect database by terminal by one line. because i'm trying to connect MySQL by terminal from PHP exec(). Here i want to import one MySQL file so i tried following code but it asks password on execution time. so i need help. I want to connect db in one line with username and password.

mysql -u username -p database_name < file.sql
7
  • 6
    WHY??? You can access a MySQL database from the PHP code direct, much more efficiently and without the complexity of an exec() call Commented Mar 29, 2019 at 9:28
  • yes but the problem is i need to execute one big .sql file in another database. Here i'm using codeigniter framework. The problem is we are creating that another database in this process only. Commented Mar 29, 2019 at 9:31
  • 3
    Then import it and execute it line by line. Using exec() is usually a bad idea if you can avoid it. Commented Mar 29, 2019 at 9:37
  • Possible duplicate of Calling MySQL exe using PHP exec doesn't work Commented Mar 29, 2019 at 9:38
  • If it is a big sql file, then access MySQL directly from cli or a proper desktop client, not through php. Commented Mar 29, 2019 at 9:40

2 Answers 2

3

mysql -h your_host -u your_username -pyour_password -P your_port dbname

-p and your_password without space.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you very much
This worked for me. Note that -p and your_password are without any space.
0

Please be careful while doing such kinds of operations. I am giving you one of the solutions to import your database in a single line.

1- Create one configuration file which includes user and password of the database.

[client]user=root password=redhat

2-Fire this command mysql --defaults-extra-file= pathtothenewconfig file [all my other options]

mysql --defaults-extra-file=db.php databasename < /home/pratikesh/Downloads/localdb.sql

Hopes it will works

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.