Can we run multiple MySQL servers on a single machine?
Thanks.
Yes, you just need to run them on separate ports and point them at different lib directories for their data.
Here's a good reference: http://dev.mysql.com/doc/refman/5.1/en/mutiple-servers.html
(If you want to use this for testing, I suggest checking out MySQL Sandbox which is now replaced by dbdeployer)
There are various methods to run multiple instances of mysql (on different ports) on the same machine. Here I have used the same binary and used a separate configuration file (with separate port, pid, socket and data directory).
We need to create new directories for our datadir and log folder (if used). Also we need to assign proper permissions on those folders:
# mkdir /var/lib/mysql2
# chown -R mysql.mysql /var/lib/mysql2/
# mkdir /var/log/mysql2
# chown -R mysql.mysql /var/log/mysql2
Next we need a separate configuration file same as a default mysql configuration file. So start by copying the existing one and changing the needed values.
# cp /etc/my.cnf /etc/my2.cnf
(or change the path appropriately for your configuration file is in a different place).
Next, we need to edit our new configuration file with different mysql port (default to 3306), the pid and socket than the default ones, and also point the data and log folders to the ones created before.
# cd /etc
# sed -i ‘s/3306/3307/g’ my2.cnf
# sed -i ‘s/mysqld.sock/mysqld2.sock/g’ my2.cnf
# sed -i ‘s/mysqld.pid/mysqld2.pid/g’ my2.cnf
# sed -i ‘s/var\/lib\/mysql/var\/lib\/mysql2/g’ my2.cnf
# sed -i ‘s/var\/log\/mysql/var\/log\/mysql2/g’ my2.cnf
Finally we need to initialize the default dbs:
# mysql_install_db –user=mysql –datadir=/var/lib/mysql2/
Finally we can start our new mysql instance with:
# mysqld_safe – -defaults-file=/etc/my2.cnf &
We can connect to our new instance using:
# mysql -S /var/run/mysqld/mysqld2.sock
or
# mysql -h 127.0.0.1 -P 3307
and if we no longer need it, stop it with:
# mysqladmin -S /var/run/mysqld/mysqld2.sock shutdown
Ref Site : https://linuxinpakistan.com/start-multiple-instances-mysql-machine
My steps on Windows 10:
Copy C:\ProgramData\MySQL\MySQL Server 8.0\my.ini to C:\ProgramData\MySQL\MySQL Server 8.0\my1.ini
Open my1.ini and modify:
port=3307(under Client and Server Section)
datadir=C:/ProgramData/MySQL/MySQL Server 8.0/Data1
report_port=3307
Copy C:\ProgramData\MySQL\MySQL Server 8.0\Data to C:\ProgramData\MySQL\MySQL Server 8.0\Data1
Run on cmd prompt: (With Administrator privileges if necessary)
C:\Program Files\MySQL\MySQL Server 8.0\bin>mysqld --install MySQL80-1 --defaults-file="C:\ProgramData\MySQL\MySQL Server 8.0\my1.ini"
If all went well, you will see:
Service successfully installed.
Win+R
Type services.msc, find the service name MySQL80-1, right-click on it and click Start.
If all went well, you will see the Status change to Running.
If it did not go well, open xxx.err file found in C:\ProgramData\MySQL\MySQL Server 8.0\Data1 to check why.
If you do not want the service anymore:
Stop it
Delete it on the cmd prompt using sc delete MySQL80-1 where MySQL80-1 is your service name.
For Windows, if the version of mysql server is different then using MYSQL Installer download and install the different versions of the MYSQL server.
Select Reconfigure for each MYSQL server and configure the PORT differently. Complete the configuration steps by clicking next until it is finished
Yes definitely,
Create multiple configuration files with different ports.
This is the best resource to understand:
Video Tutorial: MySQL Multiple Instances
Reference article: Click here
Related
I've been trying to install mysql using WSL and I've followed the steps indicated in this guide https://learn.microsoft.com/en-us/windows/wsl/tutorials/wsl-database. I tried running mysql --version and it worked (prompted mysql Ver 8.0.23 for Linux on x86_64 (MySQL Community Server - GPL)). But as I try the next command sudo /etc/init.d/mysql start, it says sudo: /etc/init.d/mysql: command not found. I've also tried checking the contents of /etc/init.d/ directory and there is no existing mysql file/folder there. What should I do next to proceed with my mysql installation?
Thanks!
There is a great blog post on this problem, with explanations of the issue and detailed solutions.
https://www.58bits.com/blog/2020/05/03/installing-mysql-80-under-wsl-2-and-ubuntu
From the post:
One solution is to download the mysql.server.sh script from here -
https://github.com/mysql/mysql-server/tree/8.0/support-files - and
then copy and rename the script to /etc/init.d/mysql (make sure that
it's also executable - chmod +x mysql)
You'll then need to set the default values for basdir, datadir and pid
file locations.
Here's an excerpt with the top portion of the file and the settings
that worked for me...
# If you change base dir, you must also change datadir. These may get
# overwritten by settings in the MySQL configuration files.
basedir=/usr
datadir=/var/lib/mysql
# Default value, in seconds, afterwhich the script should timeout waiting
# for server start.
# Value here is overriden by value in my.cnf.
# 0 means don't wait at all
# Negative numbers mean to wait indefinitely
service_startup_timeout=900
# Lock directory for RedHat / SuSE.
lockdir='/var/lock/subsys'
lock_file_path="$lockdir/mysql"
# The following variables are only set for letting mysql.server find things.
# Set some defaults
mysqld_pid_file_path=/var/run/mysqld/mysqld.pid
if test -z "$basedir"
After this you should be able to start and stop MySQL as follows:
sudo service mysql start
sudo service mysql stop
As he mentions later, you also need to create /var/run/mysqld/ and set permissions:
sudo mkdir /var/run/mysqld
sudo chown mysql:mysql /var/run/mysqld
After installing MariaDB repository configuration tool for the first time in my Linux WSL for Windows (as described in MariaDB Download Page), I executed mysql but there was a socket error. netstat -apn | grep mysql shows nothing, indicating the mysql service is stopped; sudo apt list | grep *mysql-server* shows I had successfully installed mysql-server.
However, as I tried sudo service mysql start, the command line gives:
* Starting MariaDB database server mysqld [fail]
I tried the following methods, but all failed and yielded the same answer:
Using /etc/init.d/mysql start
Removing /var/lib/mysql/ib_logfile0 and /var/lib/mysql/ib_logfile1
Upgrading access of /var/lib/mysql using chmod -R 777 /var/lib/mysql
Removing everything from /var/lib/mysql/
Changing port setting using port=1112 in /etc/my.cnf (since I have another mysql on the Windows side)
Filling in additional information in /etc/my.cnf (my configuration file was initially empty after installation, and I filled in the basedir, datadir, socket, log_error, and pid-file properties)
Trying systemctl instead of service (this failed because Linux WSL uses sysvinit instead of systemd)
How could I start my MariaDB service? Thanks
I'm able to reproduce your problem (or one that looks an awfully lot like it) on WSL1. Can you confirm that you are using WSL1?
I spun up two cloned instances (wsl --import of a clean backup) of Ubuntu 20.04 -- One on WSL1 and the other on WSL2. Unfortunately, I don't have a handy 18.04 to work with, but I'm hoping the problem is the same.
On WSL2, everything worked properly. After the installation steps (pretty much the ones you put in your comment, but for 20.04), I was able to:
sudo service mariadb start
and then sudo mysql -u root successfully.
On WSL1, however, the MariaDB installation seems to fail in a strange way. It does not create /etc/mysql/mariadb.cnf, which leads to what you saw with an empty /etc/mysql/my.cnf, since it's a symlink to mariadb.cnf.
So I created mariadb.cnf manually:
sudo vi /etc/mysql/mariadb.cnf
with the contents:
# The MariaDB configuration file
#
# The MariaDB/MySQL tools read configuration files in the following order:
# 0. "/etc/mysql/my.cnf" symlinks to this file, reason why all the rest is read.
# 1. "/etc/mysql/mariadb.cnf" (this file) to set global defaults,
# 2. "/etc/mysql/conf.d/*.cnf" to set global options.
# 3. "/etc/mysql/mariadb.conf.d/*.cnf" to set MariaDB-only options.
# 4. "~/.my.cnf" to set user-specific options.
#
# If the same option is defined multiple times, the last one will apply.
#
# One can use all long options that the program supports.
# Run program with --help to get a list of available options and with
# --print-defaults to see which it would actually understand and use.
#
# If you are new to MariaDB, check out https://mariadb.com/kb/en/basic-mariadb-articles/
#
# This group is read both by the client and the server
# use it for options that affect everything
#
[client-server]
# Port or socket location where to connect
# port = 3306
socket = /run/mysqld/mysqld.sock
# Import all .cnf files from configuration directory
!includedir /etc/mysql/conf.d/
!includedir /etc/mysql/mariadb.conf.d/
This is simply the default mariadb.cnf that was created correctly by the installation on WSL2.
Attempting to start the service then gave an error about a missing /etc/mysql/debian-start, so I repeated the same steps of copying it over:
sudo vi /etc/mysql/debian-start
With the contents:
#!/bin/bash
#
# This script is executed by "/etc/init.d/mariadb" on every (re)start.
#
# Changes to this file will be preserved when updating the Debian package.
#
# NOTE: This file is read only by the traditional SysV init script, not systemd.
#
source /usr/share/mysql/debian-start.inc.sh
# Read default/mysql first and then default/mariadb just like the init.d file does
if [ -f /etc/default/mysql ]; then
. /etc/default/mysql
fi
if [ -f /etc/default/mariadb ]; then
. /etc/default/mariadb
fi
MYSQL="/usr/bin/mysql --defaults-file=/etc/mysql/debian.cnf"
MYADMIN="/usr/bin/mysqladmin --defaults-file=/etc/mysql/debian.cnf"
# Don't run full mysql_upgrade on every server restart, use --version-check to do it only once
MYUPGRADE="/usr/bin/mysql_upgrade --defaults-extra-file=/etc/mysql/debian.cnf --version-check"
MYCHECK="/usr/bin/mysqlcheck --defaults-file=/etc/mysql/debian.cnf"
MYCHECK_SUBJECT="WARNING: mysqlcheck has found corrupt tables"
MYCHECK_PARAMS="--all-databases --fast --silent"
MYCHECK_RCPT="${MYCHECK_RCPT:-root}"
## Checking for corrupt, not cleanly closed (only for MyISAM and Aria engines) and upgrade needing tables.
# The following commands should be run when the server is up but in background
# where they do not block the server start and in one shell instance so that
# they run sequentially. They are supposed not to echo anything to stdout.
# If you want to disable the check for crashed tables comment
# "check_for_crashed_tables" out.
# (There may be no output to stdout inside the background process!)
# Need to ignore SIGHUP, as otherwise a SIGHUP can sometimes abort the upgrade
# process in the middle.
trap "" SIGHUP
(
upgrade_system_tables_if_necessary;
check_root_accounts;
check_for_crashed_tables;
) >&2 &
exit 0
And then chmod 755 /etc/mysql/debian-start
After that, voila:
sudo service mariadb restart
sudo mysql -u root
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 32
Server version: 10.5.8-MariaDB-1:10.5.8+maria~focal mariadb.org binary distribution
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]>
Given the steps you've tried so far, I'd recommend blowing away pretty much all of it to try to start over "clean":
sudo apt remove mariadb-server
sudo apt autoremove
sudo rm -rf /etc/mysql
sudo rm -rf /var/lib/mysql
sudo rm -rf /usr/lib/mysql
Then reinstall mariadb-server and follow the steps above to create the correct files.
While creating multiple mysql servers on same ubuntu machine under EC2 instance on AWS.I successfully created /var/lib/mysql2 , /var/log/mysql2 ,/etc/mysql2/
done desired changes in my.cnf file(under /etc/mysql2/ which i created by just copying /etc/mysql/ content) along with desired rights to mysql user
(Reference used to do the needful :--
- http://www.ducea.com/2009/01/19/running-multiple-instances-of-mysql-on-the-same-machine/
)
After running the following
sed -i 's/3306/3307/g' my.cnf
sed -i 's/mysqld.sock/mysqld2.sock/g' my.cnf
sed -i 's/mysqld.pid/mysqld2.pid/g' my.cnf
sed -i 's/var\/lib\/mysql/var\/lib\/mysql2/g' my.cnf
sed -i 's/var\/log\/mysql/var\/log\/mysql2/g' my.cnf
Proceeded by running the following command where i am getting error
mysql_install_db --user=mysql --datadir=/var/lib/mysql2/
Error:--->
FATAL ERROR: Could not find my-default.cnf file in my system and found the path
If you compiled from source, you need to run 'make install' to
copy the software into the correct location ready for operation.
If you are using a binary release, you must either be at the top
level of the extracted archive, or pass the --basedir option
pointing to that location.
I tried to do the following but in vain
sudo mysql_install_db --user=mysql --datadir=/var/lib/mysql2/ --basedir=/usr/share/doc/mysql-server-5.6/examples/
FATAL ERROR: Could not find /fill_help_tables.sql
If you compiled from source, you need to run 'make install' to
copy the software into the correct location ready for operation.
If you are using a binary release, you must either be at the top
level of the extracted archive, or pass the --basedir option
pointing to that location.
Please help in resolving the issue.I have installed MySQL server 5.6 and want to use the same version for my another instance on 3307 port
I have an AWS instance running CentOS 6.5. It has been updated, secured, and setup for web hosting (LAMP). I attached an EBS volume to the instance and mounted it under /data.
Two questions:
How can I get MySQL to use the /data directory as its database storage location? (I don't want to run the program from the /data directory, just put the .sql file there.
How can I do the same for my web site? I plan on running a wordpress site and its current location is in the /var/www/html directory. I want to change this to /data/site.
I want to keep the web site files and database on a separate volume: /data. If my instance was to get corrupt or inaccessible, I can attach the EBS volume to a new instance.
I have read dozens of tutorials and articles on how to get MySQL moved to a different directory, but nothing is working. MySQL refuses to start up after. Can I keep MySQL installed as is, but have it read/write the database on a different directory like /data which is a mounted EBS volume or is this not possible at all with linux?
Here are some of the tutorials and articles I been following/testing with:
aws.amazon.com/articles/1663?_encoding=UTF8&jiveRedirect=1
spruce.it/noise/setting-up-a-proper-lamp-stack-on-aws-ec2-ebs/
EDIT:
This is what I am doing.
Create a new instance using this ami: https://aws.amazon.com/marketplace/pp/B00IOYDTV6?ref=cns_srchrow
Once the instance is up, I run updates using: sudo yum update -y
One updated, I set it up as a LAMP web server using these instructions: http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/install-LAMP.html
In addition to the above steps, I allow port 80 tcp connections on the built-in firewall. I run these commands: sudo iptables -I INPUT -p tcp --dport 80 -j ACCEPT and sudo service iptables save
Once this is done, I test my site at http://IP-ADDRESS (this shows me the Apache Test Page)
Once LAMP is installed, I install the MySQL Server by running this: yum install mysql-server
After that is installed, I proceed to the "To secure the MySQL server" instructions on the previous Amazon document.
Next, I install PHPMyAdmin using these two tutorials: http://tecadmin.net/installing-apache-mysql-php-on-centos-redhat/# and http://tecadmin.net/how-to-install-phpmyadmin-on-centos-using-yum/
At this point, I have a fully functioning web server. Now, I want to use the AWS EBS volume to store all the databases and website files. First, I attach the newly create AWS EBS volume. I use this tutorial to do this: http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ebs-using-volumes.html
THIS IS WHERE THE PROBLEMS START.
Using the information in this tutorial: aws.amazon.com/articles/1663?_encoding=UTF8&jiveRedirect=1. It says FAILED.
So one thing you can do is the following that avoids copying all directories. You need to make sure that all permissions are setup correctly for it to work:
mysql dat dir:
mv /var/lib/mysql /var/lib/mysql.orig
mkdir -p /<your-new-ebs-mountpoint>/var/lib/mysql
chown mysql.mysql /<your-new-ebs-mountpoint>/var/lib/mysql
chmod 700 /<your-new-ebs-mountpoint>/var/lib/mysql
etc configs:
mkdir -p /<your-new-ebs-mountpoint>/etc
cp /etc/my.cnf /<your-new-ebs-mountpoint>/etc/my.cnf
mv /etc/my.cnf /etc/my.cnf.orig
ln -s /<your-new-ebs-mountpoint>/etc/my.cnf /etc/my.cnf
logs:
mkdir -p /<your-new-ebs-mountpoint>/var/log
mv /var/log/mysqld.log /var/log/mysqld.log.orig
touch /<your-new-ebs-mountpoint>/var/log/mysqld.log
chown mysql.mysql /<your-new-ebs-mountpoint>/var/log/mysqld.log
chmod 640 /<your-new-ebs-mountpoint>/var/log/mysqld.log
ln -s /<your-new-ebs-mountpoint>/var/log/mysqld.log /var/log/mysqld.log
How do I know which configuration file is used by MySQL currently? Is there any command or something to find it out?
The information you want can be found by running
mysql --help
or
mysqld --help --verbose
I tried this command on my machine:
mysql --help | grep "Default options" -A 1
And it printed out:
Default options are read from the following files in the given order:
/etc/my.cnf /usr/local/etc/my.cnf ~/.my.cnf
See if that works for you.
mysqld --help --verbose will find only location of default configuration file. What if you use 2 MySQL instances on the same server? It's not going to help.
Good article about figuring it out:
"How to find MySQL configuration file?"
If you are using terminal just type the following:
locate my.cnf
You can use the report process status ps command:
ps ax | grep '[m]ysqld'
You should find them by default in a folder like /etc/my.cnf, maybe also depends on versions. From MySQL Configuration File:
Interestingly, the scope of this file
can be set according to its location.
The settings will be considered global
to all MySQL servers if stored in
/etc/my.cnf. It will be global to a
specific server if located in the
directory where the MySQL databases
are stored (/usr/local/mysql/data for
a binary installation, or
/usr/local/var for a source
installation). Finally, its scope
could be limited to a specific user if
located in the home directory of the
MySQL user (~/.my.cnf). Keep in mind
that even if MySQL does locate a
my.cnf file in /etc/my.cnf (global to
all MySQL servers on that machine), it
will continue its search for a
server-specific file, and then a
user-specific file. You can think of
the final configuration settings as
being the result of the /etc/my.cnf,
mysql-data-dir/my.cnf, and ~/.my.cnf
files.
There are a few switches to package managers to list specific files.
RPM Sytems:
There are switches to rpm command, -q for query, and -c or --configfiles to list config files. There is also -l or --list
The --configfiles one didn't quiet work for me, but --list did list a few .cnf files held by mysql-server
rpm -q --list mysql-server
DEB Systems:
Also with limited success: dpkg --listfiles mysql-server
you can find it by running the following command
mysql --help
it will give you the mysql installed directory and all commands for mysql.
login to mysql with proper credential and used mysql>SHOW VARIABLES LIKE 'datadir'; that will give you path of where mysql stored