How make mysql start automatically ? (linux-cli only) - mysql

How can i make mysql start every time the system boot ? I need that in a dedicated server(ubuntu distro) in which i have my blog, but every time the server goes down, on booting mysql is stopped.
Btw i can use only command line.

update-rc.d allows setting init script links on Ubuntu and Debian Linux systems to control what services are run by init when entering various runlevels. It should be able to add mysql to the list of services to run at boot:
sudo update-rc.d mysql defaults
If you later want to disable running mysql on bootup:
sudo update-rc.d mysql remove

You can do it by using sysv-rc-conf, on debian based you can install it with sudo apt-get install sysv-rc-conf
then you can choose what start at boot with a simple X on the name of the deamon, all via command line

Run the following command to see your mysql current status:
/sbin/chkconfig mysqld --list
it will return a line such as below:
mysqld 0:off 1:off 2:off 3:off 4:off 5:off 6:off
to make mysql start every time the system boots, type the following:
sudo /sbin/chkconfig mysqld on
Result now from '--list' is:
mysqld 0:off 1:off 2:on 3:on 4:on 5:on 6:off

No answer helped. Finally chkconfig and update-rc.d did not work with MySQL on my machine.
Solution, I had a file /etc/init/mysql.override which contained
manual
i just deleted that file
$ sudo rm /etc/init/mysql.override

Deleting
/etc/init/mysql.override
did the job in my case (HostEurope VPS with Ubuntu 12.04)

I had the same problem, so I checked how I disabled it in the first place:
https://askubuntu.com/questions/138487/how-to-keep-apache-and-mysql-from-starting-automatically
Check your /etc/init/mysql.conf to make sure you don't have start on commented out (like I did).
# MySQL Service
description "MySQL Server"
author "Mario Limonciello <superm1#ubuntu.com>"
start on runlevel [2345]
stop on starting rc RUNLEVEL=[016]
...
Rebooted the machine and it works.
$ sudo service mysql status
mysql start/running, process 972

Use the chkconfig command as per the manpage http://manpages.ubuntu.com/manpages/maverick/man8/chkconfig.8.html

