Document not found! Please try again

rename a mysql database

11 downloads 0 Views 150KB Size Report
2. mv /var/lib/mysql/mvc /var/lib/mysql/pepa rename source-file as target-file. 3. ls -la /var/lib/mysql/. 4. service mysql restart. 5. open phpmyadmin o write mysql.
1

RENAME A MYSQL DATABASE Jorge Domínguez Chávez Universidad Politécnica Territorial del estado Aragua “Federico Brito Figueroa” Departamento de Informática [email protected]

Abstract. A database administrator (DBA) task, is to rename a database, but not a situation originated by the motives you want. Such a command does NOT exist in MySQL or was deleted for security or just does not work or do not know how to use it. It presents a manual procedure and a script, renombrar_db.sh, which does not need the MySQL daemon, nor does it need credentials. The user who executes the command must be allowed to descend into that directory or simply be su. Keywords. MySQL, DBA, Linux, Bash

For additional information write to the author via email [email protected].

II. METHOD The following processes have been performed on a Pc, with 2 GB ram, 320 GB hard drive, CoreDuo 1.27 processor, Debian Jessie 8.7 64-bit as operating system, MySQL See 14.14 Release 5.5.54, for Debian-LinuxGnu (x86_64) using readline 6.3. You must work, from console or terminal, as superuser or su. To rename a MySQL database, from console or terminal, execute:

I. INTRODUCTION One of the tasks of a database administrator (DBA), is to rename a database, situation originated by the reasons you want. For sure, the first thing that occurs to the DBA is to use the command: RENAME {DATABASE | SCHEMA} db_name TO new_db_name; The MySQL Technical Literature says: This statement was added in MySQL 5.1.7 but was found to be dangerous and was removed in MySQL 5.1.23. It was intended to enable upgrading pre-5.1 databases to use the encoding implemented in 5.1 for mapping database names to database directory names (see https://www.oracle.com/search/results? Ntt=rename+schema+or+database&Dy=1&Nty=1&cat= mysql&Ntk=SI-ALL5). However, use of this statement could result in loss of database contents, which is why it was removed. Do not use RENAME DATABASE in earlier versions in which it is present. Such a command does NOT exist in MySQL or was deleted for security or just does not work or do not know how to use it. See syntax proposed by MySQL manual. The author found alternatives based on a script to create, copy and upload data from one database to another. The script requires that a DB exists and a new name is assigned to the target DB. What if my database has many records or I can not stop the MySQL service for productivity? What solution to implement such a case?

1. ls -la /var/lib/msqyl ls -la count quantity of files in actual directory and subdirectories. Include directories and files whose names begin with a dot (hidden files). Other command is: find . -type f | wc -l 2. mv /var/lib/mysql/mvc /var/lib/mysql/pepa rename source-file as target-file 3. ls -la /var/lib/mysql/ 4. service mysql restart 5. open phpmyadmin o write mysql 6. ready To create a MySQL database, in a console o terminal, execute: 1. ls -la /var/lib/msqyl 2. mkdir /var/lib/mysql/pepa create a directory, follow path 3. chown -R mysql.mysql Change privilegies of files and/or directories. 4. ls -la /var/lib/mysql/ 5. service mysql restart Start or restart MySQL service 6. open phpmyadmin o write MySQL 7. ready

The process of renaming the database is handled

2

through the following BASH script. In this case, /var/lib/mysql is the directory where MySQL is located. In the script presented below you do not need the MySQL daemon, nor do you need credentials. Of course, the user who executes the command must be allowed to descend into that directory or simply be su. Script renombrar_bd.sh #!/bin/bash # Copyright 2017 Jorge Domínguez Chávez (jodocha) # Venezuela # uso: renombra_db.sh origen destino set -e DIRECTORIO="/var/lib/mysql/" if [ $USER != root ]; then echo -e "Error: must be root" echo -e "Close..." fi test ! -d $DIRECTORIO$1 && echo "$1 NO exists" && exit 1 test -d $DIRECTORIO$2 && echo "$2 EXISTS" && exit 1 echo "Working..." mv $DIRECTORIO$1 $DIRECTORIO$2 ls -la $DIRECTORIO$2 service mysql restart echo -e "Finish…"

III. RESULTS Both processes, rename or create a MySQL database. In both processes its result is efficient and effective, as well as safe. The author assumes that MySQL when it starts its work, "reads" the folder / var / lib / mysql and from that reading sets the directory of the databases contained by it. IV. DISCUSSION The existing alternatives to perform this task are consumers of resources and time; Both expensive elements according to the productivity of the database. 1. Dump the database and recover the dump on the new database 2. Move each of the tables to the new database 3. For security reasons the source database is NOT deleted, leaving it to the DBA discretion to do so. 4. Learn how to program Bash.

IV. REFERENCES [1] [2] [3]

Garrels, M. Bash Guide for Beginners, 2010, p. 81. Neves, J. Shell Scripting, 2012. Robbins, A, Beebe, N. Classic Shell Scripting. Oreilly Media

V. BIOGRAPH Jorge Domínguez Chávez Born in Mexico City, D. F. Mexico. Physicist, Master of Science and Doctor of Science, Mention of Software Development by the Universidad Nacional Autónoma de México (UNAM). Resident in Venezuela. His teaching and work experience includes the Faculty of Engineering of the UNAM. The University of the Americas, Puebla, Mexico. Visiting Professor Federal University of Rio Grande do Sul, Brazil. Currently Associate Professor Universidad Politécnica Territorial del Estado Aragua (Venezuela). He has experience as a software developer, database administrator and Linux servers. He has written several books and numerous articles in the area, directs undergraduate and graduate theses at national and international level. He is considered an expert in free software and an excellent lecturer. Domínguez currently holds a master's degree in Data Mining, (online) at the University of Illinois (Urbana-Champaing), a master's degree in Software Development (on-line) at Universite 42 (France), has a master's degree in Software Development (On-line) at Stanford University.