The adjoint of the perturbed and linearized compressible viscous flow equations is formulated in such a way that its solution can be used to optimize control actuation in order to reduce flow-generated sound. We apply it to a direct numerical simulation of a randomly excited two-dimensional mixing layer, with inflow vorticity-thickness Reynolds number 500 and free-stream Mach numbers 0.9 and 0.2. The control actuation is implemented as general source terms in the flow equations (body forces, mass sources, and internal energy sources) with compact support near the inflow boundary. The noise to be reduced is defined by a space-time integral of the mean-square pressure fluctuations on a line parallel to the mixing layer in the acoustic field of the low-speed stream. Both the adjoint and flow equations are solved numerically and without modelling approximations. The objective is to study the mechanics of the noise generation and its control. All controls reduce targeted noise with very little required input power, with the most effective (the internal energy control) reducing the noise intensity by 11 dB. Numerical tests confirm that the control is not by any simple acoustic cancellation mechanism but instead results from a genuine change of the flow as a source of sound. The comparison of otherwise identical flows with and without control applied shows little change of the flow's gross features: the evolution and pairings of the energetic structures, turbulence kinetic energy, spreading rate, and so on are superficially unchanged. However, decomposition of the flow into empirical eigenfunctions, as surrogates for Fourier modes in the non-periodic streamwise direction, shows that the turbulence structures advect downstream more uniformly. This change appears to be the key to reducing their acoustic efficiency, a perspective that is clarified by comparing the randomly excited mixing layer to a harmonically excited mixing layer, which is relatively quiet because it is highly ordered. Unfortunately, from the perspective of any practical implementation with actuators, the optimized control identified has a complex spatial and temporal structure, but it can be simplified. Two empirical eigenmodes were required to represent it sufficiently to reduce the targeted noise intensity by about 50%. Optimization of a simple single-degree-of-freedom control with an ad hoc spatial structure is less effective.