MySQL Tutorial/MySQL Utilities/mysql

Материал из SQL эксперт
Перейти к: навигация, поиск

Continue even if errors in batch mode, you should use the --force command-line option

shell> mysql --force < batch-file


If anonymous (unnamed) user is allowed, you should be able to connect to that server by invoking mysql without any options:

shell> mysql


If you are logging in on the same machine that MySQL is running on, you can omit the host.

shell> mysql -u user -p


If you need to specify connection parameters on the command line in batch mode

shell> mysql -h host -u user -p < batch-file


Logging In with mysql

$ mysql -u <username> -p


Non-Interactive Commands

Just do a quick look up on a table without logging into the client, running the query then logging back out again.

You can instead just type one line using the " -e " flag.



c:\mysql\bin\mysql -u admin -p myDatabase -e "SELECT * FROM employee"


Remote Client Connection

If you have set the privileges to allow remote connections to a database, you can connect from a remote command line client by using the -h flag:



c:\mysql\bin\mysql -u <username> -p -h <host>


Run the output through a pager

shell> mysql < batch-file | more


Supply your password on the command line after the -p option.

You must do so with no intervening space



mysql> -pmypassword, not as -p mypassword


To escape some special characters in the file

shell> mysql -e "source batch-file"


To see a list of options provided by mysql, invoke it with the --help option:

shell> mysql --help


Use scripts from the mysql prompt by using the source command or \. command

mysql> source filename;
mysql> \. filename

mysql>


Using mysql in Batch Mode

Put the commands you want to run in a file.



shell> mysql < batch-file


You can catch the output in a file for further processing

shell> mysql < batch-file > mysql.out


You can select the database on the command line when you invoke mysql.

You must select database for use each time you begin a mysql session.



mysql>
shell> mysql -h host -u user -p yourDatabaseName
mysql>