Optimality has not been addressed in existing works on control of (stochastic) nonholonomic systems. This paper presents a design of optimal controllers with respect to a meaningful cost function to globally asymptotically stabilize (in probability) nonholonomic systems affine in stochastic disturbances. The design is based on the Lyapunov direct method, the backstepping technique, and the inverse optimal control design. A class of Lyapunov functions, which are not required to be as nonlinearly strong as quadratic or quartic, is proposed for the control design. Thus, these Lyapunov functions can be applied to design of controllers for underactuated (stochastic) mechanical systems, which are usually required Lyapunov functions of a nonlinearly weak form. The proposed control design is illustrated on a kinematic cart, of which wheel velocities are perturbed by stochastic noise.