Install Go 1.8 on Fedora / Ubuntu

You can follow below steps to install Go 1.8 on Fedora / Ubuntu. (It should work for other Linux distribution as well but it is only tested for Ubuntu / Fedora).

Step 1:

Download the Go 1.8 bundle by clicking here.

Step 2:

Extract the downloaded bundle using below command,

tar -C /usr/local -xzf go1.8.linux-amd64.tar.gz 

Step 3:

Add below to your to your path variable, you can add this to either of bash_profile or /etc/profile.

export PATH=$PATH:/usr/local/go/bin

Reload the configuration & GO will be available to use.

 

Mysql Dump in XML format

To take dump in XML format you have to provide an extra option in default mysqldump command. This can be useful in cases where you want to migrate your MySQL database to some other database.

You can use below command to take MySQL Database dump in XML format.


mysqldump --xml -u[username] -p[password] [db_name] > filename.xml

or


mysqldump -X -u[username] -p[password] [db_name] > filename.xml

For example if you have a database test_database with an username as root and password as mypassword the command to take dump will be,


mysqldump --xml -u root -p mypassword test_database > filename.xml

Handling two Django models in a single POST

There are times when, you want to handle multiple Django models in a single POST request (By this i meant data posted from html form/ template contains data of two different Django models). I was struggling with this few months back and got a fantastic reference link from #django on IRC.

You can use the below method as a simple example, here’s a view that updates a User and their Profile in one view:

def user_edit(request):
    if request.method == 'POST':
        user_form = UserForm(request.POST, instance=request.user)
        profile_form = ProfileForm(request.POST, instance=request.user.profile)

        if all([user_form.is_valid(), profile_form.is_valid()]):
            user = user_form.save()
            profile = profile_form.save()
            return redirect(user)
    else:
        user_form = UserForm(instance=request.user)
        profile_form = ProfileForm(instance=request.user.profile)

    return render(request, 'user_form.html', {
            'user_form': user_form,
            'profile_form': profile_form,
            })

The two most important things to notice here are:

a. The use of all() instead of user_form.is_valid() and profile_form.is_valid().

This is because Python will skip the if clause if the first form is not valid, and so it won’t run validation on the second form. If some error is present in second from user will again get the error second time. using all() runs validation on all the submitted forms thus you get the errors at one time.

b. Two forms in the context.

Just render them all inside the one <form> tag, and it will be fine. If any of the field names clash, pass a prefix= when constructing one of the forms, to prefix its field names so they’re unique.

Related blogs :

  1. Django – AttributeError: ‘Http404’ object has no attribute ‘status_code’
  2. Make primary key with two or more field in Django

Set up SSH on Github

If you want to setup SSH trust between your local development machine & a project hosted on Github, please follow the process below & you will be able to setup it. This process is tested on Fedora/ Ubuntu but it should work on any Linux based OS. On your local Linux machine please generate a RSA SSH Public key if already not generated. You can check it by going to .ssh folder of home directory. If not please execute the below command and follow the instructions:

Step 1:

 ssh-keygen -t rsa 

After you are done with generating SSH key Please execute the below two commands on terminal.

 eval "$(ssh-agent -s)" 
 ssh-add ~/.ssh/id_rsa 

Now copy the content of id_rsa.pub to clipboard.

Step 2:

Open this URL in your browser : https://github.com/settings/ssh  , you will see there are two options are there to be filled.

Title : <Your Title> you can fill some meaningful title here. example : Shashank’s Fedora Laptop Key

Key : Paste the content of id_rsa.pub file & save it.

You can verify it by running below command on terminal.

ssh git@github.com

It will show your github username with some other information. If not please check the steps above.

Step 3:

Now, goto your git project’s home directory and run below command:

git remote -v

Below result should be shown on Terminal.

origin https://github.com/USERNAME/PROJECTNAME.git (fetch)
origin https://github.com/USERNAME/PROJECTNAME.git (push)

Your fetch and push origin URL Will be listed. If it is a HTTPS URL change it by running,

git remote set-url origin git@github.com:USERNAME/PROJECTNAME.git

Verify it by running “git remote -v” again. You will get below result on terminal.

origin git@github.com:USERNAME/PROJECTNAME.git (fetch)
origin git@github.com:USERNAME/PROJECTNAME.git (push)

Now , goto your projects home and try to run git commands to Fetch/ Pull from origin it’ll work through SSH.

If you feel there is a better way to setup SSH on Github please leave a comment.

Cinnamon Desktop on Fedora

Installing Cinnamon desktop on fedora is very easy. You can execute the below command from your terminal.

sudo dnf install @cinnamon-desktop

How to change existing desktop

You can restart your fedora system & while signing in you can select it from a available options settings which is present alongside signin button.

You can also install other fedora Desktop,

Mate Desktop on Fedora

dnf install @mate-desktop

KDE Desktop on Fedora

 dnf install @kde-desktop

For more information about other available desktop spins you can visit here : https://spins.fedoraproject.org/

Related Posts :

Install Redis server on fedora.

Install Redis server on Fedora

Installing Redis server on fedora is very easy. open your terminal and execute the below command,


sudo dnf install redis

Start the redis server by executing


sudo systemctl start redis

Redis server will start running at port 6379.

You can check the it’s current status by executing below command


sudo systemctl status redis

Ignore file permission changes in git

If the permission of a file changes inside a git repository,  Git identifies that as a changed file even though you haven’t changed that file. For example if you change a file’s permission from 644 to 755, It will be shown as a modified file. If you don’t want this to be shown as a modified file.

Execute below command in your repository home.

git config core.fileMode false

and if you want to change it for all the git repository in your system.

git config --add --global core.filemode false

MySQL General query logs

MySQL prints each query in General query logs. This can be helpful in application profiling if you want to see how many and which queries are being run in a transaction and when commit/rollback is executed.

You can enable MySQL general query logs by running below sql command from root user:

SET GLOBAL general_log = 'ON';

By default, logs will be stored at /var/log/query.log ,  If you are getting the “permission error” after running above command.

Edit your mysqld.conf file which is located under /etc/mysql/mysql.conf.d ( This location may be different for your installation )

vi /etc/mysql/mysql.conf.d/mysqld.conf

and change the value of

general_log_file = /var/log/query.log

to

general_log_file = /var/lib/mysql/your_log_filename.log

In my case default general query log file location was /var/log/query.log & MySQL didn’t had permission to write at that location.

Credits : Chirag Jain

Increase MySQL maximum connection limit

MySQL’s default configuration sets the maximum concurrent connections to 151. If you get a too many connections error when you are trying to connect to MySQL server, this means that all available connections are in use by other clients / Users.

The number of connections permitted is controlled by the max_connections system variable. Its default value is 151, although you can it can accept a max of 152 connection (1 Connection reserved for Super User).  If you need to support more connections, you can set a larger value for this .

MySQL 3.x:

# vi /etc/my.cnf
set-variable = max_connections = 170

MySQL 4.x and 5.x:

# vi /etc/my.cnf
max_connections = 170

Restart MySQL once you’ve made the changes.

 /etc/init.d/mysqld restart 

or

 service mysqld restart 

You can also change this variable after logging into mysql server as a root user.

max conn var change

change max connections limit from mysql terminal

Find the existing limit :

show variables like ‘max_connections’;

Set the updated maximum connection limit :

SET GLOBAL max_connections = 170;