Mysql has the "mysql" database by default. in this database are various tables defining the rights of the different users allowed to access the database. in the "user" table you can set the host from which the users are allowed to connect. if you use user name "foo" and password "bar" to log in to the database, simply set the host column to localhost where user name = foo, to accept only connections from localhost when this user is connecting to the database.
hope this made sense.