{"id":4130,"date":"2018-01-05T18:34:21","date_gmt":"2018-01-05T16:34:21","guid":{"rendered":"http:\/\/www.laurentmarot.fr\/wordpress\/?p=4130"},"modified":"2018-01-05T19:19:38","modified_gmt":"2018-01-05T17:19:38","slug":"mysql-on-debian-cheat-sheet-for-my-students","status":"publish","type":"post","link":"https:\/\/www.laurentmarot.fr\/wordpress\/?p=4130","title":{"rendered":"MySQL on Debian (Cheat sheet for my students)"},"content":{"rendered":"<h2 id=\"harden-mysql-server\">Install MySQL Server<\/h2>\n<pre><code>sudo apt-get install mysql-server<\/code><\/pre>\n<p>During the installation process, you will be prompted to set a password for the MySQL root user as shown below. Choose a strong password and keep it in a safe place for future reference (Why not usin KeePass ?).<\/p>\n<div id=\"attachment_4133\" style=\"width: 310px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/www.laurentmarot.fr\/wordpress\/wp-content\/uploads\/2018\/01\/mysql-rootpw-debian.png\" rel=\"lightbox[4130]\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-4133\" class=\"size-medium wp-image-4133\" src=\"http:\/\/www.laurentmarot.fr\/wordpress\/wp-content\/uploads\/2018\/01\/mysql-rootpw-debian-300x87.png\" alt=\"mysql-rootpw-debian\" width=\"300\" height=\"87\" srcset=\"https:\/\/www.laurentmarot.fr\/wordpress\/wp-content\/uploads\/2018\/01\/mysql-rootpw-debian-300x87.png 300w, https:\/\/www.laurentmarot.fr\/wordpress\/wp-content\/uploads\/2018\/01\/mysql-rootpw-debian.png 650w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><p id=\"caption-attachment-4133\" class=\"wp-caption-text\">mysql-rootpw-debian<\/p><\/div>\n<p>MySQL will bind to localhost (127.0.0.1) by default. Then you can connect locally on to your databases.<\/p>\n<p>Allowing unrestricted access to MySQL on a public IP is not advised, but you may change the address it listens on by modifying the <code>bind-address<\/code> parameter in <code>\/etc\/my.cnf<\/code>. If you decide to bind MySQL to your public IP, you should implement firewall rules that only allow connections from specific IP addresses and then you&rsquo;ll be able to connect using SSH from another computer.<\/p>\n<h2 id=\"harden-mysql-server\">Harden MySQL Server<\/h2>\n<p>It&rsquo;s also a good thing to harden you database server. Fo taht, run the <code>mysql_secure_installation<\/code> script to address several security concerns in a default MySQL installation.<\/p>\n<pre><code>sudo mysql_secure_installation<\/code><\/pre>\n<p>You will be given the choice to :<\/p>\n<ol>\n<li>change the MySQL root password;<\/li>\n<li>remove anonymous user accounts;<\/li>\n<li>disable root logins outside of localhost,<\/li>\n<li>and remove test databases.<\/li>\n<\/ol>\n<p>It is recommended that you answer yes to these options.<\/p>\n<p>You can read more about the script in the <a href=\"https:\/\/dev.mysql.com\/doc\/refman\/5.0\/en\/mysql-secure-installation.html\">MySQL Reference Manual<\/a>.<\/p>\n<h2 id=\"harden-mysql-server\">Using MySQL<\/h2>\n<p>The standard tool for interacting with MySQL is the mysql client which installs with the mysql-server package. The MySQL client is used through a terminal. In a very firts time, it&rsquo;s definitely bad thing to use web GUI to discover mysql functionalities.<\/p>\n<h3><strong>Logging as root<\/strong><\/h3>\n<ol>\n<li>To log in to MySQL as the root user:\n<pre><code>mysql -u root -p\r\n<\/code><\/pre>\n<\/li>\n<li>When prompted, enter the root password.You\u2019ll then be presented with a welcome header and the MySQL prompt as shown below:\n<pre><code>mysql&gt;\r\n<\/code><\/pre>\n<\/li>\n<li>To generate a list of commands for the MySQL prompt, enter <code>\\h<\/code>. You\u2019ll then see:\n<pre><code>List of all MySQL commands:\r\nNote that all text commands must be first on line and end with ';'\r\n?         (\\?) Synonym for `help'.\r\nclear     (\\c) Clear command.\r\nconnect   (\\r) Reconnect to the server. Optional arguments are db and host.\r\ndelimiter (\\d) Set statement delimiter. NOTE: Takes the rest of the line as new delimiter.\r\nedit      (\\e) Edit command with $EDITOR.\r\nego       (\\G) Send command to mysql server, display result vertically.\r\nexit      (\\q) Exit mysql. Same as quit.\r\ngo        (\\g) Send command to mysql server.\r\nhelp      (\\h) Display this help.\r\nnopager   (\\n) Disable pager, print to stdout.\r\nnotee     (\\t) Don't write into outfile.\r\npager     (\\P) Set PAGER [to_pager]. Print the query results via PAGER.\r\nprint     (\\p) Print current command.\r\nprompt    (\\R) Change your mysql prompt.\r\nquit      (\\q) Quit mysql.\r\nrehash    (\\#) Rebuild completion hash.\r\nsource    (\\.) Execute an SQL script file. Takes a file name as an argument.\r\nstatus    (\\s) Get status information from the server.\r\nsystem    (\\!) Execute a system shell command.\r\ntee       (\\T) Set outfile [to_outfile]. Append everything into given outfile.\r\nuse       (\\u) Use another database. Takes database name as argument.\r\ncharset   (\\C) Switch to another charset. Might be needed for processing binlog with multi-byte charsets.\r\nwarnings  (\\W) Show warnings after every statement.\r\nnowarning (\\w) Don't show warnings after every statement.\r\n\r\nFor server side help, type 'help contents'\r\n\r\nmysql&gt;\r\n<\/code><\/pre>\n<\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n<h3 id=\"create-a-new-mysql-user-and-database\">Create a New MySQL User and Database<\/h3>\n<ol>\n<li>In the example below, <code>testdb<\/code> is the name of the database, <code>testuser<\/code> is the user, and <code>password<\/code> is the user\u2019s password.\n<div class=\"btn-copy\"><\/div>\n<pre><code>create database testdb;\r\ncreate user 'testuser'@'localhost' identified by 'password';\r\ngrant all on testdb.* to 'testuser';\r\n<\/code><\/pre>\n<p>You can shorten this process by creating the user <em>while<\/em> assigning database permissions:<\/p>\n<pre><code>create database testdb;\r\ngrant all on testdb.* to 'testuser' identified by 'password';\r\n<\/code><\/pre>\n<\/li>\n<li>Then exit MySQL.\n<pre><code>exit\r\n<\/code><\/pre>\n<\/li>\n<\/ol>\n<h3 id=\"create-a-sample-table\">Create a Sample Table<\/h3>\n<ol>\n<li>Log back in as <code>testuser<\/code>.\n<pre><code>mysql -u testuser -p\r\n<\/code><\/pre>\n<\/li>\n<li>Create a sample table called <code>customers<\/code>. This creates a table with a customer ID field of the type <code>INT<\/code> for integer (auto-incremented for new records, used as the primary key), as well as two fields for storing the customer\u2019s name.\n<pre><code>use testdb;\r\ncreate table customers (customer_id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, first_name TEXT, last_name TEXT);\r\n<\/code><\/pre>\n<\/li>\n<li>Then exit MySQL.\n<pre><code>exit\r\n<\/code><\/pre>\n<\/li>\n<\/ol>\n<h3 id=\"reset-the-mysql-root-password\">Reset the MySQL Root Password<\/h3>\n<p>If you forget your root MySQL password, it can be reset.<\/p>\n<ol>\n<li>Stop the current MySQL server instance.\n<pre><code>sudo systemctl stop mysql.serivce\r\n<\/code><\/pre>\n<\/li>\n<li>Use dpkg to re-run the configuration process MySQL goes through on first installation. You will again be asked to set a root password.\n<pre><code>sudo dpkg-reconfigure mysql-server-5.5\r\n<\/code><\/pre>\n<\/li>\n<\/ol>\n<p><code>dpkg<\/code> will restart MySQL automatically and you\u2019ll now be able to log in again using <code>mysql -u root -p<\/code>.<\/p>\n<h3 id=\"tune-mysql\">Tune MySQL<\/h3>\n<p><a href=\"https:\/\/github.com\/major\/MySQLTuner-perl\">MySQL Tuner<\/a> is a Perl script that connects to a running instance of MySQL and provides configuration recommendations based on workload. Ideally, the MySQL instance should have been operating for at least 24 hours before running the tuner. The longer the instance has been running, the better advice MySQL Tuner will give.<\/p>\n<ol>\n<li>Install MySQL Tuner from Ubuntu\u2019s repositories.\n<pre><code>sudo apt-get install mysqltuner\r\n<\/code><\/pre>\n<\/li>\n<li>To run it:\n<pre><code>mysqltuner\r\n<\/code><\/pre>\n<p>You will be asked for the MySQL root user\u2019s name and password. The output will show two areas of interest: General recommendations and Variables to adjust.<\/li>\n<\/ol>\n<p>MySQL Tuner is an excellent starting point to optimize a MySQL server but it would be prudent to perform additional research for configurations tailored to the application(s) utilizing MySQL on your Linode.<\/p>\n<h2>More Information<\/h2>\n<p>You may wish to consult the following resources for additional information on this topic. While these are provided in the hope that they will be useful, please note that we cannot vouch for the accuracy or timeliness of externally hosted materials.<\/p>\n<ul>\n<li><a href=\"https:\/\/dev.mysql.com\/doc\/refman\/5.5\/en\/index.html\">MySQL 5.5 Reference Manual<\/a><\/li>\n<li><a href=\"http:\/\/us2.php.net\/manual\/en\/book.mysql.php\">PHP MySQL Manual<\/a><\/li>\n<li><a href=\"http:\/\/sql-info.de\/mysql\/examples\/Perl-DBI-examples.html\">Perl DBI examples for DBD::mysql<\/a><\/li>\n<li><a href=\"http:\/\/mysql-python.sourceforge.net\/MySQLdb.html\">MySQLdb User\u2019s Guide<\/a><\/li>\n<li><a href=\"http:\/\/www.debiantutorials.com\/tuning-mysql-with-mysqltuner-to-increase-efficiency-and-performance\">MySQL Tuner Tutorial<\/a><\/li>\n<\/ul>\n<h2><\/h2>\n\n","protected":false},"excerpt":{"rendered":"<p>Install MySQL Server sudo apt-get install mysql-server During the installation process, you will be prompted to set a password for the MySQL root user as shown below. Choose a strong password and keep it in a safe place for future reference (Why not usin KeePass ?). MySQL will bind to localhost (127.0.0.1) by default. Then [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[39],"tags":[],"_links":{"self":[{"href":"https:\/\/www.laurentmarot.fr\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/4130"}],"collection":[{"href":"https:\/\/www.laurentmarot.fr\/wordpress\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.laurentmarot.fr\/wordpress\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.laurentmarot.fr\/wordpress\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.laurentmarot.fr\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=4130"}],"version-history":[{"count":10,"href":"https:\/\/www.laurentmarot.fr\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/4130\/revisions"}],"predecessor-version":[{"id":4141,"href":"https:\/\/www.laurentmarot.fr\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/4130\/revisions\/4141"}],"wp:attachment":[{"href":"https:\/\/www.laurentmarot.fr\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4130"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laurentmarot.fr\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4130"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laurentmarot.fr\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4130"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}