Another place to look for clues as to what is and isn't starting at boot time...
/etc/init.d/.depend.start (and its buddy at shutdown time, .depend.stop)
Here's a little more info http://www.pyenet.co.nz/2-202-1-customising-system-startup-and-boot-processes/
I had 2 servers - after booting, 1 would have mysql running, the other not so much.
On the box where mysql was starting at boot time:
chkconfig wasn't even installed
there were zero links from any scripts in /etc/rc?.d/* back to /etc/init.d/mysql
BUT... /etc/init.d/.depend.start contained:
TARGETS = halt apache2 umountfs umountnfs.sh sendsigs networking umountroot reboot killprocs unattended-upgrades urandom mysql mdadm dns-clean landscape-client pppd-dns sysstat rsync sudo postfix single grub-common ondemand rc.local
INTERACTIVE = apache2
postfix: mysql
single: killprocs dns-clean pppd-dns
grub-common: apache2 unattended-upgrades postfix mysql mdadm dns-clean landscape-client pppd-dns sysstat rsync sudo
ondemand: apache2 unattended-upgrades postfix mysql mdadm dns-clean landscape-client pppd-dns sysstat rsync sudo
rc.local: apache2 unattended-upgrades postfix mysql mdadm dns-clean landscape-client pppd-dns sysstat rsync sudo
When I simply copied this file over to the problem server, and rebooted, mysql was up & running.

With Debian 9, I installed MySQL today and typed "reboot" and mysqld restarted automatically. Also rebooted from my VPS dashboard, mysqld restarted automatically. In short, if you have Debian 9, there's nothing extra to do, it just works.

Related

Not To Start MySql Automatically at Centos Startup

i have "CentOS 6" VPS and i wanted to start mysql service automatically at Startup of Server when it is restarted. so i used this command in putty
chkconfig --level 345 mysqld on
this command is working and mysql starts on every startup automatically.
BUT how can i now stop this? what if i want to start Mysql manually on every startup, then what command should i use?
also what is the File where i can see the list of programs that are running automatically on every setup.
Thanks
You can turn off auto-start with this command:
chkconfig --level 345 mysqld off
To see what is configured for auto-start, you can run:
chkconfig --list
See more info on chkconfig here:
https://access.redhat.com/documentation/en-US/Red_Hat_Enterprise_Linux/6/html/Deployment_Guide/s2-services-chkconfig.html

mysql_install_db Generic Message Why?

For whatever reason my MySQL server crashed and I had to use mysql_install_db initialise mysql data directory.
This installation worked. However I have to start the server manually every time it reboots.
I have tried to run sudo update-rc.d mysql defaults so that it should start automatically at boot time but the MySQL server does not start.
One thing I can remember is that after finishing installation using mysql_install_db there was this generic message
"To start mysqld at boot time you have to copy
support-files/mysql.server to the right place for your system"
According to some quarters the right place for the system (Ubuntu 14.04.2 LTS (GNU/Linux 3.16.0-77-generic x86_64)) is /etc/init.d/ but the problem at hand the location of
support-files/mysql.server. I have tried $ sudo find / -name mysql.server and outputs nothing. Where is support-files/mysql.server located.
MySQL version is mysql Ver 14.14 Distrib 5.5.50, for debian-linux-gnu (x86_64)
A thousand ways to kill a rat.The mysql server can be started manually by running the following commands:
sudo service mysql stop
sudo /etc/init.d/mysql stop
sudo killall -KILL mysql mysqld_safe mysqld
sudo /etc/init.d/mysql start
sudo service mysql start
Automate these series of commands by putting them in Ubuntu rc.local file. A crontab at boot up may execute the commands prematurely.

mysql.sock does not exist error in fresh install of MySQL on Arch Linux

I'm trying to use MySQL on Arch Linux. it is already installed but this error comes up when I try to connect:
connect to server at 'localhost' failed
error: 'Can't connect to local MySQL server through socket '/run/mysqld/mysqld.sock' (2 "No such file or directory")'
I've looked for /etc/my.cfg but the file does not exist.
Something must have gone wrong during the installation.
How can I "purge" MariaDB and reinstall it?
If you're using archlinux it is a vital idea to understand the package manager (pacman). For the question about /etc/my.cfg you can run
pacman -Ql mariadb
there you will see that the file is actually called:
/etc/mysql/my.cnf
Arch linux will not configure the package for you, that is part of the arch philosophy. It will provide example configurations, and even provide you with a systemd unit file
usr/lib/systemd/system/mysqld.service
but it is your responsibility to ensure that the configuration is correct and actually start the daemon.
systemctl enable mysqld # add the unit file to the boot sequence
systemctl start mysqld # runs ExecStart= in the unit file
systemctl stop mysqld # kills the daemon
systemctl disable mysqld # remove unit from boot sequence
reinstall
Since the word reinstall is in the title of the question and someone might find this question thanks to that: To reinstall mariadb you simply do
pacman -S mariadb
pacman will reinstall a package that is already installed, there is no need to remove the package (for completeness, package removal happens with pacman -R)
as of 7-28-17 I had to do this on a new install. Newbie here might save someone some time. It was a real pain.
OK HERE IS THE DEAL!!!!!
INSTALL APACHE _ NO PROB
INSTALL MYSQL _PROBLEM
pacman -S mysql then before starting service
MUST UNCOMMENT INNODB IN:
nano /etc/mysql/my.cnf
then must initialize datadirectory before starting service:
mysql_install_db --user=mysql --basedir=/usr --datadir=/var/lib/mysql
You need to initialize the MariaDB data directory prior to starting
the service. This can be done with mysql_install_db command, e.g.:
mysql_install_db --user=mysql --basedir=/usr --datadir=/var/lib/mysql
Optional dependencies for mariadb
galera: for MariaDB cluster with Galera WSREP
perl-dbd-mysql: for mysqlhotcopy, mysql_convert_table_format and
mysql_setpermission
CNF file is /etc/mysql/my.cnf in Arch Linux.
One simple way I can reproduce your issue is when MariaDB is shut down. Sorry if it sounds dumb but as you did not mention it: is MariaDB started? sudo systemctl start mysqld.service
You should have a look at MariaDB logs to get some clue: journalctl _SYSTEMD_UNIT=mysqld.service (maybe paste some part if you still don't get what is going on).
This happens the first time you install MySQL and MariaDB. As grochmal pointed out, you have to set up configurations before first use. But, the user teckk sent these three links in the archlinux newbie corner:
https://wiki.archlinux.org/index.php/MariaDB
https://wiki.archlinux.org/index.php/MariaDB#Reset_the_root_password
https://bbs.archlinux.org/viewtopic.php?id=51981
In short, you have to run the command below before starting the service:
sudo mariadb-install-db --user=mysql --basedir=/usr --datadir=/var/lib/mysql
Optionally (recommended) you should improve the initial security by calling:
sudo mysql_secure_installation
Now you can start the service:
sudo systemctl start mariadb
Optionally, you could install and use a graphical front-end tool.
Carry on with setting up the configurations as described in the archwiki post on MariaDB Configuration.

Installing MySQL 5.6 in Amazon Linux Machine

I am trying to install MySQL 5.6 in amazon linux machine. by using following link.
after completing following steps.
sudo yum localinstall http://repo.mysql.com/mysql-community-release-el6-3.noarch.rpm
sudo yum install mysql-community-server
when i am starting mysql services by this command.
sudo service mysqld start
I am getting MySQL Daemon failed to start error.
Remove old mySql
sudo yum remove mysql mysql-server mysql-common mysql-client
remove all pre-installed packages. You can get a list:
rpm -qa | grep -i mysql
Then uninstall all of them. For example:
rpm -e mysql libmysqlclient15-5.0.94-0.2.4.1 <and so on>
Completely remove the /var/lib/mysql folder
cd /var/lib
rm -rf mysql
Installing fresh mySql 5.6 =>
wget http://dev.mysql.com/get/Downloads/MySQL-5.6/MySQL-5.6.23-1.el7.x86_64.rpm-bundle.tar
tar -xvf MySQL-5.6.23-1.el7.x86_64.rpm-bundle.tar
sudo yum -y install MySQL-client-5.6.23-1.el7.x86_64.rpm
sudo yum install MySQL-shared-compat-5.6.23-1.el7.x86_64.rpm
sudo yum install MySQL-server-5.6.23-1.el7.x86_64.rpm
Starting from 2015 Sep, Mysql 5.6 server is now natively available through Amazon yum repos. More info # https://aws.amazon.com/blogs/aws/now-available-amazon-linux-ami-2015-09/
You can now simply install Mysql 5.6 server using
sudo yum install mysql56-server
Then you can simply start/stop/status look using the regular service commands
service mysqld start ( restart | stop | status | )
I couldn't figure out the default root password in Amazon AMI then I simply reset the root user password by starting the mysqld service with skip grant tables
mysqld_safe --skip-grant-tables start
mysql -u root
update user set password=PASSWORD('[New Password]') where User='root';
OR
/usr/libexec/mysql56/mysqladmin -u root password 'new password'
Also, you need to run the following command to upgrade the MySQL database to 5.6
sudo mysql_upgrade -u root -p
I met the same problem on my micro instance. Check your mysql logs
If the issue is because of
InnoDB: Cannot allocate memory for the buffer pool
Adding a swap page might solve the problem. It solved for me.
You can follow this setup
http://www.prowebdev.us/2012/05/amazon-ec2-linux-micro-swap-space.html
I copied the contents if the page does not loads
Amazon EC2 Micro Instance Swap Space - Linux
I have a Amazon EC2 Linux Micro instance. Since Micro instances have only 613MB of memory, MySQL crashed every now and then. After a long search about MySQL, Micro Instance and Memory Managment I found out there is no default SWAP space for Micro instance. So if you want to avoid the crash you may need to setup a swap space for your micro instance. Actually performance wise is better to enable swap.
Steps below show how to make a swap space for your Micro instance. I assume you have AWS Account with a Micro instance running.
Run dd if=/dev/zero of=/swapfile bs=1M count=1024
Run mkswap /swapfile
Run swapon /swapfile
Add this line /swapfile swap swap defaults 0 0 to /etc/fstab
Step 4 is needed if you would like to automatically enable swap file after each reboot.
Some useful command related to SWAP space:
$ swapon -s
$ free -k
$ swapoff -a
$ swapon -a

ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' [duplicate]

This question already has answers here:
Can't connect to local MySQL server through socket '/var/mysql/mysql.sock' (38)
(43 answers)
Closed 21 days ago.
I installed MySQL on Mac OS X Mountain Lion with homebrew install mysql, but when I tried mysql -u root I got the following error:
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)
What does this error mean? How can I fix it?
You'll need to start MySQL before you can use the mysql command on your terminal. To do this, run brew services start mysql. By default, brew installs the MySQL database without a root password. To secure it run: mysql_secure_installation.
To connect run: mysql -uroot. root is the username name here.
This happened after the homebrew install and occurs due to permission issues. The following commands fixed the issue.
sudo chown -R _mysql:mysql /usr/local/var/mysql
sudo mysql.server start
It's probably because MySQL is installed but not yet running. [...] To verify that it's running, open up Activity Monitor and under "All Processes", search and verify you see the process "mysqld".
You can start it by installing "MySQL.prefPane".
The above quote is from the blog Obscured Clarity, "Install MySQL on Mac OS X", where you can also find the complete tutorial that I found most helpful.
Run: brew info mysql
And follow the instructions. From the description in the formula:
Set up databases to run AS YOUR USER ACCOUNT with:
unset TMPDIR
mysql_install_db --verbose --user=`whoami` --basedir="$(brew --prefix mysql)" --datadir=/usr/local/var/mysql --tmpdir=/tmp
To set up base tables in another folder, or use a different user to run
mysqld, view the help for mysql_install_db:
mysql_install_db --help
and view the MySQL documentation:
* http://dev.mysql.com/doc/refman/5.5/en/mysql-install-db.html
* http://dev.mysql.com/doc/refman/5.5/en/default-privileges.html
As others have pointed out this is because MySQL is installed but the service isn't running. There are many ways to start the MySQL service and what worked for me is the below.
To start the service:
Go to "System Preference"
At the bottom pane there should be MySql icon.
Double click that to launch the 'MySQL Server Status' and press the button 'Start MySQL Server'
My env:
Mac Yosemite 10.10.3
Installed Package: /Volumes/mysql-advanced-5.6.24-osx10.8-x86_64
Solutions revolve around:
changing MySQL's permissions
sudo chown -R _mysql:mysql /usr/local/var/mysql
Starting a MySQL process
sudo mysql.server start
Just to add on a lot of great and useful answers that have been provided here and from many different posts, try specifying the host if the above commands did not resolve this issue for you, i.e
mysql -u root -p h127.0.0.1
The designated data directory /usr/local/var/mysql/ is unusable. You can remove all files that the server added to it.
MacOS:
$brew services stop mysql
$brew services list
$brew uninstall mysql
$brew install mysql
$brew postinstall mysql
If Any error found then run those cmd
! Warning: this will delete any databases on this server:
$sudo rm -rf /usr/local/var/mysql
$sudo rm /usr/local/etc/my.cnf
$brew postinstall mysql
$brew services start mysql
$mysql_secure_installation
Completed All process of secure installation then run
$mysql -u root -p
Congratulations you’ve just set up mysql!
Warning - this method will remove all of your databases in the /usr/local/var/mysql folder
I had MySQL installed with Homebrew, and the only thing that fixed this for me was re-installing MySQL.
On my company laptop, I didn't have permission to uninstall MySQL from my computer via Homebrew:
$ brew uninstall mysql --ignore-dependencies
Uninstalling /usr/local/Cellar/mysql/8.0.12... (255 files, 233.0MB)
Error: Permission denied # dir_s_rmdir - /usr/local/Cellar/mysql/8.0.12
So instead, I removed and reinstalled MySQL manually:
$ sudo rm -rf /usr/local/Cellar/mysql
$ brew cleanup
$ sudo rm -rf /usr/local/var/mysql
$ brew install mysql
And that worked!
Looks like your mysql server is not started. I usually run the stop command and then start it again:
mysqld stop
mysql.server start
Same error, and this works for me.
Below I'm including the latest instructions from brew install mysql so newer searches for this issue can benefit:
$ brew install mysql
==> Downloading https://homebrew.bintray.com/bottles/mysql-5.6.26.yosemite.bottle.1.tar.gz
######################################################################## 100.0%
==> Pouring mysql-5.6.26.yosemite.bottle.1.tar.gz
To connect:
mysql -uroot
To have launchd start mysql at login:
ln -sfv /usr/local/opt/mysql/*.plist ~/Library/LaunchAgents
Then to load mysql now:
launchctl load ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist
Or, if you don't want/need launchctl, you can just run:
mysql.server start
In my case I loaded mysql now via launchctl load ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist and was then able to launch $ mysql and be on my way.
This problem related to /usr/local/var/mysql folder access, I remove this folder and reinstall mysql.
uninstall mysql with brew :
brew uninstall mysql
sudo rm -r /usr/local/var/mysql
brew install mysql#8.0
mysql -u root
This solution works fine for me!
BUT YOU LOST ALL YOUR DATABASES! WARNING!
This fixed my issue when I restarted the mysql service. Just run:
brew services start mysql
In my case it was simply a matter of deleting a lock file.
sudo rm -f /tmp/mysql.sock.lock
Ough, that took me a while to figure out. I saw it in a comment. After installing mysql using brew, and starting the service (perhaps using sudo brew services start mysql) then run:
$ mysqld
And MySQL should be running for your thereafter.
For me it was simple as running:
/usr/local/opt/mysql/bin/mysqld_safe
instead of mysqld
I went through this issue and I managed to run mysql server using below solution
Install mysql through .dmg(https://dev.mysql.com/downloads/mysql/5.7.html), you will get mysql service panel in system preferences then start mysql from the panel and try
mysql -u root -p
Images attached for reference
just hit "brew services start mysql" in terminal
I found the solution to my problem. It was indeed because my MySQL server was not running.
It was caused by MySQL not being correctly set up on my machine, thus not being able to run.
To remedy this, I used a script which installs MySQL on Mac OSX Mountain Lion, which must have installed missing files.
Here is the link: http://code.macminivault.com/
Important Note: This script sets the root password as a randomly generated string, which it saves on the Desktop, so take care not to delete this file and to note the password. It also installs MySQL manager in your system preferences. I'm also not sure if removes any existing databases, so be careful about that.
After working on this for several hours what worked for me was
go to /etc/mysql/ and edit the my.cnf file. Add the following
[client]
port = 3306
socket = /var/run/mysqld/mysqld.sock
AFTER USING ALL THE ABOVE SOLUTIONS, NOTHING WORKED FOR ME BUT THIS WORKED.
I you have installed MySQL using HomeBrew then check System Preferences > MySQL in your mac that MySQL Server is stopped if it is running stop it by clicking on Stop MySQL Server and start MySQL from terminal writing the command mysqlserver.start.
If it doesn't work you can also try:-
If you have also installed MySQL workbench then just uninstall MySQL workbench and MySQL and after that install MySQL first and then MySQL workbench.
I would recommend you to run
mysql.server start
before going to
mysql -u root -p
so as to make sure that the mysql server is running before trying to login into it
This happens many times what you start/restart a machine where by no mysql server is running.
I keep coming back to this post, I've encountered this error several times. It might have to do with importing all my databases after doing a fresh install.
I'm using homebrew. The only thing that used to fix it for me:
sudo mkdir /var/mysql
sudo ln -s /tmp/mysql.sock /var/mysql/mysql.sock
This morning, the issue returned after my machine decided to shut down overnight. The only thing that fixed it now was to upgrade mysql.
brew upgrade mysql
After trying many solutions, seems like the one that finally did the trick was to connect by IP. No longer file sockets getting deleted randomly.
Just update your MySQL client config (e.g. /usr/local/etc/my.cnf) with:
[client]
port = 3306
host=127.0.0.1
protocol=tcp
Probably you might have faced some issues during the homebrew mysql installation and mysql services might not be running. If that is the case, then it might be worth trying below steps to reinstall mysql properly and then try to connect.
sudo chown -R $(whoami) /usr/local/var
sudo chown -R $(whoami) /Library/Caches/Homebrew
brew uninstall mysql
brew install mysql
mysql.server start
mysql -uroot
Homebrew will absolutely never repair the permissions using a standard reinstall, and chasing down which folder/file is corrupted will take longer than it is worth.
In this case - blow away the mysql install manually and reinstall via homebrew. Takes about 2 minutes.
cd /usr/local/var
sudo rm -rf mysql
brew install mysql
Homebrew installs mysql insecure by default, so if you want a password:
mysql_secure_installation
Then fire it up.
mysql -uroot
After installing MySQL on my mac m1 macOS Monterey, using brew install mysql I got this info:
[System] [MY-013169] [Server] /opt/homebrew/Cellar/mysql/8.0.27_1/bin/mysqld (mysqld 8.0.27) initializing of server in progress as process 3624
[ERROR] [MY-010457] [Server] --initialize specified but the data directory has files in it. Aborting.
[ERROR] [MY-013236] [Server] The designated data directory /opt/homebrew/var/mysql/ is unusable. You can remove all files that the server added to it.
[ERROR] [MY-010119] [Server] Aborting
[System] [MY-010910] [Server] /opt/homebrew/Cellar/mysql/8.0.27_1/bin/mysqld: Shutdown complete (mysqld 8.0.27) Homebrew.
and this warning:
Warning: The post-install step did not complete successfully You can
try again using: brew postinstall mysql
After that I try to start MySQL using brew services start mysql, and I got this error:
ERROR 2002 (HY000): Can't connect to local MySQL server through socket
'/tmp/mysql.sock' (2)
I managed to fix it using:
> rm -rf ~/opt/homebrew/var/mysql/
> brew postinstall mysql
Now you can connect with mysql -uroot or mysql -uroot -p.
only this has made the trick for me
brew services start --all
(after trying all answers)
I managed to solve this issue by changing localhost to 127.0.0.1
For my case, just specify host as 127.0.0.1, instead of localhost:
$ bin/mysql -uroot -p -h127.0.0.1
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.26
Copyright (c) 2000, 2021, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
if you are using Kali linux you might want to specify the host explicitly
do that with
mysql -h 127.0.0.1 -u root
where 127.0.0.1 is your localhost