{"id":1088,"date":"2013-09-13T09:40:53","date_gmt":"2013-09-13T09:40:53","guid":{"rendered":"https:\/\/raspberry-projects.com\/pi\/?p=1088"},"modified":"2016-01-19T10:42:00","modified_gmt":"2016-01-19T10:42:00","slug":"mysql","status":"publish","type":"post","link":"https:\/\/raspberry-projects.com\/pi\/software_utilities\/web-servers\/mysql","title":{"rendered":"MySQL"},"content":{"rendered":"<p>\n&nbsp;\n<\/p>\n<h4>\nInstalling MySQL<br \/>\n<\/h4>\n<p>\nIf you want MySQL also do the following:\n<\/p>\n<pre>\r\n<code>\r\nsudo apt-get install mysql-server\r\nsudo apt-get install php5-mysql\r\n<\/code><\/pre>\n<p>\nThe php5-mysql install adds the mysql libraries to allow PHP to access the mysql database.\n<\/p>\n<h4>\nAccessing MySQL from the command line<br \/>\n<\/h4>\n<p>\nFirst connect to the database and specify a user:\n<\/p>\n<pre>\r\n<code>\r\nmysql -p -u root\r\n<\/code><\/pre>\n<p>\nThen enter the users password when prompted. You should now have a mysql&gt; prompt. &nbsp;\n<\/p>\n<h5>\nExiting MySQL&nbsp;connection \/ command line login<br \/>\n<\/h5>\n<pre>\r\n<code>\r\nquit\r\n<\/code><\/pre>\n<h4>\nRestarting The MySQL service<br \/>\n<\/h4>\n<pre>\r\n<code>\r\nsudo service mysql restart\r\n<\/code><\/pre>\n<p>\n&nbsp;\n<\/p>\n<h4>\nCreating A Database<br \/>\n<\/h4>\n<p>\nFist connect to the database using &quot;mysql -p -u root&quot;. &nbsp;To create a database:\n<\/p>\n<pre>\r\n<code>\r\nCREATE DATABASE MY_DATABASE_NAME;\r\n<\/code><\/pre>\n<h4>\nAdding A&nbsp;User<br \/>\n<\/h4>\n<p>\nLogin to mysql using &quot;mysql -p -u root&quot; and then create a new user to avoid using root:\n<\/p>\n<pre>\r\n<code>\r\nCREATE USER &#39;MY_USERNAME&#39;@&#39;localhost&#39; IDENTIFIED BY &#39;MY_PASSWORD&#39;;\r\n<\/code><\/pre>\n<h4>\nAccessing a&nbsp;database<br \/>\n<\/h4>\n<h5>\nLocal Access<br \/>\n<\/h5>\n<p>\nFor security reasons, by default&nbsp;access to the MySQL server via the main IP address is disabled in the MySQL config. You can connect locally using:\n<\/p>\n<p style=\"margin-left: 40px;\">\nlocalhost<br \/>\n127.0.0.1<br \/>\nor the internal socket connection on &quot;\/var\/run\/mysqld\/mysqld.sock&quot;\n<\/p>\n<h5>\nRemote Access<br \/>\n<\/h5>\n<p>\nFirst we need to edit the MySQL config:\n<\/p>\n<pre>\r\n<code>\r\nsudo nano \/etc\/mysql\/my.cnf\r\n<\/code><\/pre>\n<p>\nFind the configuration line called bind-address. By default this is set to 127.0.0.1.&nbsp;\n<\/p>\n<p>\nThis is&nbsp;the local address(es) \/ network adaptors that MySQL will listen for connections on. The RPi default is 127.0.0.1 for&nbsp;localhost&nbsp;only. To allow connection on all interfaces set it to &quot;0.0.0.0&quot;\n<\/p>\n<p>\nSet it to &quot;0.0.0.0&quot; and save the file\n<\/p>\n<pre>\r\n<code>\r\nsudo service mysql restart\r\n<\/code><\/pre>\n<p>\n&nbsp;\n<\/p>\n<p>\nTo grant&nbsp;access for a remote connection, login to mysql using &quot;mysql -p -u root&quot; and then create a new user to avoid using root:\n<\/p>\n<pre>\r\n<code>\r\nCREATE USER &#39;MY_USERNAME&#39;@&#39;localhost&#39; IDENTIFIED BY &#39;MY_PASSWORD&#39;;\r\n<\/code><\/pre>\n<p>\nTo allow access from any IP address\n<\/p>\n<pre>\r\n<code>\r\nGRANT ALL PRIVILEGES ON MY_DATABASE_NAME.* TO &#39;MY_USERNAME&#39;@&#39;%&#39; IDENTIFIED BY &#39;MY_PASSWORD&#39;;\r\nflush privileges;\r\n<\/code><\/pre>\n<p>\nTo allow access from a fixed IP address\n<\/p>\n<pre>\r\n<code>\r\nGRANT ALL PRIVILEGES ON MY_DATABASE_NAME.* TO &#39;MY_USERNAME&#39;@&#39;12.34.56.78&#39; IDENTIFIED BY &#39;MY_PASSWORD&#39;;\r\nflush privileges;\r\n<\/code><\/pre>\n<p>\n&nbsp;\n<\/p>\n<h4>\nChanging MySQL root User Password<br \/>\n<\/h4>\n<p>\nSee <a href=\"http:\/\/www.debian-administration.org\/articles\/442\" target=\"_blank\">here<\/a>.\n<\/p>\n<p>\n&nbsp;\n<\/p>\n<h4>\nCopy Database To\/ From A Remote Server<br \/>\n<\/h4>\n<h5>\nInsecure Method<br \/>\n<\/h5>\n<p>\nIf you have direct access to the remote server and aren&#39;t worried about&nbsp;security\n<\/p>\n<pre>\r\n<code>\r\nmysqldump -h [server] -u [user] -p[password] [databasename] | mysql -h [server] -u [user] -p[password] [databasename]\r\n<\/code><\/pre>\n<p>\nNote: There is NO space between -p and [password]<br \/>\nThe left side is the from, the right side is the to. &nbsp;[server] can be localhost on either side.\n<\/p>\n<h5>\nSecure Method<br \/>\n<\/h5>\n<p>\nIf you can SSH&nbsp;into the remote server you can use this\n<\/p>\n<pre>\r\n<code>\r\nmysqldump -h [server] -u [user] -p[password] [databasename] | ssh [ssh_username]@remote_domain.com mysql -u [user] -p[password] [databasename]\r\n<\/code><\/pre>\n<p>\nYou will then be promoted for the ssh password of the remote server\n<\/p>\n<h4>\nCopy A Single Database Table To\/From A Remote Server<br \/>\n<\/h4>\n<h5>\nInsecure Method<br \/>\n<\/h5>\n<p>\n<span style=\"font-size: 10px;\">If you have direct access to the remote server and aren&#39;t worried about&nbsp;security<\/span>\n<\/p>\n<p>\nIts the same method as for copying an entire database except that you specify the tablename after the from database name:\n<\/p>\n<pre>\r\n<code>\r\nmysqldump -h [server] -u [user] -p[password] [databasename] [tablename] | mysql -h [server] -u [user] -p[password] [databasename]\r\n<\/code><\/pre>\n<p>\nIf the table already exists it is overwritten.\n<\/p>\n<h4>\nUsing SSL For Database &amp; Table Copy<br \/>\n<\/h4>\n<p>\nSee this guide <a href=\"http:\/\/www.howtoforge.com\/how-to-set-up-mysql-database-replication-with-ssl-encryption-on-centos-5.4\" target=\"_blank\">here<\/a>.\n<\/p>\n<p>\nIn theory using the &#8211;ssl&nbsp;option with mysqldump&nbsp;enables the connection over ssl&nbsp;but we&#39;ve not verified this and it doesn&#39;t look like you get errors for domains with no SSL certificate setup so maybe this option is only good if you&#39;ve alread ensure SSL is available?:\n<\/p>\n<pre>\r\n<code>\r\nmysqldump --ssl\r\n<\/code><\/pre>\n<p>\n&nbsp;\n<\/p>\n<p>\n&nbsp;\n<\/p>\n<p>\n&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>&nbsp; Installing MySQL If you want MySQL also do the following: sudo apt-get install mysql-server sudo apt-get install php5-mysql The php5-mysql install adds the mysql libraries to allow PHP to access the mysql database. Accessing MySQL from the command line First connect to the database and specify a user: mysql -p -u root Then enter [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[120,34],"tags":[],"class_list":["post-1088","post","type-post","status-publish","format-standard","hentry","category-databases","category-web-servers"],"_links":{"self":[{"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/posts\/1088","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/comments?post=1088"}],"version-history":[{"count":19,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/posts\/1088\/revisions"}],"predecessor-version":[{"id":1524,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/posts\/1088\/revisions\/1524"}],"wp:attachment":[{"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/media?parent=1088"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/categories?post=1088"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/tags?post=1088"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}