Securing mySQL
Now that we have our mySQL server installed it's time for us to secure it. The good news is that it is really easy to do so. mySQL comes with a wizard that helps you get rid of security threats to your installation. The wizard is located at: /usr/bin/mysql_secure_installation.
Video Transcript
00:00All right, so now that we have our server installed,
00:03the next step is actually to secure it.
00:06By default, MySQL comes with users
00:09that we really don't want to have in existence at all.
00:13So to make sure that we don't have those,
00:15MySQL comes with a really wonderful package
00:17that enables you to install MySQL in a more secure way.
00:22To do that, I'm gonna call the sudo command,
00:25and then I'm gonna go to the folder user
00:27by putting a backslash first and then typing user.
00:31I could even type through the tab
00:32and it should fill it up for me automatically.
00:35Then into the bin folder,
00:37and then I'm gonna call the MySQL secure installation.
00:40MySQL secure installation.
00:47Now once I call this command,
00:48all I have to do is press and enter,
00:50and it's gonna start going through this wizard
00:53that will help me through the installation
00:55of securing my SQL server.
00:59First of all, this is recommended
01:02to any production level MySQL,
01:04so you should do this in every single one,
01:06and we're gonna read through carefully the steps involved.
01:09First of all, we need to put in our current password,
01:11and the default is no password
01:14because our first installation came without a password.
01:16Now imagine a real live application
01:19where your root user that has access to everything
01:21that could delete everything has no server.
01:24That seemed incredibly scary and not that smart.
01:27We're gonna click on enter, so initially,
01:29because we don't have currently a password.
01:32Once we do that, it's gonna ask us
01:33if we want to create our own password,
01:36a password for the root, and we absolutely do,
01:38so I'm gonna type there Y, capital Y,
01:40and I'm gonna click on enter.
01:42It's gonna then ask me to put inside a new password.
01:45I'm gonna put here a very robust password,
01:47and so should you.
02:01Okay, so I put here a very robust password.
02:14There we go, so we just put here twice
02:18a really complicated password.
02:19Obviously, it's something that you should recall.
02:21The longer it is, the better.
02:22As long as you remember it, save it somewhere,
02:24this is not something that you're gonna have to use
02:26on a daily basis, and you wanna make sure
02:28that it's a very complicated password.
02:30The more random characters,
02:32the more non-language-oriented passwords, the better.
02:36Numbers, anything else you can mix inside
02:39to make sure that this password is not that easy
02:41to decrypt, all right?
02:43So now, the next step is gonna ask us,
02:45by default, there is an anonymous user
02:49allows them to come into your MySQL
02:52without even having any password.
02:54This is created for testing,
02:56but we really don't want to have that user,
02:58so it's asking us if we wanna remove that user,
03:00and we're gonna say yes again
03:01by putting there a capital Y and putting an enter there.
03:05Now, this is a really important one as well.
03:08Do we want the root user to be able
03:11to be logged in from anywhere?
03:12And what that means is, localhost means
03:17that the only way to log into that root user
03:20is if you're already on our server.
03:23And what that means is, for example,
03:24if our PHP script, which is on the same server,
03:28wants to communicate with the MySQL using our root user,
03:32it will be able to because it's sitting on the same server.
03:36Now, if you're running on, for example,
03:38that you have your MySQL on one server
03:40in a completely different IP,
03:42and you have your real server in a completely different IP,
03:45then it's not a localhost,
03:46then the communications are with real IPs.
03:49But really, you never wanna give access
03:51to anyone to your localhost.
03:54So even if you have an application
03:55that is running on a different server,
03:57you would probably give it a different username
03:59and put it inside of a sandbox
04:01and give it access only to specific things.
04:04So really, we always wanna put yes to this
04:07because we wanna make sure
04:08that the only way to access the root account
04:10is if you're inside our server
04:12or from our SSH or from our PHP
04:15once we install the PHP later on,
04:17but it has to be on the same server.
04:19The communication must be local
04:20for it to actually accept the username and password,
04:24which lowers the probability
04:25of someone hacking into your MySQL using your root
04:28because they would have to first hack into your PHP,
04:31hack into your server,
04:33and only then they could try to hack
04:34into your root account.
04:36Okay, so I'm gonna click on yes to this as well
04:39because this is really important.
04:40But really, by the way,
04:41this is all the stuff we wanna say yes to all of them.
04:46And by the way,
04:46O2, the MySQL comes with a test database.
04:50We wanna get rid of it.
04:51It's accessible to anyone.
04:53It's really there only for testing.
04:54So if you're a MySQL developer, you might have that,
04:58but you would never have it
04:59on a production level database or a production level.
05:03And by the way, even if you have your stage
05:05and a live server,
05:06as long as they're on the web,
05:07they're all production
05:08because someone could hack into them.
05:10So anything that's sitting on a real server
05:11and not on your local hard drive,
05:13you wanna consider that as a real server.
05:15So we're gonna type for this yes as well.
05:17We wanna get rid of that database that already exists
05:20that we just don't wanna have there.
05:24Let's see, reloading privileges tables will change so far.
05:27And this basically doesn't really matter for us the second
05:30because we don't have any users logged in.
05:31But what this will do is it's gonna reload
05:33the privileges table right away right now,
05:36making sure it kicks out any user
05:38that isn't allowed to be inside of here.
05:41So I'm gonna click on yes for sure
05:43because it definitely cannot hurt me.
05:45And that's it, the process of this configuration
05:49and securing MySQL was really easy,
05:52much easier than we had with our HTTP D.
05:55Now, the last step left for us to do,
05:58we might wanna restart our server,
06:00but we don't really need to
06:02because our MySQL already reset its configurations.
06:07But if we ever wanted to, again, you remember,
06:09you could always call the sudo service MySQL restart,
06:13although it's not really necessary in this scenario.
06:17And I'm gonna just type the password for my demo user.
06:22And it's gonna stop MySQL, it's gonna restart MySQL.
06:27And that's really all we had to do
06:29to set up and configure HTTP D and MySQL.
06:34Now, the next step involved,
06:37which we're gonna look at in the next lesson
06:40is how do we set up and install PHP,
06:43which you'll be surprised or not
06:45that it's actually quite easy to install.
06:47So we'll see you in the next video when we do that.
