# MySQL ## mysqldump备份数据库 ### 备份实例下的所有库 ```shell mysqldump -uroot -p -A > all.sql ``` ### 备份单个指定数据库 ```shell mysqldump -uroot -p test > test.sql ``` ### 备份多个指定数据库 ```shell mysqldump -uroot -p test1 test2 > test12.sql ``` ### 备份指定数据库中的单个表 ```shell mysqldump -uroot -p test user > test.user.sql ``` ### 备份指定数据库中的多个表 ```shell mysqldump -uroot -p test user role > test.ur.sql ``` ### 备份数据库表结构只包含DDL语句 ```shell # --no-data 或 -d mysqldump -uroot -p test --no-data > test.sql ``` ### 备份数据库带库名 ```shell mysqldump -uroot -p -B test > test.sql ``` ## Windows 下安装 绿色版 先下载[MySQL :: Download MySQL Community Server](https://dev.mysql.com/downloads/mysql/) ![image-20231007124717167](https://markdownhexo.oss-cn-hangzhou.aliyuncs.com/img/image-20231007124717167.png) 1. 解压下载好的压缩包 ![在这里插入图片描述](https://img-blog.csdnimg.cn/20210120105901505.png#pic_left) 2. 解压后得到 ![在这里插入图片描述](https://img-blog.csdnimg.cn/20210120105247435.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L0NXZVFpbg==,size_16,color_FFFFFF,t_70#pic_center) 3. 新建一个 `my.ini`文件 ![在这里插入图片描述](https://img-blog.csdnimg.cn/20210120110525427.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L0NXZVFpbg==,size_16,color_FFFFFF,t_70#pic_center) 4. 解压后的mysql根目录下没有my.ini文件,自己去网上找一份就可或者使用我在后面给出的代码。.ini文件会在初始化mysql中用到 ```ini # For advice on how to change settings please see # http=//dev.mysql.com/doc/refman/5.7/en/server-configuration-defaults.html # *** DO NOT EDIT THIS FILE. It's a template which will be copied to the # *** default location during install, and will be replaced if you # *** upgrade to a newer version of MySQL. [client] port = 3306 default-character-set = utf8 [mysqld] # Remove leading # and set to the amount of RAM for the most important data # cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%. # innodb_buffer_pool_size = 128M # Remove leading # to turn on a very important data integrity option= logging # changes to the binary log between backups. # log_bin port = 3306 # These are commonly set, remove the # and set as required. basedir="D:\app\mysql-5.7.43-winx64" datadir="D:\app\mysql-5.7.43-winx64\data" # server_id = ..... character_set_server = utf8 # Remove leading # to set options mainly useful for reporting servers. # The server defaults are faster for transactions and fast SELECTs. # Adjust sizes as needed, experiment to find the optimal values. # join_buffer_size = 128M # sort_buffer_size = 2M # read_rnd_buffer_size = 2M sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES ``` 5. 修改ini配置文件中的安装目录和数据存放目录修改为mysql文件的路径 6. \#设置mysql的安装目录 basedir=D:\app\mysql-5.7.43-winx64 \#设置mysql数据库的数据的存放目录 datadir=D:\app\mysql-5.7.43-winx64\data 7. 打开cmd,初始化数据库 ```powershell mysqld --initialize ``` 8. 初始化完成后,mysqld根目录下会自动新增data文件夹 ![在这里插入图片描述](https://img-blog.csdnimg.cn/20210120153744844.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L0NXZVFpbg==,size_16,color_FFFFFF,t_70#pic_center) 9. 打开data文件夹,找到.err后缀文本打开 ![在这里插入图片描述](https://img-blog.csdnimg.cn/20210120153933128.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L0NXZVFpbg==,size_16,color_FFFFFF,t_70#pic_center) 10. 找到文件password位置,红色框中为数据库初始化密码,后续修改初始化密码使用 ```err 2023-10-07T04:37:02.330654Z 1 [Note] A temporary password is generated for root@localhost: (iw?Mw:Vs7n& ``` 11. 安装数据库 ```powershell mysqld --install ``` 12. 启动服务 ```powershell net start mysql ``` 13. 关闭服务 ```powershell net stop mysql ``` 14. 修改初始密码 - 登录 ```powershell mysql -uroot -p'你的初始密码,步骤4中红框里的字符' ``` - 修改密码为 123456 ```mysql ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'root'; ``` 15. 服务卸载 ```powershell net stop mysql mysqld --remove ```