{"id":1850,"date":"2023-04-05T21:45:12","date_gmt":"2023-04-05T15:45:12","guid":{"rendered":"http:\/\/techsatwork.com\/?p=1850"},"modified":"2023-04-05T21:45:13","modified_gmt":"2023-04-05T15:45:13","slug":"how-to-access-aurora-mysql-from-python-code-via-a-bastion-server","status":"publish","type":"post","link":"https:\/\/techsatwork.com\/?p=1850","title":{"rendered":"How to access Aurora MySQL from Python code via a bastion server"},"content":{"rendered":"\n<p data-renderer-start-pos=\"1\">This example assumes you have a bastion server setup that will allow connection to follow thru to an existing MySQL server. The python code is run on an EC2 server that has security group setup on the bastion server to allow connections from the EC2 server.<\/p>\n<p data-renderer-start-pos=\"1\">We need the following info:<\/p>\n<ol class=\"ak-ol\" data-indent-level=\"1\">\n<li>\n<p data-renderer-start-pos=\"282\">On the EC2 server make sure the SSHTunnerForwarder and pymysql libraries are installed using pip install<\/p>\n<\/li>\n<li>\n<p data-renderer-start-pos=\"391\">Get the following info:<\/p>\n<ol class=\"ak-ol\" data-indent-level=\"2\">\n<li>\n<p data-renderer-start-pos=\"418\">bastion server name<\/p>\n<\/li>\n<li>\n<p data-renderer-start-pos=\"441\">pem file to connect to bastion server along with the userid<\/p>\n<\/li>\n<li>\n<p data-renderer-start-pos=\"505\">Aurora mysql connection endpoint with the port<\/p>\n<\/li>\n<li>\n<p data-renderer-start-pos=\"555\">Aurora userid and password<\/p>\n<\/li>\n<li>\n<p data-renderer-start-pos=\"585\">Database name you want to connect to<\/p>\n<\/li>\n<li>\n<p data-renderer-start-pos=\"625\">Ensure that the localhost resolves to 127.0.0.1 if not then figure out what the local ip is<\/p>\n<\/li>\n<\/ol>\n<\/li>\n<\/ol>\n<p data-renderer-start-pos=\"722\">Here is a sample code that demonstrate how the python code can be constructed:<\/p>\n<div class=\"fabric-editor-breakout-mark fabric-editor-block-mark css-15vl9my\" data-mode=\"wide\">\n<div class=\"code-block  css-1si5tit\">\n<div role=\"presentation\">\u00a0<\/div>\n<span class=\"prismjs css-1x00our\" data-code-lang=\"\" data-ds--code--code-block=\"\"><span class=\"prismjs css-1x00our\" data-code-lang=\"\" data-ds--code--code-block=\"\"><code><\/code><\/span><\/span>\n<p>import pymysql<br \/>from sshtunnel import SSHTunnelForwarder<br \/><br \/>#ssh bastion host information<\/p>\n<p>ssh_host = &#8216;&lt;bastion server name&gt;&#8217; \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0### bastion server name<br \/>ssh_username = &#8216;ec2-user&#8217;<br \/>ssh_pkey = &#8216;&lt;location of the pem file&gt;&#8217; \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 ### pem file for the bastion server<br \/>ssh_port = 22<br \/><br \/># setting up the ssh tunnel<br \/><br \/>ssh_tunnel = SSHTunnelForwarder(<br \/>(ssh_host, ssh_port),<br \/>ssh_username=ssh_username,<br \/>ssh_pkey=ssh_pkey,<br \/>remote_bind_address=(&#8216;&lt;Mysql connection end point server name&gt;&#8217;, 3306) # MySQL end point and my sql port<br \/>)<br \/><br \/>#start the ssh tunnel<br \/><br \/>ssh_tunnel.start()<br \/><br \/>#mysql database connection info<br \/><br \/>db_user = &#8216;&lt;mysql user id &gt;&#8217;<br \/>db_password = &#8216;&lt;password for the mysql userid&gt;&#8217;<br \/>db_name = &#8216;&lt;name of the mysql database&#8217;<br \/>db_host = &#8216;127.0.0.1&#8217;<br \/><br \/>#connection string to mysql via ssh_tunnel<br \/><br \/>mysql_conn = pymysql.connect(<br \/>host=db_host, \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 # Use localhost, as the tunnel forwards the connection<br \/>port=ssh_tunnel.local_bind_port,<br \/>user=db_user,<br \/>password=db_password,<br \/>db=db_name<br \/>)<br \/><br \/>#connect connect and run the query via cursor<br \/><br \/>cursor = mysql_conn.cursor()<br \/>cursor.execute(&#8216;SELECT * FROM &lt;table name&gt;&#8217;)<br \/>for row in cursor.fetchall():<br \/>print(row)<br \/><br \/>#close mysql connection and stop the tunnel<br \/>mysql_conn.close()<br \/>ssh_tunnel.stop()<\/p>\n<span class=\"prismjs css-1x00our\" data-code-lang=\"\" data-ds--code--code-block=\"\"><span class=\"prismjs css-1x00our\" data-code-lang=\"\" data-ds--code--code-block=\"\"><code><\/code><\/span><\/span><span class=\"prismjs css-1x00our\" data-code-lang=\"\" data-ds--code--code-block=\"\"><span class=\"prismjs css-1x00our\" data-code-lang=\"\" data-ds--code--code-block=\"\"><code><\/code><\/span><\/span><span class=\"prismjs css-1x00our\" data-code-lang=\"\" data-ds--code--code-block=\"\"><span class=\"prismjs css-1x00our\" data-code-lang=\"\" data-ds--code--code-block=\"\"><code><\/code><\/span><\/span><span class=\"prismjs css-1x00our\" data-code-lang=\"\" data-ds--code--code-block=\"\"><code><\/code><\/span><\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>This example assumes you have a bastion server setup that will allow connection to follow thru to an existing MySQL server. The python code is run on an EC2 server that has security group setup on the bastion server to allow connections from the EC2 server. We need the following info: On the EC2 server [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-container-style":"default","site-container-layout":"default","site-sidebar-layout":"default","site-transparent-header":"default","disable-article-header":"default","disable-site-header":"default","disable-site-footer":"default","disable-content-area-spacing":"default","footnotes":""},"categories":[961,358],"tags":[1050,686,1051,62,1049],"class_list":["post-1850","post","type-post","status-publish","format-standard","hentry","category-aws","category-how-to","tag-aurora","tag-aws","tag-bastion-server","tag-mysql","tag-python"],"_links":{"self":[{"href":"https:\/\/techsatwork.com\/index.php?rest_route=\/wp\/v2\/posts\/1850","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/techsatwork.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/techsatwork.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/techsatwork.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/techsatwork.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1850"}],"version-history":[{"count":1,"href":"https:\/\/techsatwork.com\/index.php?rest_route=\/wp\/v2\/posts\/1850\/revisions"}],"predecessor-version":[{"id":1851,"href":"https:\/\/techsatwork.com\/index.php?rest_route=\/wp\/v2\/posts\/1850\/revisions\/1851"}],"wp:attachment":[{"href":"https:\/\/techsatwork.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1850"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/techsatwork.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1850"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/techsatwork.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1850"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}