3

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

[mysqld]

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

[mysqld]

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

[mysqld]

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

effectivly making it look like this:

#start file
[mysqld]
#options here
[mysql.server]
#options
[mysqld_safe]
#options
[mysqld]
log-bin=mysql-bin
server-id=1
Entity_Razer
  • 475
  • 1
  • 5
  • 17

1 Answers1

2

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
quanta
  • 51,413
  • 19
  • 159
  • 217