Mycobacterial diseases are prevalent in cancer and rheumatoid arthritis (RA) patients, especially those receiving tumor necrosis factor-α inhibitor (TNFi). However, the impact of cancer development on the risk of mycobacterial diseases among RA patients is unknown. Data from the Taiwan National Health Insurance Research Database were used to conduct a retrospective study to assess the occurrence of mycobacterial diseases in RA patients developing cancer (cancer-positive), those using TNFi (TNFi-exposure), those with cancer and using TNFi (cancer-TNFi-comb), and those without cancer and not using TNFi (cancer-TNFi-free). Cancer and TNFi exposure were time-dependent, and independent risk factors of mycobacterial diseases were assessed by Cox regression. Among 1344 RA patients diagnosed during 2000–2013, 68 (5·1%) developed cancer before their end points. The incidence rates of mycobacterial diseases in the cancer-positive (n = 56), TNFi-exposure (n = 290), cancer-TNFi-comb (n = 12), and cancer-TNFi-free (n = 986) subgroups were 6·7, 2·0, 7·6, and 1·3 per 1000 person-years, respectively. As compared with the cancer-TNFi-free group, the risk for mycobacterial diseases increased for the TNFi-exposure group (adjusted HR = 3·6, 95% confidence interval (95% CI) 1·1–11·5, P = 0·032) and remained high for cancer-positive (adjusted HR = 14·6, 95% CI 3·3–63·7, P < 0·001) after adjustment. This study suggested that cancer development increased the risk of mycobacterial diseases in RA patients, and risk assessment for this subgroup should be considered.