Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I've got a pretty basic question,

I am making a configuration script for mySQL replication between 2 new blank servers, now to make it a bit more failsafe I added the lines:

sed '/log-bin/d' /etc/my.cnf
sed '/server/d' /etc/my.cnf

So that, if there where already config options set by default, it would remove the lines in question.

Now, in the next step of the script I want it to readd those lines, with the options we desire. The only problem I'm having is that the my.cnf file is made up with


like headers, what I'm wondering is (and can't find by means of google) is if these actually matter, of if they are mainly to indicate things.

can I just add

echo "log-bin=mysql-bin" >> /etc/my.cnf
echo "server-id=1" >> /etc/my.cnf

or do they need to be in the section marked with


and if they are, can I "expand" the section by adding another


at the bottom of the file so that you would have 2x this "region" ?

effectivly making it look like this:

#start file
#options here
share|improve this question
up vote 2 down vote accepted

Sure. Options in the group apply specially to that program. For example, [mysqld] apply to the mysqld server, [client] option group is read by all client programs, ...

About your question, sed can insert a line after finding the pattern, try this:

# sed '/\[mysqld\]/ a server-id=1' /etc/my.cnf
  • a command stands for append

It can be rewritten in 2 lines for more easier reading:

# sed '/\[mysqld\]/ a\
> server-id=1' /etc/my.cnf

You probably want to check if the server-id exist first:

# [ $(grep -c server-id /etc/my.cnf) -eq 0 ] && \
    sed '/^\[mysqld\]$/ a server-id=1' /etc/my.cnf
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.