备份原数据库:使用 MySQL 自带的 mysqldump 工具将源数据库备份到一个 SQL 文件中,例如
$ mysqldump -uroot -p smart_energy > /home/user/sqlCopy/smart_energy.sql
其中,root 是 MySQL 用户名,smart_enery 是要备份的数据库名称,smart_energy.sql 是备份文件名。
将备份文件复制到目标机器:可以使用 scp 命令或其他工具复制备份文件到目标机器上,例如:
$ scp smart_energy.sql user@xxx:/path/to/smart_energy.sql
其中,user 是目标机器的用户名,xxx 是目标机器的 IP 或主机名,/path/to/smart_energy.sql 是备份文件在目标机器上的路径。
创建新的数据库:在目标机器上创建一个新的空数据库。
恢复备份数据:使用 mysql 命令将备份数据恢复到新的数据库中,例如:
$ mysql -u root -p newdatabase < smart_energy.sql
其中,root 是 MySQL 用户名,newdatabase 是新的数据库名称,smart_energy.sql 是备份文件名。
配置数据库访问权限:为新的数据库配置合适的访问权限,以确保其他用户可以连接并使用新的数据库。
使用命令登录
mysql -u root -p
#修改密码
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '123456';
#创建远程访问账户
create user 'root'@'%' identified with mysql_native_password by '123456';
#给权限
grant all privileges on *.* to 'root'@'%' with grant option;
#刷新权限
flush privileges;