Solving: Can’t connect to MySQL server on ‘IP Adress’ (10061)

MySQL remote connection error

If you are having this problem, it means that your MySQL server is now allowing remote connections.

To solve this, you must follow the next steps:

1. Login into your machine via SSH with a ROOT user.
2. Connect to your MySQL server:

mysql --host=localhost --user=myname --password=mypass

3. Execute next line on MySQL console with your username and password:

GRANT ALL PRIVILEGES ON *.* TO 'your_mysql_username'@'%' IDENTIFIED BY 'your_mysql_password' WITH GRANT OPTION;

4. Open MySQL folder:

cd /etc/mysql/

5. Change Bind Ip Adress on my.cnf file:

sudo nano my.cnf

6. Change the ip adress ‘127.0.0.1’ to your local machine IP adress:

Modifying MySQL server bin adress
 

7. Save file changes pressing Ctrl + O to save and Ctrl + X to exit from editor.
8. Now, restart your MySQL server and you should connect externally to it.

sudo service mysql restart

 

How to show the current GIT branch name in command prompt

git-branches

We can get the current GIT branch name in command prompt with adding a simple code into the .bashrc file.

Open your terminal and type the following command:

sudo nano ~/.bashrc

Go to the end of the file and add this code:

function parse_git_branch () {
  git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ (\1)/'
}
 
RED="\[\033[0;31m\]"
YELLOW="\[\033[0;33m\]"
GREEN="\[\033[0;32m\]"
NO_COLOR="\[\033[0m\]"
 
PS1="$GREEN\u@\h$NO_COLOR:\w$YELLOW\$(parse_git_branch)$NO_COLOR\$ "

Save file changes.

Now, if you open a new terminal and access into a GIT repository, you will see the current GIT branch name.

How to ignore folder tracking with GIT

A frequently asked question on GIT is how to ignore a folder tracking. We can have in our project folders which contains cache files, file uploads, etc and are unnecesary to be included in our GIT repository.

To do this, we need to create a .gitignore file on our GIT root folder.

Then, if we want to disable a folder for tracking, we add the next line on the file:

folder/*

 
Save the .gitignore file.

Now all files and subfolders from this directory will be ignored by GIT.

How to manage a website with GIT

It is not known by many people, but we can use GIT to manage our website or web project in seconds, without the need to upload one by one all the files through a FTP clients.

Maybe, your are asking: Why I must start using it?

  • You can update your website to the last version in seconds.
  • You can go back to the lastest stable version in seconds in case you have any problem on production environment.
  • Prevent human errors when you are uploading many files with a FTP client.
  • Your website update is faster.
  • Prevent reupload unnecesary files.

If you want to try it, continue reading:

First, you need a Linux server with shell access and root permissions.

Obviously, you must have GIT installed in your server.

sudo apt-get install git

Once you have installed GIT, create a folder where you are going to save our GIT repositories.

mkdir /var/www/gitrep/project.git

Access to the just created folder

cd /var/www/gitrep/project.git/

Init a GIT bare repository

git init --bare

A bare repository, is a central repository. This means that is a repository where we only are going to do push from our project.

Now, we create a HOOK which is used for the auto-updating the folder where is located our web project in our web server when we do a push.

sudo vi /var/www/gitrep/project.git/hooks/post-update

Enter the next content:

GIT_WORK_TREE=/var/www/project/ git checkout -f

Replace ‘/var/www/project/’ for the path where is located your project folder in your web server. After this, save the file changes.

Then, you must set necessary permissions to the HOOK you created before.

chmod +x /var/www/gitrep/project.git/hooks/post-update

With the GIT repository configured in your web server, it is time to configure a few things in our local repository (working copy).

First, add remote repository to your local repository:

git remote add remote_repository_name ssh://user@host/var/www/git_rep/project.git

Set write permissions to your GIT repository

sudo chmod -R 0777 /var/www/git_rep/project.git

Push local repository content to remote repository

git push remote_repository_name +master:refs/heads/master

In case that your web server hasn’t updated your project with your local repository content, you must to do a local change in some of your project files, then create a new commit, and push to the remote repository again.

git push remote_repository_name master

And this is all you must to know. Now, when you do a push into your remote (production) server your project will be updated automatically.

If you have any question, tell us as a post comment.

How to auto backups MySQL databases with Linux crontab

It is very useful having automatic backups from your MySQL databases to avoid data loss from any of our projects. With a simple command line in our crontab, we can do it easily.

First of all, we must login as root user to our server


ssh root@server_ip_or_hostname

We are going to use the command ‘myslqdump‘, which is used to create a MySQL backup from a specified database.

mysqldump -u database_username -pdatabase_password database_name | gzip > /var/www/db_backups/mysite.com/$(date +%d-%m-%Y).gz

We must replace the following vars:

– database_username: username to login to database
– database_password: password to login to database
– database_name: name of the database to connect

When we are inserting your database password, the ‘p’ character goes together with the password string.

For example, this are my credentials:

– Database user: root
– Database password: 1234
– Database name: my_project_db

Our command will be this:

mysqldump -u root -p1234 my_project_db | gzip > /var/www/db_backups/mysite.com/$(date +%d-%m-%Y).gz

Gzip parameter says that our sql file must be compressed with gzip. And the last part of the command, indicates where will be created our backup, with the next format name for the file: DAY-MONTH-YEAR.gz

With your ‘mysqldump‘ command generated with the credentials, we are going to create a cron job in our linux crontab for beign executed and create a MySQL backup for your project every day.

First of all, open your crontab:

crontab -e

Note: You must escape the ‘%’ character with ‘\’ from your ‘mysqldump’ command to be executed by the crontab.

$(date +\%d-\%m-\%Y).gz

Example with the full command:

mysqldump -u database_username -pdatabase_password database_name | gzip > /var/www/db_backups/mysite.com/$(date +\%d-\%m-\%Y).gz

Create the cron tab:

30 4 * * * HERE_YOUR_MYSQLDUMP_COMMAND

For example:

30 4 * * * mysqldump -u database_username -pdatabase_password database_name | gzip > /var/www/db_backups/mysite.com/$(date +\%d-\%m-\%Y).gz

This crontab will be executed every day at 4:30.

Save your crontab, and enjoy your daily MySQL backups.