开发者

Create MySQL DB from Command Line

I am trying to create a batch file to create a MySQL Database. So far, none of the information I am finding is working. Just for testing, this is what开发者_高级运维 I am trying...

C:\>mysql -uroot -ppassword < CREATE DATABASE testdb;
C:\>mysql -uroot -ppassword mysql < CREATE DATABASE testdb;

No matter how I put it, I keep getting the error "The system cannot find the file specified". If I just put...

C:\>mysql -uroot -ppassword

It logs into the MySQL prompt fine. What exactly am I doing wrong?


I agree with the other posters, it's much better to put the schema into a file. However, here's how you can do it on the command line:

mysql -uroot -ppassword -e "CREATE DATABASE testdb"


acess as root user :

mysql -u root -p

it asks for password..enter your password

then run the create command like this:

mysql> create database database_name;


It's better to write your MySQL inside a file and then import that file. That way, when you need to run it again (reinstalling or migrating) you have a record of the MySQL to run. The code I use for a file like this is as follows, which destroys anything that's already there, and then creates the database and assigns a dedicated user.

# uninstall if anything's already there
GRANT ALL PRIVILEGES ON *.* TO 'username'@'%';
DROP USER 'username'@'%';
DROP DATABASE IF EXISTS `tablename`;

# create the user
CREATE USER 'username'@'%' IDENTIFIED BY 'password';
CREATE DATABASE IF NOT EXISTS `tablename`;
GRANT ALL PRIVILEGES ON `tablename` . * TO 'username'@'%';


Try putting your sql into a text file, like 'createDb.sql' and then run:

mysql -uroot -ppassword < createDb.sql; 
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