3

So I have a box that includes a mysqldump file executed at provision. The problem now is that I need to increase the max_allowed_packet variable before execute this script.

How can I do something like:

max_allowed_packet = 128M >> /etc/my.cnf

but rather than at the EOF do it after the [mysqld] section

Thank you

Alwin Kesler
  • 1,450
  • 1
  • 20
  • 41

2 Answers2

4

With GNU sed:

sed -i 's|\[mysqld\]|&\nmax_allowed_packet = 128M|' /etc/my.cnf

-i: edit file "in place"

&: refer to that portion which matched

\n: new line

Community
  • 1
  • 1
Cyrus
  • 84,225
  • 14
  • 89
  • 153
  • Thank you @Cyrus. You sollution outputs but didn't write. With your hint, I found http://stackoverflow.com/questions/5171901/sed-command-find-and-replace-in-file-and-overwrite-file-doesnt-work-it-empties#answer-5171935 and now I'm done – Alwin Kesler Feb 08 '16 at 19:25
  • I suppose you do not use GNU sed. – Cyrus Feb 08 '16 at 21:06
0

Thanks to @Cyrus

sed -i.bak 's|\[mysqld\]|&\nmax_allowed_packet = 128M|' /etc/my.cnf
Cyrus
  • 84,225
  • 14
  • 89
  • 153
Alwin Kesler
  • 1,450
  • 1
  • 20
  • 41