Bash script on Linux 2 AMI (AWS EC2) to install MySQL - mysql

I am trying to install WordPress on my EC2 Linux2.
I use a bash script with even the following rows that I think does not work:
# MySQL
sudo yum install -y mysql
sudo export MYSQL_HOST=${db_address}
sudo mysql --user=${db_username} --password=${db_password} ${db_name}
sudo mysql -e "CREATE USER 'wordpress' IDENTIFIED BY '${db_password}';"
sudo mysql -e "GRANT ALL PRIVILEGES ON ${db_name}.* TO wordpress;"
sudo mysql -e "FLUSH PRIVILEGES;"
sudo mysql -e "Exit"
What is wrong?

Related

Installing MySQL on CentOS 7 using Dockerfile failing

I am trying to setup MySQL using a Dockerfile which uses CentOS 7 .. I know I should be using a seperate container for MySQL, but due to limitations I can't, so now I have to install it using the Dockerfile and i am running into some issues.
I have looked a few different places and got some ideas, and put the following together;
FROM centos:7
RUN yum -y update
RUN yum -y install \
httpd \
mariadb-server \
mariadb \
&& rm -rf /var/cache/yum/* \
&& yum clean all
ENV DB_USER testuser
ENV DB_PASSWORD testpass
ENV DB_NAME testdb
RUN /usr/bin/mysqld && sleep 5 && \
mysql -uroot -e "CREATE USER '${DB_USER}'#'%' IDENTIFIED BY '${DB_PASSWORD}'" && \
mysql -uroot -e "GRANT ALL PRIVILEGES ON *.* TO '${DB_USER}'#'%' WITH GRANT OPTION" &&\
mysql -uroot -e "CREATE DATABASE ${DB_NAME}"
EXPOSE 80
EXPOSE 3306
CMD ["/usr/sbin/httpd","-D","FOREGROUND"]
This seems to be for abother distro as /usr/bin/mysqld doesnt actually exist on Centos7 and I can find no other way to set things up manually ... I usually use the mysql_secure_installation command when setting up a VM, but as im doing this through a Dockerfile i can't as it requires input from the user.
I was wondering if anybody has manually setup MySQL using a Dockerfile on Centos and has some tips to get this going.
The error i keep coming up against when trying to use Mysql in the container is;
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock'

Docker MySQL automatical Installation and configuration

On old MySQL 5.6 I was automatically installing it and creating all needed users and passwords, using the code below.
Is there a way to do the same on a new MySQL 8.0 so that it automatically installs into docker containers allows root without a password and creates all the above?
RUN yum -y install https://dev.mysql.com/get/mysql57-community-release-el7-11.noarch.rpm
RUN yum-config-manager --disable mysql57-community; yum-config-manager --enable mysql56-community
RUN yum -y install mysql-community-server mysql-community-client
#Configure MySQL
# Needed to create system tables etc.
chown -R mysql:mysql /var/lib/mysql
su -l mysql -c "/usr/bin/mysql-systemd-start pre"
# Start main service
(su -l mysql -c "/usr/bin/mysqld_safe --basedir=/usr") &
# Don't signal startup success before a ping works
su -l mysql -c "/usr/bin/mysql-systemd-start post"
mysql -e "CREATE DATABASE IF NOT EXISTS \`${USERNAME}\`;"
mysql -e "GRANT ALL PRIVILEGES ON \`${USERNAME}\`.* TO \`${USERNAME}\`#'%' IDENTIFIED BY \"${MYSQL_PASS}\";"
mysql -e "GRANT ALL PRIVILEGES ON \`${USERNAME}\`.* TO \`${USERNAME}\`#'localhost' IDENTIFIED BY \"${MYSQL_PASS}\";"
mysqladmin shutdown

How to Install mysql-server via dockerfile

Does anybody knows how to install mysql-server via dockerfile? I have written a Dockerfile, but the build ends with an error: /bin/sh: 1: /usr/bin/mysqld: not found
USER root
RUN apt-get update
RUN DEBIAN_FRONTEND=noninteractive apt-get install -y mysql-server-5.7
# Remove pre-installed database
RUN rm -rf /var/lib/mysql/*
RUN sed -i -e"s/^bind-address\s*=\s*127.0.0.1/bind-address = 0.0.0.0/"/etc/mysql/my.cnf
ENV DB_USER example
ENV DB_PASSWORD example
ENV DB_NAME example
ENV VOLUME_HOME "/var/lib/mysql"
EXPOSE 3306
RUN cp /etc/mysql/my.cnf /usr/share/mysql/my-default.cnf
RUN /usr/bin/mysqld && sleep 5 && \
mysql -uroot -e "CREATE USER '${DB_USER}'#'%' IDENTIFIED BY '${DB_PASSWORD}'" && \
mysql -uroot -e "GRANT ALL PRIVILEGES ON *.* TO '${DB_USER}'#'%' WITH GRANT OPTION" &&\
mysql -uroot -e "CREATE DATABASE ${DB_NAME}" && \
mysqladmin -uroot shutdown
For an ubuntu:16.04 base image, mysqld is found in /usr/sbin, not /usr/bin
If you can add a step RUN which mysqld before your final RUN command that will show you where the mysqld executable is found. It may vary depending on which base image/distro you're using.
You can also use RUN mysqld ... without a full path, if the file is in your $PATH
You may also need to update your RUN sed line as below, adding spaces around the quoted string:
RUN sed -i -e "s/^bind-address\s*=\s*127.0.0.1/bind-address = 0.0.0.0/" /etc/mysql/my.cnf
Otherwise, you may see the following error:
The command '/bin/sh -c sed -i -e"s/^bind-address\s*=\s*127.0.0.1/bind-address = 0.0.0.0/"/etc/mysql/my.cnf' returned a non-zero code: 1

Change mysql bind-address from command line in Vagrant

I am setting up a new vagrant box for a NodeJS project. For the database I will be using MySQL. I have setup a provisioning script to install everything I need and it all works great apart from accessing MySQL from the host machine (Using Sequel Pro). I have it working but I have to edit the /etc/mysql/my.cnf file in order to comment out the bind-address line.
Is there anyway to do this on the command line so I can add it to the provisioning script? If this is possible then any other dev that I share the project with would only need to do vagrant up and have everything ready to go.
Below are the parts of my script relevant to the MySQL setup:
#!/bin/bash
# Variables
DBHOST=localhost
DBNAME=vagrant
DBUSER=vagrant
DBPASSWD=test123
# Update packages
apt-get update
# Install MySQL
debconf-set-selections <<< "mysql-server mysql-server/root_password password $DBPASSWD"
debconf-set-selections <<< "mysql-server mysql-server/root_password_again password $DBPASSWD"
apt-get install -y mysql-server
mysql -uroot -p$DBPASSWD -e "CREATE DATABASE $DBNAME"
mysql -uroot -p$DBPASSWD -e "CREATE USER '$DBUSER'#'localhost' IDENTIFIED BY '$DBPASSWD'"
mysql -uroot -p$DBPASSWD -e "GRANT ALL PRIVILEGES ON $DBNAME.* TO '$DBUSER'#'localhost'"
mysql -uroot -p$DBPASSWD -e "CREATE USER '$DBUSER'#'10.0.2.2' IDENTIFIED BY '$DBPASSWD'"
mysql -uroot -p$DBPASSWD -e "GRANT ALL PRIVILEGES ON $DBNAME.* TO '$DBUSER'#'10.0.2.2'"
echo "Installed MySQL"
Thanks for any help you can provide.
You should be able to achieve this using sed command from your script
apt-get install -y mysql-server
sed -i "s/.*bind-address.*/bind-address = 0.0.0.0/" /etc/mysql/my.cnf
...
Note you might need to restart the service to take effect if you provision and do not restart the instance for the 1st use, add this then to your script
service mysql stop
service mysql start

I can't use MySQL in Windows Linux Subsystem

I entered Windows Linux Subsystem with:
C:\> bash --login
Installed MySQL as follows:
$ sudo apt-get install libmysqlclient-dev
$ sudo apt-get install mysql-server
$ sudo apt-get install mysql-client
When I try the following:
$ sudo mysql -u root
I get:
Error 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)
use like:
mysql --host=127.0.0.1 --user=root --password=root
I believe the mysql service isn't running. You can start it with:
sudo /etc/init.d/mysql start
Use TCP/IP connection instead as shown below:
mysql --protocol=tcp -uroot -ppassword
I had the same issue and found that this works for me:
mysqladmin -u root password "<enter_new_password>";
mysql -u root -p
This will prompt you with the password you created