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;" 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. |
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)
|
Quote:
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. |
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;" 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 |
Quote:
Code:
var2=$(echo $var | sed -e 's/\./_/g' -e 's/-/_/g' -e 's/ /_/g') Code:
var2=$(echo "vpc-1.2 3" | sed -e 's/\./_/g' -e 's/-/_/g' -e 's/ /_/g') Code:
# mysql -h 127.0.0.1 -u root -p'' -e "CREATE DATABASE $var2;" |
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. |