LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   mysql command line - create database with hyphen, dot and space (https://www.linuxquestions.org/questions/linux-newbie-8/mysql-command-line-create-database-with-hyphen-dot-and-space-4175493799/)

unclesamcrazy 02-05-2014 09:16 AM

mysql command line - create database with hyphen, dot and space
 
I am not able to create mysql database if database name has, hyphen (-), dot (.) or space ( ).
I am running following command.

Code:

# mysql -D mysql -e "CREATE DATABASE $var;"
Now database name is in variable $var, it is not able to create databases.

The databases names are like
data-base
data.base
data base

I do not have to create databases on mysql prompt, I have to create it on shell prompt.

Please help.

pan64 02-05-2014 09:38 AM

see here: https://dev.mysql.com/doc/refman/5.7...-database.html about how can you handle special chars (there are two links to section 9.2 and 9.2.3)

TB0ne 02-05-2014 10:32 AM

Quote:

Originally Posted by unclesamcrazy (Post 5112036)
I am not able to create mysql database if database name has, hyphen (-), dot (.) or space ( ).
I am running following command.
Code:

# mysql -D mysql -e "CREATE DATABASE $var;"
Now database name is in variable $var, it is not able to create databases.

The databases names are like
data-base
data.base
data base

I do not have to create databases on mysql prompt, I have to create it on shell prompt.Please help.

While you *CAN* do what you want, it is a bad idea to do it. Even MySQL recommends strongly against it, and for good reason. The MySQL prompt isn't the only thing that has trouble with those characters...things like backup utilities and other database administration tools will also. Good luck getting things like phpmyadmin to work.

The best way for us to help you, is to tell you not to do it. You're only going to cause yourself MAJOR headaches in the future.

unclesamcrazy 02-05-2014 11:37 PM

Thanks pan64, I read that page before posting my question here. It was not much help for me because I wanted to create database desperately with these special characters (I did not read TB0ne's answer :) )

Thanks TB0ne for your accurate suggestion.
We use phpmyadmin as a mysql client in our organization. Phpmyadmin allows user to create database with these special characters. The problem came forward when a developer created a database name "vpc-1.2 3" (Now I understand how dangerous name was it), generally developers took back up of their all databases frequently using import export facility of phpmyadmin. When I studied it deeply, I found phpmyadmin converted a space ( ) into underscore (_) in the name of exported sql file. Actually problem was not found yet because developer can import any sql file into any database, it is not necessary that sql file name and database name should be same.That's why developer did not know that the sql file name was "vpc-1.2_3.sql" and database name was still "vpc-1.2 3"

Developers run a script frequently which saves every database of phpmyadmin individually in a directory (Ex- database-name1.sql, database-name2.sql, database-name3.sql)

The problem was found when a developer's all mysql databases are dropped , the phpmyadmin was completely empty.
There was another script which imports all databases from sql files. First it creates all databases then it imports them.
Since our script exports sql files and the name of sql file is same as name of the database so it is easy for us to create database and import them using script.
Code:

# mysql -h 127.0.0.1 -u root -p'' -e "CREATE DATABASE $var;"
# mysql "$var" < "/path of/sql/file"

Now sql file name contains hyphens and dots that's why variable contains these special characters but create database does not allow with these characters so simply it does create the database and when database is not created it does not import the sql file in it.
That the problem developer reported
"Your script does not import all databases in phpmyadmin, I have to import 7 databases manually. Here are the list of databases which are skipped."
and in the list, the name was there vpc-1.2 3.

--Thanks & Kind Regards
Sam

eklavya 02-06-2014 05:22 AM

Quote:

Originally Posted by unclesamcrazy (Post 5112036)

Code:

# mysql -D mysql -e "CREATE DATABASE $var;"
Now database name is in variable $var, it is not able to create databases.
The databases names are like
data-base
data.base
data base

If database name is stored in the variable and there are special characters in the database name, you can replace special characters from database name. Use another variable and replace all special characters with underscore. Underscore is allowed by mysql.
Code:

var2=$(echo $var | sed -e 's/\./_/g' -e 's/-/_/g' -e 's/ /_/g')
As you said vpc-1.2 3 is like nightmare for you
Code:

var2=$(echo "vpc-1.2 3"  | sed -e 's/\./_/g' -e 's/-/_/g' -e 's/ /_/g')
var2 will store value vpc_1_2_3 in this case, now you can run your commands.
Code:

# mysql -h 127.0.0.1 -u root -p'' -e "CREATE DATABASE $var2;"
# mysql -h 127.0.0.1 -u root -p'' "$var2" < "/path of/sql/file"

I am not giving you any solution, I am just suggesting you. You can find your own as you are the one who knows about your environment and problem.

unclesamcrazy 02-06-2014 11:40 AM

Thanks Eklavya
Right now I have implemented it temporarily according to your suggestion until I get better solution. It replaces these three special characters with single underscore, double underscore and triple underscore respectively so data-base, data.base or data base will not have same names and it will not give database already created for rest two databases.I wonder why it is friendly with underscore yet it is another special character.
Now developers will not complain that script is not importing all databases.

If script finds a database name with special characters which is going to be replaced, I have echoed a line like this..."Your database 'db-name' contains special character, its name has been changed to 'db_name'. Change in your project's config file according to it"

Here in LQ there are very smart and expert people, you have mentioned some name in your signature or my senior admins will change their mind and help me then I will get perfect solution and I will use it.

Thanks.


All times are GMT -5. The time now is 03:35 AM.