Link to home
Start Free TrialLog in
Avatar of marrowyung
marrowyung

asked on

make linux auto run MySQL when it start

hi,

what is the way to make Linux automatically start MySQL using the command I want whenever it restart?
Avatar of A Q Choudary
A Q Choudary
Flag of India image

You need shell script to restart automatically, try the following script or other related:

#!/bin/bash
/usr/bin/mysqladmin ping| grep 'mysqld is alive' > /dev/null 2>&1
if [ $? != 0 ]
then
    sudo service mysql restart
fi


Hope it work
Hi @marrowyung,

You can add it using chkconfig and I'd also suggest you to follow mysql documentation.
SOLUTION
Avatar of David Favor
David Favor
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of marrowyung
marrowyung

ASKER

David Favor,

"My guess is you're building from source (shudder), else this will be done by default."

yes.

you step seems a bit complicated to me, so shell scripting already enough, right?

KV:

use  systemd, ?

A Q Choudary,

is this command:

 
#!/bin/bash

Open in new window


going to edit anything? this is not vi editor,right ?

I think I can replace this line:

    sudo service mysql restart

Open in new window


to the real command I start MysQL, right?
Follow the documentation.
I type this:

mysqldump --databases sample --single-transaction --user=root --password P@ssw0rd > all_databases.sql


but it said

mysqldump: Got error: 2002: Can't connect to local MySQL server through socket '/mysqldata/mysqldb/mysql.sock' (2) when trying to connect

Open in new window


how can I specify the my.cnf path so that it can read the socket information ?
looks like this is working:

Mysqldump --defaults-file=/opt/MySQL_5_7_19/my.cnf --databases sample --single-transaction --user=root --password > all_databases.sql

Open in new window


but how to specify the restore script to restore as another DB name on the same server? I want to test it.

Mysqldump --defaults-file=/opt/MySQL_5_7_19/my.cnf --databases sample#2 --single-transaction --user=root --password < all_databases.sql

Open in new window


?

usually on the target DB, anything needs to be done before restore can perform ? and by default, all data on the target will be over write ?
This is logical backup... meaning you have sql scripts inside.
Open it and observe.


When you do --databases you also get CREATE DATABASE syntax in the dump file. So you cannot directly post backup sample#1 and restore it as sample#2.

Also, mysqldump is a backup utility. It cannot restore.
What mysqldump generate is an sql file. You can load that against mysql client.

So, restore goes like:
mysql -uUSER -pPWD < yourfilename.sql


- You should review mysqldump documentation page.


About last question of overwriting: Yes. It will overwrite. You will get your answers when you know what it does. Just open that file and read :)
PS: This is not related to the question here but I'm mentioning just so that you know. There is a utility for splitting mysqldump file or say extracting specific database or table - mysqldumpsplitter.
Add your MySql startup script in : /etc/init.d/start_my_app
K V,

"This is logical backup... meaning you have sql scripts inside. "

 this mean there are physical backup too ? or this is just mean item on the disk level?

"When you do --databases you also get CREATE DATABASE syntax in the dump file. So you cannot directly post backup sample#1 and restore it as sample#2."

but I have to edit the .sql file myself to change the create database statement to the DB name  I want, right?

"So, restore goes like:
mysql -uUSER -pPWD < yourfilename.sql"

this has to be on the shell level and not after login to the mysql and see mysql >   ?

so for my example it should be:

 bin/mysql --defaults-file=/opt/MySQL_5_7_19/my.cnf -u root -p < output.sql 

Open in new window



 if I start MysQL using :

 bin/mysql --defaults-file=/opt/MySQL_5_7_19/my.cnf -u root -p

Open in new window

?

"About last question of overwriting: Yes. It will overwrite. You will get your answers when you know what it does. Just open that file and read :)"
what I mean is if the database in the create database statement already exists, so it will just totally overwrite... ?

Prashant Sabnekar,

"/etc/init.d/start_my_app"

there are no such file on init.d folder, has to vi one myself ?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Marrowyung,

"  this mean there are physical backup too ? or this is just mean item on the disk level?"
>> YES (http://kedar.nitty-witty.com/blog/setup-and-configure-mysql-backup-using-holland-and-xtrabackup)

" but I have to edit the .sql file myself to change the create database statement to the DB name  I want, right?"
>> YES.


" this has to be on the shell level and not after login to the mysql and see mysql >   ?"
>> YES.

Now, you don't require to specify defaults file to mysql client.
Also I suppose /bin should be there in your path so only mysql should do but again work as you feel confirtable.

"  if I start MysQL using :"
No, you don't start mysql like that.
mysql - is client utility to connect mysql server
mysqld - is the daemon.


"so it will just totally overwrite... ?"
>> YES. I said that there.


About init file. You should see in the installation / basedir there you should be provided with support-files.
under support file you will have file: mysql.server
Look for that file and:

cp mysql.server /etc/init.d/
vi mysql.server
-- find datadir and basedir and give the appropriate paths.

Refer documentation for more.
Also, if you don't have specific needs, you don't need to compile and install.
Prashant Sabnekar,,

then I am not sure when Linux is installed, this file is not created automatically.

K V,

"No, you don't start mysql like that.
mysql - is client utility to connect mysql server
mysqld - is the daemon."

I always start mysql using that command or it said socket is not found.

"under support file you will have file: mysql.server
Look for that file and:

cp mysql.server /etc/init.d/
vi mysql.server
-- find datadir and basedir and give the appropriate paths."

when I tried to find the server using which, it said:

which mysql.server
/usr/bin/which: no mysql.server in (/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/home/support/.local/bin:/home/support/bin:/opt/MySQL_5_7_19/bin)

Open in new window


"Also, if you don't have specific needs, you don't need to compile and install."

sorry compile and install what ?
what is the diff between:

/etc/init.d/start_my_app

Open in new window


and

mysql.server

Open in new window


?

why do the same thing  ?
" I always start mysql using that command or it said socket is not found."
- Using mysql client you're not starting mysql. You are connecting client to the server. So what I mentioned was for mysql server.
- About socket, there's option "--socket=/path/to/socket.sock" that you can specify.
On a side-note, it is advisable to follow standard documentation steps. It is well written.



" when I tried to find the server using which, it said:..."
- You don't find using which. It is not a binary or something under PATH by default.
- It is a file ships with mysql installation, you can find it under setup files.
- May be you can use updatedb && locate mysql.server
(How about you referring that documentation link above? It will clear your doubts. Just in case if you wish...)


" sorry compile and install what ? "
- Ignore that comment, I was mentioning your mysql installation method. But don't consider that comment now.
KV,

how can I edit the .sql file I export and change the database name in order to create a new one to test the restore ?

must use vi editor for it ?
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
hi,

tks all. I will come back later on as I am testing sth else.