How to export a MySQL database to JSON?

后端 未结 15 1797
感动是毒
感动是毒 2020-12-04 17:52

I am interested in exporting a subset of values from a MySQL database into a JSON-formatted file on disk.

I found a link that talks about a possible way to do this:

15条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-04 18:07

    If you have Ruby, you can install the mysql2xxxx gem (not the mysql2json gem, which is a different gem):

    $ gem install mysql2xxxx
    

    and then run the command

    $ mysql2json --user=root --password=password --database=database_name --execute "select * from mytable" >mytable.json
    

    The gem also provides mysql2csv and mysql2xml. It's not as fast as mysqldump, but also doesn't suffer from some of mysqldump's weirdnesses (like only being able to dump CSV from the same computer as the MySQL server itself)

提交回复
热议问题