解决问题:重命名在Centos7下MariaDB里创建的数据库,过程非常详细,连小白都能看懂,亲测有效!!!

目录

第一种:重命名使用InnoDB存储引擎的数据库

方法一:使用“rename database <旧数据库名> [to] <新数据库名>;”命令。

方法二:使用mysqldump命令先导出旧数据库数据为备份文件,再将备份文件导入到新数据库。

方法三:编写脚本去重命名所有数据表,确认无误后再删除原数据库。

方法四:手动重命名原数据库里所有的数据表。

第二种:重命名使用MyIASM存储引擎的数据库

方法五:直接修改/var/lib/mysql目录下对应要重命名的数据库目录。

补充:

查询MariaDB数据库支持的所有存储引擎类型

查看MariaDB数据库默认的存储引擎类型


解决问题:重命名在Centos7下MariaDB里创建的数据库

由于重命名数据库名没有单独的命令,所以就需要我们另辟蹊径,这里提供了5种方法,亲测有效。

在重命名前,首先应该考虑MariaDB数据库使用的是哪种存储引擎?这里主要考虑两种最常用的存储引擎InnoDB和MyISAM。


第一种:重命名使用InnoDB存储引擎的数据库

  • 方法一:使用“rename database <旧数据库名> [to] <新数据库名>;”命令。但是,这个方法仅适用于5.1.7到5.1.23版本,后续版本又被取消,官方一般不推荐使用,因为有丢失数据的风险。

MariaDB [(none)]> rename database db_life to db_data;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'database db_life to db_data' at line 1
  • 方法二:使用mysqldump命令先导出旧数据库数据为备份文件,再将备份文件导入到新数据库。如果数据量较大,其备份速度会有所降低,花费较长时间。

1.将db_work数据库导出成备份文件
[root@localhost ~]# mysqldump -uroot -p971224 db_work>db_work.sql

2.创建db_students新数据库,并查看所有数据库
[root@localhost ~]# mysql -h localhost -u root -p
Enter password:
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 6
Server version: 5.5.65-MariaDB MariaDB Server
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> create database if not exists db_students;     #创建db_students新数据库
Query OK, 1 row affected, 1 warning (0.00 sec)

MariaDB [(none)]> show databases;     #查看所有数据库,新数据库创建成功
+--------------------+
| Database           |
+--------------------+
| information_schema |
| db_life            |
| db_students        |
| db_work            |
| mysql              |
| performance_schema |
+--------------------+
6 rows in set (0.00 sec)

3.将db_work数据库备份文件导入到新数据库中,并重建表及数据
[root@localhost ~]# mysql -u root -p db_students use db_students;     #启用新数据库db_students
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed

MariaDB [db_students]> desc tab_students;     #查看表结构,数据正常未丢失
+------------+-------------+------+-----+---------+-------+
| Field      | Type        | Null | Key | Default | Extra |
+------------+-------------+------+-----+---------+-------+
| stu_id     | int(5)      | YES  |     | NULL    |       |
| stu_name   | varchar(10) | YES  |     | NULL    |       |
| stu_gender | char(2)     | YES  |     | NULL    |       |
| stu_age    | int(3)      | YES  |     | NULL    |       |
| stu_tel    | int(15)     | YES  |     | NULL    |       |
| stu_qq     | int(15)     | YES  |     | NULL    |       |
| stu_addr   | varchar(20) | YES  |     | NULL    |       |
+------------+-------------+------+-----+---------+-------+
7 rows in set (0.01 sec)

5.删除原数据库db_work,再次查看所有数据库
MariaDB [db_students]> drop database db_work;
Query OK, 1 row affected (0.04 sec)

MariaDB [db_students]> show databases;     #再次查看所有数据库,旧数据库删除成功
+--------------------+
| Database           |
+--------------------+
| information_schema |
| db_life            |
| db_students        |
| mysql              |
| performance_schema |
+--------------------+
5 rows in set (0.00 sec)
  • 方法三:编写脚本去重命名所有数据表,确认无误后再删除原数据库。

1.查看需要重命名的数据库,以及该数据库下的所有表
MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| db_work            |
| mysql              |
| performance_schema |
+--------------------+
5 rows in set (0.00 sec)

