This is a tricky one, I have the following output:
mysqldump: Got error: 1045: Access denied for user \'root\'@\'localhost\' (using password: YES) when tr
Doing without the -u and -p worked for me (when I was logged in as root):
-u
-p
mysqldump --opt mydbname > mydbname.sql