Сan't automatically connect to RDS via the script on EC2? - mysql

Im use terraform to create infrastr. in AWS. I'm using script on ec2 userdata, that connects to rds.But this script doesn`t work.
#! /bin/bash
yum update -y
yum install -y httpd
service httpd start
usermod -a -G apache centos
chown -R centos:apache /var/www
yum install -y mysql php php-mysql
systemctl enable httpd.service
cd /var/www/html/
echo "[mysql]" > ~/.my.cnf
echo "user = myuser" >> ~/.my.cnf
echo "password = passworddata" >> ~/.my.cnf
chmod 600 ~/.my.cnf
cd database/
mysql -h db_server_address < script.sql
systemctl restart httpd.service
/var/log/cloud-init-output.log
ERROR 1045 (28000): Access denied for user 'root'#'10.0.1.91' (using password: NO)
Application cannot connect to the database because there is no schema created.
But, when I do it manually into the instance everything works fine.I understand that script is not perfect, but what's the problem,why the script don`t take credentials from ~/.my.cnf?

Probably env variables are not loaded to load data from /root, use:
mysql --defaults-file=~/.my.cnf -h db_server_address < script.sql

Related

Amazon EC2 user-data, how install and configure MySQL? (third-party database)

I try to provision an ec2 instance and I using an userdata:
#!/bin/bash
yum update -y
yum install httpd php php-mysql -y
amazon-linux-extras install php7.4 -y
yum install php-mbstring php-xml php-intl -y
service httpd start
chkconfig httpd on
yum install mysql -y
Everything is working properly but I don't know how to set DBRootPassword, DBUser and DBName. And run SQL command.
Indeed, this commands don't works:
mysqladmin -u root password 'DBRootPassword'
mysql -u root --password='DBRootPassword'
and sql command like that:
CREATE DATABASE DBName;
CREATE USER 'DBUser'#'localhost' IDENTIFIED BY 'DBPassword';
GRANT ALL ON DBName.* TO 'DBUser'#'localhost';
FLUSH PRIVILEGES;
Can you help me? (In this project I don't want use RDS, but a third-party database)
Best Regards

How to set up root password while installing mysql in docker?

I am trying to install mysql in docker and setting default password of root as root using following Dockerfile.
Dockerfile:
FROM ubuntu:20.04
COPY mysql_setup.sh /software/mysql_setup.sh
RUN sh /software/mysql_setup.sh
CMD ["/bin/sh"]
Where /software/mysql_setup.sh
# Download and Install the Latest Updates for the OS
apt-get update && apt-get upgrade -y
# Set the Server Timezone to CST
echo "America/Chicago" > /etc/timezone
dpkg-reconfigure -f noninteractive tzdata
# Enable Ubuntu Firewall and allow SSH & MySQL Ports
ufw enable
ufw allow 22
ufw allow 3306
# Install essential packages
apt-get -y install zsh htop
# Install MySQL Server in a Non-Interactive mode. Default root password will be "root"
echo "mysql-server mysql-server-8.0.26/root_password password root" | sudo debconf-set-selections
echo "mysql-server mysql-server-8.0.26/root_password_again password root" | sudo debconf-set-selections
apt-get -y install mysql-server
# Run the MySQL Secure Installation wizard
mysql_secure_installation
sed -i 's/127\.0\.0\.1/0\.0\.0\.0/g' /etc/mysql/my.cnf
mysql -uroot -p -e 'USE mysql; UPDATE `user` SET `Host`="%" WHERE `User`="root" AND `Host`="localhost"; DELETE FROM `user` WHERE `Host` != "%" AND `User`="root"; FLUSH PRIVILEGES;'
service mysql restart
But above code does not seem to be working as when i enter into docker and use and use root as password it gives an error.
root#839f2946bfdf:/# mysql -u root -p
Enter password:
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (111)
How can i fix this error?
You cannot run a mysql install inside a container in a normal way. Thing like ufw cannot be done. The entire RUN step is at build time only, so service mysql restart won't be running when the container is started, only CMD is.
Recommend just using the Docker Library mysql container. There's a standard MYSQL_ROOT_PASSWORD variable for setting the root password.

Why bash script, that must creat and run zabbix server, execute without errors, but result is wrong?

everyone. Ive started to learn bash and DevOps, lost 2 days, but I cant understand the following:
requirments
Centos 7
DB mysql
zabbix server version 4
mariadb
I need to write bash script (script at the end of the question), that creates and runs zabbix server (exactly 4 version). When I run the script manually step by step (inserting each command in pwsh by hand) in two ways for db creating (example1 or example2) - it is executed properly: zabbix db is created, all services started, zabbix frontend is working well.
But when I run the script as root user or as local user by command:
>sh -c /home/zabbix_server
script is finished by:
>Created symlink from /etc/systemd/system/multi-user.target.wants/zabbix-server.service to /usr/lib/systemd/system/zabbix-server.service.
>Created symlink from /etc/systemd/system/multi-user.target.wants/zabbix-agent.service to /usr/lib/systemd/system/zabbix-agent.service.
>Created symlink from /etc/systemd/system/multi-user.target.wants/httpd.service to /usr/lib/systemd/system/httpd.service.
there are no error messages during or at the end of the script, but when I try manually execute the command create db (example1 or example2), it's doesn't matter:
#Create DB (example1)
mysql -uroot <<EOF
create database zabbix character set utf8 collate utf8_bin;
create user 'zabbix'#'localhost' identified by 'zabbix';
grant all privileges on zabbix.* to 'zabbix'#'localhost';
EOF
#Create DB (example2)
mysql -u root -e "CREATE DATABASE zabbix; CREATE USER zabbix#localhost identified by 'zabbix'; GRANT ALL ON zabbix.* to zabbix#localhost WITH GRANT OPTION;"
I get the error message: ... db can't be created, zabbix base exist
But the result of the command:
>show databases;
doesn't show db zabbix in db list.
script
#!/usr/bin/env bash
setenforce 0
#Install the repository configuration package
rpm -Uvh https://repo.zabbix.com/zabbix/4.4/rhel/7/x86_64/zabbix-release-4.4-1.el7.noarch.rpm
yum clean all
#Install Zabbix server, frontend, agent, database
yum install zabbix-server-mysql -y
yum install zabbix-web-mysql -y
yum install zabbix-agent -y
yum install mariadb-server -y
#Start DB
systemctl start mariadb
#Create DB (example1)
mysql -uroot <<EOF
create database zabbix character set utf8 collate utf8_bin;
create user 'zabbix'#'localhost' identified by 'zabbix';
grant all privileges on zabbix.* to 'zabbix'#'localhost';
EOF
#Create DB (example2)
mysql -u root -e "CREATE DATABASE zabbix; CREATE USER zabbix#localhost identified by 'zabbix'; GRANT ALL ON zabbix.* to zabbix#localhost WITH GRANT OPTION;"
#Import initial schema and data
zcat /usr/share/doc/zabbix-server-mysql*/create.sql.gz | mysql zabbix
#Configure the database for Zabbix server
echo DBPassword=zabbix >> /etc/zabbix/zabbix_server.conf
#Start zabbix server
systemctl start zabbix-server
#Configure frontend
sed -i 's:# php_value date.timezone.*:php_value date.timezone Europe\/Minsk:g' /etc/httpd/conf.d/zabbix.conf;
#Start httpd
systemctl restart zabbix-server zabbix-agent httpd
#Make Zabbix server and agent processes start at system boot
systemctl enable zabbix-server zabbix-agent httpd
Depending on what you want to achieve, this may be a good suggestion:
CREATE DATABASE IF NOT EXISTS zabbix;
This makes the call idempotent!
The problem was, that the script contains a \r (CR) at the end (0d). Remove it with:
tr -d '\r' < old_name_script > new_name_script
And the script works fine.
If it will be useful, there is fully working example of the script, that creates zabbix server:
#!/usr/bin/env bash
#Disable SELinux
enforceStatus=getenforse
if [ "$enforceStatus" != "Permissive" ]; then
setenforce 0
fi
#Install the repository configuration package
rpm -Uvh https://repo.zabbix.com/zabbix/4.0/rhel/7/x86_64/zabbix-release-4.0-2.el7.noarch.rpm
yum clean all
#Install Zabbix server, frontend, agent, database, httpd
yum install zabbix-server-mysql -y
yum install zabbix-web-mysql -y
yum install zabbix-agent -y
yum install mariadb mariadb-server -y
yum install httpd -y
#Start and add to autostart DB mariadb
systemctl start mariadb
systemctl enable mariadb.service
#Create DB (example1)
mysql -uroot <<EOF
create database zabbix character set utf8 collate utf8_bin;
create user 'zabbix'#'localhost' identified by 'zabbix';
grant all privileges on zabbix.* to 'zabbix'#'localhost';
EOF
#Import initial schema and data
zcat /usr/share/doc/zabbix-server-mysql*/create.sql.gz | mysql zabbix
#Configure the database for Zabbix server
echo DBPassword=zabbix >> /etc/zabbix/zabbix_server.conf
#Configure frontend
sed -i 's:# php_value date.timezone.*:php_value date.timezone Europe\/Minsk:g' /etc/httpd/conf.d/zabbix.conf;
#Start zabbix server processes start at system boot
systemctl restart zabbix-server
systemctl enable zabbix-server
#Start httpd processes start at system boot
systemctl restart httpd
systemctl enable httpd
#Start zabbix-agent processes start at system boot
systemctl restart zabbix-agent
systemctl enable zabbix-agent
#Add permissions to irewall
firewall-cmd --permanent --add-port=10050/tcp
firewall-cmd --permanent --add-port=10051/tcp
firewall-cmd --permanent --add-port=80/tcp
firewall-cmd --reload

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

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