MariaDB [(none)]> use db_work;
Database changed

MariaDB [db_work]> show tables;
+-----------------------+
| Tables_in_db_work     |
+-----------------------+
| tab_students          |
+-----------------------+
1 row in set (0.00 sec)

MariaDB [db_work]> desc tab_students;
+------------+-------------+------+-----+---------+-------+
| Field      | Type        | Null | Key | Default | Extra |
+------------+-------------+------+-----+---------+-------+
| stu_id     | int(5)      | YES  |     | NULL    |       |
| stu_name   | varchar(10) | YES  |     | NULL    |       |
| stu_gender | char(2)     | YES  |     | NULL    |       |
| stu_age    | int(3)      | YES  |     | NULL    |       |
| stu_tel    | int(15)     | YES  |     | NULL    |       |
| stu_qq     | int(15)     | YES  |     | NULL    |       |
| stu_addr   | varchar(20) | YES  |     | NULL    |       |
+------------+-------------+------+-----+---------+-------+
7 rows in set (0.00 sec)
小结:查看原数据库下的所有表以及表结构,为后面验证执行脚本之后原数据库下的所有表和数据都被转移到了新数据库给出参照。

2.创建并编辑/root/db_students.sh脚本文件
[root@localhost ~]# vim /root/db_students.sh
#!/bin/bash
#此方法适用于INNODB存储引擎的数据库重命名
#旧数据库名为db_work,新数据库名db_students

mysql -u root -p971224 -e'create database if not exists db_students'
list_table=$(mysql -u root -p971224 -Nse "select table_name from information_schema.TABLES where TABLE_SCHEMA='db_work'")

for table in $list_table
do
  mysql -u root -p971224 -e "rename table db_work.$table to db_students.$table"
done

3.执行/root/db_students.sh脚本文件
[root@localhost ~]# bash /root/db_students.sh
小结:未显示任何提示信息,说明执行成功,否则脚本文件存在错误,请重新仔细校对检查。

4.登录数据库,并查看新数据库下的所有表和数据是否从原数据库转移过来了
[root@localhost ~]# mysql -h localhost -uroot -p971224
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 12
Server version: 5.5.65-MariaDB MariaDB Server
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> use db_students;     #使用新创建的数据库
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed

MariaDB [db_students]> show tables;     #查看新数据库下的所有表
+-----------------------+
| Tables_in_db_students |
+-----------------------+
| tab_students          |
+-----------------------+
1 row in set (0.00 sec)

MariaDB [db_students]> desc tab_students;     #查看表结构
+------------+-------------+------+-----+---------+-------+
| Field      | Type        | Null | Key | Default | Extra |
+------------+-------------+------+-----+---------+-------+
| stu_id     | int(5)      | YES  |     | NULL    |       |
| stu_name   | varchar(10) | YES  |     | NULL    |       |
| stu_gender | char(2)     | YES  |     | NULL    |       |
| stu_age    | int(3)      | YES  |     | NULL    |       |
| stu_tel    | int(15)     | YES  |     | NULL    |       |
| stu_qq     | int(15)     | YES  |     | NULL    |       |
| stu_addr   | varchar(20) | YES  |     | NULL    |       |
+------------+-------------+------+-----+---------+-------+
7 rows in set (0.00 sec)
小结:旧数据库里的所有表和数据转移成功,数据未丢失

5.查看原数据库里的所有数据表和数据是否存在
MariaDB [db_students]> use db_work;
Database changed

MariaDB [db_work]> show tables;
Empty set (0.00 sec)
小结:原数据库里的数据表已经为空,说明执行了/root/db_students.sh脚本文件之后,db_work数据库的所有表和数据全部移动到了db_students里了,可以删除原数据库了。

6.删除旧数据库
MariaDB [db_work]> drop database db_work;
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| db_students        |
| mysql              |
| performance_schema |
+--------------------+
4 rows in set (0.00 sec)
  • 方法四:手动重命名原数据库里所有的数据表,此方法适用于数据表较少的数据库重命,效率低下。

1.创建新数据库
MariaDB [(none)]> create database db_teachers;
Query OK, 1 row affected (0.01 sec)

