Most readers today probably associate the phrase "mad as a hatter" with Lewis Carroll's Alice's Adventures in Wonderland and the character of the Mad Hatter. Carroll did not invent the term, however, although he uses it to humorous effect when the Cheshire Cat says to Alice, "In THAT direction, . . . lives a Hatter: and in THAT direction, . . . lives a March Hare. Visit either you like: they're both mad." "As mad as a hatter" and "as mad as a March hare" are both phrases that predate Carroll.
No one really knows why the phrase "as mad as a hatter" became part of the lexicon. (Why should people who create hats be more prone to madness than, say, lawyers?) Several theories have been put forward, though.
2. It may be related to the archaic verb "to hatter," which, according to Samuel Johnson (1755) meant "to harass; to weary; to wear out with fatigue"--e.g. "He's hatter'd out."
3. It may have resulted from corruption of the Anglo-Saxon phrase "mad as an atter," which could be translated today as "poisonous as an adder (snake)."
At any rate, according to phrases.org.uk, "The earliest known printed citation of the phrase that I know of is from Blackwood's Edinburgh Magazine, January-June 1829.* It appears in a section of the magazine headed Noctes Ambrocianæ. No. XL1V, in a fictional conversation between a group of characters that wouldn't have been out of place in Wonderland:
NORTH: Many years--I was Sultan of Bello for a long period, until dethroned by an act of the grossest injustice ; but I intend to expose the traitorous conspirators to the indignation of an outraged world.
TICKLER (aside to SHEPHERD.): He's raving.
SHEPHERD (to TICKLER.): Dementit.
ODOHERTY (to both.): Mad as a hatter. Hand me a segar."
*This date is incorrect. The article appears in the July-December 1829 issue. The link provided takes you to the correct issue, although my cursory look through the article referenced does not reveal the phrase.