I work for a public mental health agency. AFAIK, all of our records are kept on site and are only released if there is a court order or the individual signs a release. Insurance companies require certain information for reimbursement, as does the gov't, but what they get is not very detailed. I honestly don't know what they do with it at that point. It is supposed to be private.
This may vary from state to state, but there is no central database of mentally ill people in my state. As has been previously mentioned, there would be a record of someone that was involuntarily comitted annd someone that was found NGRI.
One of the biggest problems is that most states are so vague in their definition of mental illness. Mental illness includes plenty of things besides hearing voices and certainly includes depression. There should not be a blanket rule that precludes all people that are (or were) mentally ill from owning or carrying firearms.
If you want to avoid the chance that your seeking treatment will end up on some kind of database, you have some choices. One is to go to a minister, though most will refer you out if you need to be seend over a long period of time or frequently. Another is to pay in cash. This can get expensive, depending on where you live or if you need medication, but it is certainly an option.