2.手动重命名原数据库里所有的数据表
MariaDB [(none)]> rename table db_life.tab_teachers to db_teachers.tab_teachers;
Query OK, 0 rows affected (0.04 sec)

3.删除旧数据库
MariaDB [(none)]> drop database db_life;
Query OK, 0 rows affected (0.04 sec)

4.查看所有数据库和新建数据库下的所有表,验证是否所有数据表转移成功
MariaDB [(none)]> show databases;     #查看所有的数据库
+--------------------+
| Database           |
+--------------------+
| information_schema |
| db_life            |
| db_students        |
| db_teachers        |
| mysql              |
| performance_schema |
+--------------------+
6 rows in set (0.02 sec)

MariaDB [(none)]> use db_teachers;     #使用新数据库
Database changed

MariaDB [db_teachers]> show tables;     #查看新数据库下的所有的数据表
+-----------------------+
| Tables_in_db_teachers |
+-----------------------+
| tab_teachers          |
+-----------------------+
1 row in set (0.01 sec)

第二种:重命名使用MyIASM存储引擎的数据库

  • 方法五:首先关闭mariadb数据库服务,再直接修改/var/lib/mysql目录下对应要重命名的数据库目录,即把/var/lib/mysql/db_life目录名修改为/var/lib/mysql/db_teachers,最后再重启mariadb数据库服务即可完成数据库的重命名了。

1.关闭mariadb数据库服务,并检查其状态
[root@localhost ~]# systemctl stop mariadb
[root@localhost ~]# systemctl status mariadb
● mariadb.service - MariaDB database server
   Loaded: loaded (/usr/lib/systemd/system/mariadb.service; enabled; vendor preset: disabled)
   Active: inactive (dead) since 一 2020-05-04 22:40:20 CST; 8s ago
  Process: 8157 ExecStartPost=/usr/libexec/mariadb-wait-ready $MAINPID (code=exited, status=0/SUCCESS)
  Process: 8156 ExecStart=/usr/bin/mysqld_safe --basedir=/usr (code=exited, status=0/SUCCESS)
  Process: 8121 ExecStartPre=/usr/libexec/mariadb-prepare-db-dir %n (code=exited, status=0/SUCCESS)
Main PID: 8156 (code=exited, status=0/SUCCESS)

5月 04 20:11:27 localhost.localdomain systemd[1]: ...
5月 04 20:11:27 localhost.localdomain mariadb-prepare-db-dir[8121]: ...
5月 04 20:11:27 localhost.localdomain mariadb-prepare-db-dir[8121]: ...
5月 04 20:11:27 localhost.localdomain mysqld_safe[8156]: ...
5月 04 20:11:27 localhost.localdomain mysqld_safe[8156]: ...
5月 04 20:11:29 localhost.localdomain systemd[1]: ...
5月 04 22:40:16 localhost.localdomain systemd[1]: ...
5月 04 22:40:20 localhost.localdomain systemd[1]: ...
Hint: Some lines were ellipsized, use -l to show in full.

2.查看MariaDB目录下创建的数据库目录及文件
[root@localhost ~]# tree /var/lib/mysql
/var/lib/mysql
├── db_life     #均以创建的数据库名作为目录名,只需修改其目录名即可完成对数据库的重命名
│   ├── db.opt
│   ├── tab_teachers.frm
│   ├── tab_teachers.MYD
│   └── tab_teachers.MYI
├── db_students
│   ├── db.opt
│   └── tab_students.frm
……其他部分省略,仅显示db_life和db_students数据库……

3.修改/var/lib/mysql目录下旧数据率名为新数据库名
[root@localhost ~]# mv /var/lib/mysql/db_life /var/lib/mysql/db_teachers

4.再次查看MriaDB目录下创建的数据库目录及文件
[root@localhost ~]# tree /var/lib/mysql
/var/lib/mysql
├── db_students
│   ├── db.opt
│   └── tab_students.frm
├── db_teachers     #原数据库名已经完成修改
│   ├── db.opt
│   ├── tab_teachers.frm
│   ├── tab_teachers.MYD
│   └── tab_teachers.MYI

