From 4912dc5630258f79d9288bc319005386d4601140 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A9ctor=20Louzao?= Date: Mon, 11 Nov 2019 18:04:01 +0100 Subject: [PATCH] Installing, Configuring, Using and Troubleshotting MYSQL/MARIADB Why this change is needed: Restructuring a bit all this stuff, According to this askfedora Question: https://ask.fedoraproject.org/t/problem-installing-mysql-in-fedora-31/3908 https://ask.fedoraproject.org/t/mysql-wont-start-after-upgrade/291 What this change accomplishes: * Open Database toolbar for Next Step : review FIXME progrestSQL * Installation (include container) * Configuring * Using * Troubleshooting --- modules/ROOT/nav.adoc | 6 +- ...oc_configuring-mysql-mariadb-firewall.adoc | 48 ---------- .../_partials/proc_configuring-mysql.adoc | 23 ----- .../_partials/proc_expose-outside-mysql.adoc | 79 +++++++++++++++ .../proc_installing-mysql-from-container.adoc | 76 +++++++++++++++ ...roc_installing-mysql-from-fedora-repo.adoc | 79 +++++++++++++++ .../proc_installing-mysql-from-oracle.adoc | 51 ++++++++++ .../_partials/proc_installing-mysql.adoc | 22 ----- .../_partials/proc_troubleshooting-mysql.adoc | 96 +++++++++++++++++++ .../_partials/proc_using-mysql-mariadb.adoc | 4 - .../ROOT/pages/installing-mysql-mariadb.adoc | 17 +--- 11 files changed, 391 insertions(+), 110 deletions(-) delete mode 100644 modules/ROOT/pages/_partials/proc_configuring-mysql-mariadb-firewall.adoc delete mode 100644 modules/ROOT/pages/_partials/proc_configuring-mysql.adoc create mode 100644 modules/ROOT/pages/_partials/proc_expose-outside-mysql.adoc create mode 100644 modules/ROOT/pages/_partials/proc_installing-mysql-from-container.adoc create mode 100644 modules/ROOT/pages/_partials/proc_installing-mysql-from-fedora-repo.adoc create mode 100644 modules/ROOT/pages/_partials/proc_installing-mysql-from-oracle.adoc delete mode 100644 modules/ROOT/pages/_partials/proc_installing-mysql.adoc create mode 100644 modules/ROOT/pages/_partials/proc_troubleshooting-mysql.adoc diff --git a/modules/ROOT/nav.adoc b/modules/ROOT/nav.adoc index cb44597..54d3ff8 100644 --- a/modules/ROOT/nav.adoc +++ b/modules/ROOT/nav.adoc @@ -49,7 +49,6 @@ ** xref:using-aide.adoc[Checking file integrity with AIDE] ** xref:getting-started-with-apache-http-server.adoc[Getting started with Apache HTTP Server] ** xref:how-to-edit-iptables-rules.adoc[How to edit iptables rules] -** xref:installing-mysql-mariadb.adoc[Installing MySQL/MariaDB] * xref:getting-started-with-selinux.adoc[SELinux] @@ -90,6 +89,7 @@ //FIXME * xref:uefi-with-qemu.adoc[Using UEFI with QEMU] //FIXME * xref:upgrading-fedora-online.adoc[Upgrading Fedora using package manager] + * xref:anaconda/anaconda.adoc[Anaconda: the Fedora installer] ** xref:anaconda/anaconda_distros.adoc[Anaconda-based Distributions] ** xref:anaconda/anaconda_updates.adoc[Anaconda Updates] @@ -101,5 +101,9 @@ ** xref:create-hello-world-rpm.adoc[Creating a GNU Hello World RPM Package] +* Databases +** xref:installing-mysql-mariadb.adoc[Installing, Configuring and Troubleshooting MySql/MariaDB] + + * FAQ ** xref:fedora-and-red-hat-enterprise-linux.adoc[Difference between Fedora and Red Hat Enterprise Linux] diff --git a/modules/ROOT/pages/_partials/proc_configuring-mysql-mariadb-firewall.adoc b/modules/ROOT/pages/_partials/proc_configuring-mysql-mariadb-firewall.adoc deleted file mode 100644 index d755135..0000000 --- a/modules/ROOT/pages/_partials/proc_configuring-mysql-mariadb-firewall.adoc +++ /dev/null @@ -1,48 +0,0 @@ -= Allow or prevent access from the network to the database server -== Allow access to the database from the network -To allow remote connections, you need to open the port 3306 on the firewall. - ----- -$ sudo firewall-cmd --add-service=mysql --permanent -$ sudo firewall-cmd --reload ----- - -In addition you have to grant rights to the user you want to use to connect to the database. - -From the mysql shell, for example, grant all the privileges on the database _test_ to _my_user_ user connecting from the host _192.168.1.1_ using the password _PaSsWoRd_: - ----- -mysql> GRANT ALL PRIVILEGES ON test.* TO 'my_user'@'192.168.1.1' IDENTIFIED BY 'PaSsWoRd'; ----- - -== Limit the access to the database only from localhost - -On the other hand, if you want to avoid to expose the database service on the network, edit the configuration file - -* `/etc/my.cnf.d/mariadb-server.cnf` for MariaDB -* `/etc/my.cnf.d/community-mysql-server.cnf` for MySQL -* `/etc/my.cnf` for MySQL installed from the third party repository - -and add/uncomment/modify this option in the `[mysqld]` section - ----- -bind-address=127.0.0.1 ----- - -Restart the service (use `mysqld` in place of `mariadb` if it is the case) - ----- -$ sudo systemctl restart mariadb ----- - -Verify that the service is listening only on localhost (127.0.0.1). The output of this command: - ----- -$ ss -ntl |grep 3306 ----- - -should look like: - ----- -LISTEN 0 80 127.0.0.1:3306 0.0.0.0:* ----- diff --git a/modules/ROOT/pages/_partials/proc_configuring-mysql.adoc b/modules/ROOT/pages/_partials/proc_configuring-mysql.adoc deleted file mode 100644 index 5aa2a92..0000000 --- a/modules/ROOT/pages/_partials/proc_configuring-mysql.adoc +++ /dev/null @@ -1,23 +0,0 @@ -= Configuring MySQL - -Enable the service at boot and start it right now - ----- -$ sudo systemctl enable mysqld --now ----- - -If you installed the package from the Fedora repository, the default root password is empty. - -The package installed from the third party MySQL repository, generates instead a temporary root password. So you have to look which is this temporary password generated during the first startup in order to run the `mysql_secure_installation` script and to be able to set a custom root password. - ----- -$ sudo grep 'A temporary password is generated' /var/log/mysql/mysqld.log ----- - -Perform the initial setup: - ----- -$ sudo mysql_secure_installation ----- - -Some questions will be asked: answer to them as you prefer; answering _yes_ to all of them is perfectly fine. diff --git a/modules/ROOT/pages/_partials/proc_expose-outside-mysql.adoc b/modules/ROOT/pages/_partials/proc_expose-outside-mysql.adoc new file mode 100644 index 0000000..196b8f6 --- /dev/null +++ b/modules/ROOT/pages/_partials/proc_expose-outside-mysql.adoc @@ -0,0 +1,79 @@ += How To Allow Remote Access MYSQL/MariaDB/MYSQL Community + +== Add New Rule to Firewalld + +Open SQL port (3306) on FireWalld: + +---- +sudo firewall-cmd --permanent --zone=public --add-service=mysql +---- + +## OR ## + +---- +sudo firewall-cmd --permanent --zone=public --add-port=3306/tcp +---- + +== Restart firewalld.service + +---- +systemctl restart firewalld.service +---- + +== Editing Conf. Files: + +Configuration files: + +* MySql -> `/etc/my.cnf/` +* MySql Community -> `/etc/my.cnf.d/community-mysql-server.cnf` +* MariaDB -> `/etc/my.conf` + +NOTE: you can ensure that with the following command `rpm -qc [package]`. + +Navigate to the line that begins with the bind-address directive. It will look like this: +you could set this directive to a wildcard IP address, either *, ::, or 0.0.0.0: + +---- +bind-address = 0.0.0.0 +---- + +After changing this line, save and close the file and then restart the MySQL service: + +---- +sudo systemctl restart {mysqld|mariadb} +---- + +== Creating a USER + +---- +CREATE USER 'your_username'@'host_ip_addr' IDENTIFIED BY 'your_password'; +---- + +NOTE: Replace your_username and your_password depending on what you want the username and password to be. Here, host_ip_addr is the hostname or IP address of the computer from where you want to connect to the MySQL/MariaDB server. You can also use % as host_ip_addr if you want to connect from any computer. It can also be something like 192.168.2.% if you want to connect from computers from the IP range 192.168.2.1 – 192.168.2.254. + +== Allow Access + +---- +GRANT ALL PRIVILEGES ON *.* TO 'your_username'@'%'; + IDENTIFIED BY 'my-new-password' WITH GRANT OPTION; +---- + +#OR + +It is common for people to want to create a "root" user that can connect from anywhere, so as an example, we'll do just that, but to improve on it we'll create +a root user that can connect from anywhere on the local area network (LAN) + +---- +GRANT ALL PRIVILEGES ON *.* TO 'root'@'192.168.100.%' + IDENTIFIED BY 'my-new-password' WITH GRANT OPTION; +---- + +---- +FLUSH PRIVILEGES; +---- + +== Connecting + +---- +mysql -u [USER] -h [IP] -p +---- diff --git a/modules/ROOT/pages/_partials/proc_installing-mysql-from-container.adoc b/modules/ROOT/pages/_partials/proc_installing-mysql-from-container.adoc new file mode 100644 index 0000000..0c835cf --- /dev/null +++ b/modules/ROOT/pages/_partials/proc_installing-mysql-from-container.adoc @@ -0,0 +1,76 @@ +[id='install-from-container'] += Install from Podman + +== Downloading a SQL Server Docker Image + +---- +podman pull {mysql/mysql-server|mariadb/server} +---- + +== See Logs + +---- +podman logs {mysql|mariadb} +---- + +== Starting a MYSQL Server Instance + +The command's below contain the random password generated for the root user; + +---- +podman logs mysql 2>&1 | grep GENERATED +---- + +---- +podman -d -e MYSQL_ROOT_PASSWORD=mypassword mysql/mysql-Server +---- + +== Starting a MariaDB Server Instance + +---- +podman run -d --name=mariadb -ed MYSQL_ROOT_PASSWORD=mypassword -d mariadb/server +---- + +WARNING: Password blank default for MariaDB + +NOTE: The -d option used for _BOTH_ in the podman run command above makes the container run in the background. Use this command to monitor the output from the container: + +== Connecting to MySQL Server from within the Container + +---- +podman exec -it mysql mysql -uroot -p +---- + +you must reset the server root password by issuing this statement: + +---- +mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'password'; +---- + +== Connecting to Mariadb Server from within the Container + +---- +podman exec -it mariadb bash +---- + +== Reseting SQL_ROOT_PASSWORD + +you must reset the server root password by issuing this statement: + +---- +mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'password'; +---- + +== Stopping and Deleting a SQL Container + +---- +podman {start|stop|restart} {mysql|mariadb} +---- + +== Deleting a SQL Container + +---- +podman rm {mysql|mariadb} +---- + +WARNING: you can do the same with _docker_ just change _podman_ with _docker_. diff --git a/modules/ROOT/pages/_partials/proc_installing-mysql-from-fedora-repo.adoc b/modules/ROOT/pages/_partials/proc_installing-mysql-from-fedora-repo.adoc new file mode 100644 index 0000000..02ddacf --- /dev/null +++ b/modules/ROOT/pages/_partials/proc_installing-mysql-from-fedora-repo.adoc @@ -0,0 +1,79 @@ +[id='install-from-fedora-main-repo'] += Install from Fedora Main Repo + +The community provide a MySql package in the main repo. + +---- +sudo dnf install {community-mysql-server|mariadb-server} +---- + +== Configuring MySql/MariaDB + +Enable the service at boot and start: + +---- +sudo systemctl enable {mysqld|mariadb} +sudo systemctl start {mysqld|mariadb} +---- + +== Installing MariaDB server from the Fedora Modular repository + +To list the available versions (_streams_ in modularity terminology) of MariaDB: + +---- +dnf module list mariadb +---- + +To enable the version of MariaDB you want to use and make the stream RPMs available in the package set: + +---- +sudo dnf module enable mariadb:10.4 +---- + +At this point you can verify that the available RPM provides the 10.4 verison of MariaDB server: + +---- +dnf list mariadb-server +---- + +To install mariadb server: + +---- +sudo dnf module install mariadb/server +---- + +With modules, you could also install a specific profile: like client, devel or galera (the multi-master replica). +For instance, if you don't want to install the server stuff, but only the client packages: + +---- +sudo dnf module install mariadb:10.4/client +---- + +* MariaDB default root password is empty. +* find Default Password, For security reasons, MySQL generates a temporary root key. Please + +---- +sudo grep 'temporary password' /var/log/mysqld.log +---- + +== Configuring SQL before the first use + +---- +sudo mysql_secure_installation +---- + +Some questions will be asked: answer to them as you prefer; answering _yes_ to all of them is perfectly fine. + +== Using SQL + +---- +sudo mysql -u root -p +---- + +== Removing SQL + +I suggest to remove in the following way: + +---- +sudo dnf remove {community-mysql-server|mariadb-server} +---- diff --git a/modules/ROOT/pages/_partials/proc_installing-mysql-from-oracle.adoc b/modules/ROOT/pages/_partials/proc_installing-mysql-from-oracle.adoc new file mode 100644 index 0000000..8157bc1 --- /dev/null +++ b/modules/ROOT/pages/_partials/proc_installing-mysql-from-oracle.adoc @@ -0,0 +1,51 @@ +[id='install-from-oracle-mysql'] += Install from Oracle MySql + +include::{partialsdir}/3rdparty-message.adoc[] + +== Adding the MySQL repository to Fedora + +---- +sudo dnf install https://repo.mysql.com//mysql80-community-release-fc31-1.noarch.rpm +---- + +== Installing MySQL on Fedora + +---- +sudo dnf install mysql-community-server +---- + +== Start MySQL Service and Enable at Loggin: + +---- +sudo systemctl start mysqld +sudo systemctl enable mysqld +---- + +find Default Password, For security reasons, MySQL generates a temporary root key. Please note that MySQL has even stricter security policies than MariaDB. + +---- +sudo grep 'temporary password' /var/log/mysqld.log +---- + +== Configuring MySQL before the first use + +---- +sudo mysql_secure_installation +---- + +Then, answer the security questions as you prefer. or just say **yes** to all of them. + +== Using MYSQL + +---- +sudo mysql -u root -p +---- + +== Removing MySQL + +I suggest to remove in the following way, the most appropriate and safe way without removing many dependencies is: + +---- +sudo rpm -e --nodeps mysql-community-libs mysql-community-common mysql-community-server +---- diff --git a/modules/ROOT/pages/_partials/proc_installing-mysql.adoc b/modules/ROOT/pages/_partials/proc_installing-mysql.adoc deleted file mode 100644 index 578c3e7..0000000 --- a/modules/ROOT/pages/_partials/proc_installing-mysql.adoc +++ /dev/null @@ -1,22 +0,0 @@ -= Installing MySQL -== From the Fedora repository - -In order to install MySQL Community Edition from the Fedora repository: - ----- -$ sudo dnf install community-mysql-server ----- - -== From the MySQL repository - -include::{partialsdir}/3rdparty-message.adoc[] - -If you prefer to install the package from the third party repository maintained by MySQL (substitute the Fedora release accordingly): - ----- -$ sudo dnf install https://repo.mysql.com//mysql80-community-release-fc31-1.noarch.rpm ----- - ----- -$ sudo dnf install mysql-community-server ----- diff --git a/modules/ROOT/pages/_partials/proc_troubleshooting-mysql.adoc b/modules/ROOT/pages/_partials/proc_troubleshooting-mysql.adoc new file mode 100644 index 0000000..317c988 --- /dev/null +++ b/modules/ROOT/pages/_partials/proc_troubleshooting-mysql.adoc @@ -0,0 +1,96 @@ +[id='how-to-troubleshoot-issues-in-sql'] += How To Troubleshoot Issues in SQL + +Version: + +---- +dnf list installed | grep -i -e maria -e mysql -e galera +---- + +Check parameters in configuration file: + +* MySQL: + +---- +mysqld --print-defaults +---- + +* MariaDB/MySQL Comunnity: + +---- +/usr/libexec/mysqld --print-defaults +---- + +WARNING: Compatiblity between different version are not allowed Just install one of them. + +== How to Access SQL Error Logs + +Oftentimes, the root cause of slowdowns, crashes, or other unexpected behavior in SQL can +In many cases, the error logs are most easily read with the less program, a command line u + +if SQL isn’t behaving as expected, you can obtain more information about the source of the + +* **systemctl status mysqld.service** doesn't start well, This information doesn’t explain +well what is happening?, after this command you should type `journalctl -xe -u mariadb -u mysqld`. +* Look at Log files, can be located in `/var/log/mysql/mysqld.log` for MySQL, and `/var/log/mariabd` for MariaDB. + +== How To Troubleshoot Socket Errors in SQL + +SQL manages connections to the database server through the use of a socket file, a special kind of file that facilitates communications between different processes. The MySQL server’s socket file is named mysqld.sock and on Ubuntu systems it’s usually stored in the /var/run/mysqld/ directory. This file is created by the MySQL service automatically. + +Sometimes, changes to your system or your SQL configuration can result in SQL being unable to read the socket file, preventing you from gaining access to your databases. The most common socket error looks like this: + +---- +ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2) +---- + +There are a few reasons why this error may occur, and a few potential ways to resolve it. +One common cause of this error is that the SQL service is stopped or did not start to begin with, meaning that it was unable to create the socket file in the first place. To find out if this is the reason you’re seeing this error, try starting the service with _systemctl_: + +---- +sudo systemctl start {mysqld|mariadb} +---- + +Then try accessing the MySQL prompt again. If you still receive the socket error, double check the location where your MySQL installation is looking for the socket file. This information can be found in the `mysqld.cnf` file: + +look for the socket parameter in the [mysqld] section of this file. It will look like this: + +---- +[mysqld] +user = mysql +pid-file = /var/run/mysqld/mysqld.pid +socket = /var/run/mysqld/mysqld.sock +port = 3306 +---- + +Close this file, then ensure that the mysqld.sock file exists by running an ls command on the directory where SQL expects to find it: + +---- +ls -a /var/run/mysqld/ +---- + +If the socket file exists, you will see it in this command’s output: + +---- +mysqld.pid mysqld.sock mysqld.sock.lock +---- + +if the file does not exist, the reason may be that MySQL is trying to create it, but does not have adequate permissions to do so. You can ensure that the correct permissions are in place by changing the directory’s ownership to the mysql user and group: + +---- +sudo chown mysql:mysql /var/run/mysqld/ +---- + +Then ensure that the mysql user has the appropriate permissions over the directory. Setting these to 775 will work in most cases: + +---- +sudo chmod -R 755 /var/run/mysqld/ +---- + +Finally, restart the MySQL service so it can attempt to create the socket file again: + +---- +sudo systemctl restart {mysqld|mariadb} +---- + +Then try accessing the MySQL prompt once again. If you still encounter the socket error, there’s likely a deeper issue with your MySQL instance, in which case you should review the error log to see if it can provide any clues. diff --git a/modules/ROOT/pages/_partials/proc_using-mysql-mariadb.adoc b/modules/ROOT/pages/_partials/proc_using-mysql-mariadb.adoc index 77dbe90..372a42d 100644 --- a/modules/ROOT/pages/_partials/proc_using-mysql-mariadb.adoc +++ b/modules/ROOT/pages/_partials/proc_using-mysql-mariadb.adoc @@ -34,8 +34,4 @@ mysql> show schemas; == Files location -For both of them, the main configuration file is `/etc/my.cnf`; eventually the configuration can be splitted in many files inside the include directory `/etc/my.cnf.d`. - -Log files can be located in `/var/log/mysql` for MySQL, and `/var/log/mariabd` for MariaDB. - The database disk storage is located in `/var/lib/mysql`. diff --git a/modules/ROOT/pages/installing-mysql-mariadb.adoc b/modules/ROOT/pages/installing-mysql-mariadb.adoc index 35e3d14..42030dc 100644 --- a/modules/ROOT/pages/installing-mysql-mariadb.adoc +++ b/modules/ROOT/pages/installing-mysql-mariadb.adoc @@ -17,19 +17,12 @@ NOTE: MariaDB and MySQL packages conflict each other. You have to install MariaD In addition you can install MySQL commmunity edition (8.0 or 5.7) from the repository maintained by MySQL itself: https://dev.mysql.com/downloads/repo/yum/ -include::{partialsdir}/proc_installing-mysql.adoc[leveloffset=+1] - -include::{partialsdir}/proc_configuring-mysql.adoc[leveloffset=+1] - -include::{partialsdir}/proc_installing-mariadb.adoc[leveloffset=+1] - -include::{partialsdir}/proc_installing-mariadb-module.adoc[leveloffset=+1] - -include::{partialsdir}/proc_configuring-mariadb.adoc[leveloffset=+1] - +include::{partialsdir}/proc_installing-mysql-from-oracle.adoc[leveloffset=+1] +include::{partialsdir}/proc_installing-mysql-from-fedora-repo.adoc[leveloffset=+1] +include::{partialsdir}/proc_installing-mysql-from-container.adoc[leveloffset=+1] include::{partialsdir}/proc_using-mysql-mariadb.adoc[leveloffset=+1] - -include::{partialsdir}/proc_configuring-mysql-mariadb-firewall.adoc[leveloffset=+1] +include::{partialsdir}/proc_expose-outside-mysql.adoc[leveloffset=+1] +include::{partialsdir}/proc_troubleshooting-mysql.adoc[leveloffset=+1] ifdef::parent-context[:context: {parent-context}] ifndef::parent-context[:!context:]