-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
Promise Preston
  • 24,334
  • 12
  • 145
  • 143
D. Surendar
  • 74
  • 1
  • 9
  • 6
    WHY??? You can access a MySQL database from the PHP code direct, much more efficiently and without the complexity of an `exec()` call – RiggsFolly Mar 29 '19 at 09: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. – D. Surendar Mar 29 '19 at 09:31
  • 3
    Then import it and execute it line by line. Using `exec()` is usually a bad idea if you can avoid it. – Qirel Mar 29 '19 at 09:37
  • Possible duplicate of [Calling MySQL exe using PHP exec doesn't work](https://stackoverflow.com/questions/814586/calling-mysql-exe-using-php-exec-doesnt-work) – Yassine CHABLI Mar 29 '19 at 09:38
  • If it is a big sql file, then access MySQL directly from cli or a proper desktop client, not through php. – Shadow Mar 29 '19 at 09:40
  • Is there multiple databases or only one password. I haven't tested but I think .my.cnf could work here also. Look https://easyengine.io/tutorials/mysql/mycnf-preference – T.Nylund Mar 29 '19 at 09:43
  • I got. Following is working fine. mysql -u usernmae -p'password' dbname < file.sql – D. Surendar Mar 29 '19 at 10:01

2 Answers2

3

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

-p and your_password without space.

StevenTan
  • 111
  • 5
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

Pratikesh
  • 12
  • 1