5.重新开启MariaDB数据库服务,并检查其状态
[root@localhost ~]# systemctl start mariadb
[root@localhost ~]# systemctl status mariadb
● mariadb.service - MariaDB database server
   Loaded: loaded (/usr/lib/systemd/system/mariadb.service; enabled; vendor preset: disabled)
   Active: active (running) since 一 2020-05-04 23:20:20 CST; 7s ago
  Process: 8637 ExecStartPost=/usr/libexec/mariadb-wait-ready $MAINPID (code=exited, status=0/SUCCESS)
  Process: 8600 ExecStartPre=/usr/libexec/mariadb-prepare-db-dir %n (code=exited, status=0/SUCCESS)
Main PID: 8636 (mysqld_safe)
   CGroup: /system.slice/mariadb.service
           ├─8636 /bin/sh /usr/bin/mysqld_safe ...
           └─8809 /usr/libexec/mysqld --basedir...

5月 04 23:20:18 localhost.localdomain systemd[1]: ...
5月 04 23:20:18 localhost.localdomain mariadb-prepare-db-dir[8600]: ...
5月 04 23:20:18 localhost.localdomain mariadb-prepare-db-dir[8600]: ...
5月 04 23:20:18 localhost.localdomain mysqld_safe[8636]: ...
5月 04 23:20:18 localhost.localdomain mysqld_safe[8636]: ...
5月 04 23:20:20 localhost.localdomain systemd[1]: ...
Hint: Some lines were ellipsized, use -l to show in full.

6.进入MariaDB数据库再次查看所有数据库
MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| db_students        |
| db_teachers        |
| mysql              |
| performance_schema |
+--------------------+
5 rows in set (0.09 sec)

补充:

  • 查询MariaDB数据库支持的所有存储引擎类型

MariaDB [(none)]> show engines \G;
*************************** 1. row ***************************
      Engine: InnoDB
     Support: DEFAULT
     Comment: Percona-XtraDB, Supports transactions, row-level locking, and foreign keys
Transactions: YES
          XA: YES
  Savepoints: YES
*************************** 2. row ***************************
      Engine: MRG_MYISAM
     Support: YES
     Comment: Collection of identical MyISAM tables
Transactions: NO
          XA: NO
  Savepoints: NO
*************************** 3. row ***************************
      Engine: MyISAM
     Support: YES
     Comment: Non-transactional engine with good performance and small data footprint
Transactions: NO
          XA: NO
  Savepoints: NO
*************************** 4. row ***************************
      Engine: BLACKHOLE
     Support: YES
     Comment: /dev/null storage engine (anything you write to it disappears)
Transactions: NO
          XA: NO
  Savepoints: NO
*************************** 5. row ***************************
      Engine: PERFORMANCE_SCHEMA
     Support: YES
     Comment: Performance Schema
Transactions: NO
          XA: NO
  Savepoints: NO
*************************** 6. row ***************************
      Engine: CSV
     Support: YES
     Comment: Stores tables as CSV files
Transactions: NO
          XA: NO
  Savepoints: NO
*************************** 7. row ***************************
      Engine: ARCHIVE
     Support: YES
     Comment: gzip-compresses tables for a low storage footprint
Transactions: NO
          XA: NO
  Savepoints: NO
*************************** 8. row ***************************
      Engine: MEMORY
     Support: YES
     Comment: Hash based, stored in memory, useful for temporary tables
Transactions: NO
          XA: NO
  Savepoints: NO
*************************** 9. row ***************************
      Engine: FEDERATED
     Support: YES
     Comment: Allows to access tables on other MariaDB servers, supports transactions and more
Transactions: YES
          XA: NO
  Savepoints: YES
*************************** 10. row ***************************
      Engine: Aria
     Support: YES
     Comment: Crash-safe tables with MyISAM heritage
Transactions: NO
          XA: NO
  Savepoints: NO
10 rows in set (0.01 sec)
  • 查看MariaDB数据库默认的存储引擎类型

MariaDB [(none)]> show variables like 'default_storage_engine';     #查看数据库默认存储引擎类型
+------------------------+--------+
| Variable_name          | Value  |
+------------------------+--------+
| default_storage_engine | InnoDB |
+------------------------+--------+
1 row in set (0.00 sec)

你可能感兴趣的:(解决运维相关问题,mysql,运维,centos)